I have two tables that I need to join... I want to join table1 and table2 on 'id' - however in table two id is not unique. I only want one value returned for table two, and this value represents the sum of a column called 'total_sold' - within a specified date range (say one month), however I want more than one date range at the same time...
SELECT ta.id, sum(tb.total_sold) as total_sold_this_week, sum(tc.total_sold) as total_sold_this_month
FROM table_a as ta
LEFT JOIN table_b as tb ON ta.id=tb.id AND tb.date_sold BETWEEN ADDDATE(NOW(),INTERVAL -1 WEEK) AND NOW()
LEFT JOIN table_b as tc ON ta.id=tc.id AND tc.date_sold BETWEEN ADDDATE(NOW(),INTERVAL -1 MONTH) AND NOW()
GROUP BY ta.id
this works but does not SUM the rows - only returning one row for each id... how do I get the sum from table b instead of only one row???
Please criticise if format of question could use more work - I can rewrite and provide sample data if required - this is a trivialised version of a much larger problem.
-Thanks
Using Subqueries
One way to solve this would be to use subqueries. LEFT JOIN creates a new "result" for each match in the right table, so using two LEFT JOINs is creating more ROWS than you want. You could just sub select the value you want, but this can be slow:
SELECT ta.id,
(SELECT SUM(total_sold) as total_sold
FROM table_b
WHERE date_sold BETWEEN ADDDATE(NOW(), INTERVAL -1 WEEK) AND NOW()
AND id=ta.id) as total_sold_this_week,
(SELECT SUM(total_sold) as total_sold
FROM table_b
WHERE date_sold BETWEEN ADDDATE(NOW(), INTERVAL -1 MONTH) AND NOW()
AND id = ta.id) as total_sold_this_month
FROM table_a ta;
Result:
+----+----------------------+-----------------------+
| id | total_sold_this_week | total_sold_this_month |
+----+----------------------+-----------------------+
| 1 | 3 | 7 |
| 2 | 4 | 4 |
| 3 | NULL | NULL |
+----+----------------------+-----------------------+
3 rows in set (0.04 sec)
Using SUM(CASE ...)
This method doesn't use subqueries (and will likely be faster on larger data sets). We want to join the table_a and table_b together once, using our "biggest" date range, and then use a SUM() based on a CASE to calculate the "smaller range".
SELECT ta.*,
SUM(total_sold) as total_sold_last_month,
SUM(CASE
WHEN date_sold BETWEEN NOW() - INTERVAL 1 WEEK AND NOW()
THEN total_sold
ELSE 0
END) as total_sold_last_week
FROM table_a AS ta
LEFT JOIN table_b AS tb
ON ta.id=tb.id AND tb.date_sold BETWEEN ADDDATE(NOW(),INTERVAL -1 MONTH) AND NOW()
GROUP BY ta.id;
This returns nearly the same resultset as the subquery example:
+----+-----------------------+----------------------+
| id | total_sold_last_month | total_sold_last_week |
+----+-----------------------+----------------------+
| 1 | 7 | 3 |
| 2 | 4 | 4 |
| 3 | NULL | 0 |
+----+-----------------------+----------------------+
3 rows in set (0.00 sec)
The only difference is the 0 instead of NULL. You could summarize as many date ranges as you'd like using this method, but its still probably best to limit the rows returned to the largest range in the ON clause.
Just to show how it works: removing the GROUP BY and SUM() calls, and adding date_sold to the SELECT returns this:
+----+------------+-----------------------+----------------------+
| id | date_sold | total_sold_last_month | total_sold_last_week |
+----+------------+-----------------------+----------------------+
| 1 | 2010-04-30 | 2 | 2 |
| 1 | 2010-04-24 | 2 | 0 |
| 1 | 2010-04-24 | 2 | 0 |
| 1 | 2010-05-03 | 1 | 1 |
| 2 | 2010-05-03 | 4 | 4 |
| 3 | NULL | NULL | 0 |
+----+------------+-----------------------+----------------------+
6 rows in set (0.00 sec)
Now when you GROUP BY id, and SUM() the two total_sold columns you have your results!
Old Advice
Before you brought the two different date ranges into the mix, you could use GROUP BY to group using the table id on table1, and the SUM() aggregate function to add up the rows returned.
SELECT ta.id, SUM(tb.total_sold) as total_sold_this_week
FROM table_a as ta
LEFT JOIN table_b as tb
ON ta.id=tb.id AND tb.date_sold BETWEEN ADDDATE(NOW(),INTERVAL -3 WEEK) AND NOW()
GROUP BY ta.id
+----+----------------------+
| id | total_sold_this_week |
+----+----------------------+
| 1 | 7 |
| 2 | 4 |
| 3 | NULL |
+----+----------------------+
3 rows in set (0.00 sec)
The test data
NOW() is 2010-05-03
mysql> select * from table_a; select * from table_b;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
3 rows in set (0.00 sec)
+----+------------+------------+
| id | date_sold | total_sold |
+----+------------+------------+
| 1 | 2010-04-24 | 2 |
| 1 | 2010-04-24 | 2 |
| 1 | 2010-04-30 | 2 |
| 1 | 2010-05-03 | 1 |
| 2 | 2010-05-03 | 4 |
+----+------------+------------+
5 rows in set (0.00 sec)
Related
There are tables:
1.current_table:
date value
02.10.2019 1
03.10.2019 2
04.10.2019 2
05.10.2019 -1
06.10.2019 1
07.10.2019 1
08.10.2019 2
09.10.2019 2
10.10.2019 -1
11.10.2019 2
12.10.2019 1
2.intervals
date_start date_end
02.10.2019 04.10.2019 3
06.10.2019 09.10.2019 4
11.10.2019 12.10.2019 2
"intervals" table contains maximum length of an uninterrupted sequence of positive values.
How to select rows from "current_table" between each interval taken from "intervals" table (there are many of such intervals)?
So result should be:
date value
02.10.2019 1
03.10.2019 2
04.10.2019 2
06.10.2019 1
07.10.2019 1
08.10.2019 2
09.10.2019 2
11.10.2019 2
12.10.2019 1
My first inclination is simply:
select t1.*
from table1 t1
where t1.value > 0;
Perhaps your intervals might overlap. Or you might want to filter only for intervals in the second table. If so, then exists is handy:
select t1.*
from table1 t1
where t1.value > 0 and
exists (select 1
from table2 t2
where t1.date between t2.date_start and t2.date_end
);
This is overkill for your sample data, though.
Join the tables.
Only the rows that belong to an interval in table intervals will be returned:
select t.*
from current_table t inner join intervals i
on t.date between i.date_start and i.date_end
See the demo.
Or with EXISTS:
select t.*
from current_table t
where exists (
select 1 from intervals i
where t.date between i.date_start and i.date_end
)
See the demo.
Results:
| date | value |
| ---------- | ----- |
| 2019-02-10 | 1 |
| 2019-03-10 | 2 |
| 2019-04-10 | 2 |
| 2019-06-10 | 1 |
| 2019-07-10 | 1 |
| 2019-08-10 | 2 |
| 2019-09-10 | 2 |
| 2019-11-10 | 2 |
| 2019-12-10 | 1 |
I have a table that has an autoincremented numeric primary. I'm trying to get a count of rows that match a condition grouped by increments of their primary key. Given the data:
| id | value |
|----|-------|
| 1 | a |
| 2 | b |
| 3 | a |
| 4 | a |
| 5 | b |
| 6 | a |
| 7 | b |
| 8 | a |
| 9 | b |
| 10 | b |
| 11 | a |
| 12 | b |
If I wanted to know how many rows matched value = 'a' for every five rows, the result should be:
| count(0) |
|----------|
| 3 |
| 2 |
| 1 |
I can nest a series of subqueries in the SELECT statement, like such:
SELECT (SELECT count(0)
FROM table
WHERE value = 'a'
AND id > 0
AND id <= 5) AS `1-5`,
(SELECT count(0)
FROM table
WHERE value = 'a'
AND id > 5
AND id <=10) AS `6-10`,
...
But is there a way to do this with a GROUP BY statement or something similar where I don't have to manually write out the increments? If not, is there a more time efficient method than a series of subqueries in the SELECT statement as in the above example?
You could divide the ID by 5 and then ceil the result:
SELECT CONCAT((CEIL(id / 5.0) - 1) * 5, '-', CEIL(id / 5.0) * 5), COUNT(*)
FROM mytable
WHERE value = 'a'
GROUP BY CEIL(id / 5.0)
The following aggregated query should do the trick :
SELECT CEIL(id/5), COUNT(*)
FROM table
WHERE value = 'a'
GROUP BY CEIL(id/5)
To track orders i need order id from orders table and check orderdates table that order is made on specific date range. Using following query i can get id's but it will return id 3 also which is not needed.
Example: See orderdates table. First date of id 3 is overlapping with last date of id 2.
How i have to change query that id is not returned if it's first date is overlapping some other id's last date or is it even possible?
SELECT id
FROM orders
WHERE id in(
SELECT id
FROM orderdates
WHERE
orderdate BETWEEN '2017-06-01' AND '2017-06-30'
GROUP BY id);
orders
id sent
1 1
2 1
3 1
orderdates
id orderdate
1 2017-6-10
1 2017-6-11
1 2017-6-12
1 2017-6-13
2 2017-6-14
2 2017-6-15
2 2017-6-16
2 2017-6-17 <--- Last date of id 2
3 2017-6-17 <--- First date of id 3
3 2017-6-18
3 2017-6-19
3 2017-6-20
Maybe this where I compare the min date for every id against the max date for every id
select u.*
from t u
where u.id not in
(
select z.id
from
(
select s.*,t.id tid,t.mindt tmindt,t.maxdt tmaxdt
from
(
select id, min(dt) mindt,max(dt) maxdt
from t
group by id
order by id
) s
join
(select id, min(dt) mindt,max(dt) maxdt
from t
group by id
order by id
) t on
t.id <> s.id and
t.maxdt = s.mindt
) z
)
order by u.id,u.dt
given this
truncate table t;
insert into t values
(1,'2017-01-01'),(1,'2017-01-02'),
(2,'2017-01-03'),(2,'2017-01-04'),
(3,'2017-01-04')
results in
+------+------------+
| id | dt |
+------+------------+
| 1 | 2017-01-01 |
| 1 | 2017-01-02 |
| 2 | 2017-01-03 |
| 2 | 2017-01-04 |
+------+------------+
4 rows in set (0.00 sec)
given this
truncate table t;
insert into t values
(1,'2017-01-01'),(1,'2017-01-02'),
(2,'2017-01-03'),(2,'2017-01-04'),
(3,'2017-01-04'),
(4,'2017-01-04'),(4,'2017-01-06');
Result
+------+------------+
| id | dt |
+------+------------+
| 1 | 2017-01-01 |
| 1 | 2017-01-02 |
| 2 | 2017-01-03 |
| 2 | 2017-01-04 |
+------+------------+
4 rows in set (0.00 sec)
Given
truncate table t;
insert into t values
(1,'2017-01-01'),(1,'2017-01-02'),
(2,'2017-01-03'),(2,'2017-01-04'),
(3,'2017-01-04'),
(4,'2017-01-05'),(4,'2017-01-06');
result
+------+------------+
| id | dt |
+------+------------+
| 1 | 2017-01-01 |
| 1 | 2017-01-02 |
| 2 | 2017-01-03 |
| 2 | 2017-01-04 |
| 4 | 2017-01-05 |
| 4 | 2017-01-06 |
+------+------------+
6 rows in set (0.02 sec)
This seems to be a convoluted problem, but I'll try my best to articulate the idea and illustrate a scenario. Essentially I have two tables that need to be combined and returned as the result set for a single query. One table needs to be merged into the other in a specific order.
Say table one is called Articles and table two is called Features. Both tables have an ID field with unique numbers. Articles has a date field which will be used to initially sort its records in descending order. The Features table has a Delta field which be used initially to sort its records. Some of the records in the Features table are placeholders and are not meant to be included in the final set. Their only purpose is to affect the sort order. Each record has a unique value in the Delta field, from 1 - X which will be used to sort these records. Another field called Skip has a value of 1 if it should be eliminated when merging the two tables together. Again, the only purpose to the skipped records is to take up space during the initial sort on the Features table. Even though they are unnecessary, they exist and can't be deleted.
The tricky part is that when the results from both tables are merged, any non-skipped records from the Features table need to be inserted into the results from the Articles table in the exact order they appears in the Features table.
So lets say I have 6 records in the Features table, A - F and the order field ranges from 1 - 6. Records A,B,D,E all have a value of 1 in the Skip field. That means I'm only interested in records C and F both of which need to be inserted into the final record set in positions 3 and 6 respectively.
The records may look something like this for the Articles table:
+----+------------+
| id | date |
+----+------------+
| 1 | 9999999999 |
+----+------------+
| 2 | 9999999998 |
+----+------------+
| 3 | 9999999997 |
+----+------------+
| 4 | 9999999996 |
+----+------------+
| 5 | 9999999995 |
+----+------------+
| 6 | 9999999994 |
+----+------------+
| 7 | 9999999993 |
+----+------------+
| 8 | 9999999992 |
+----+------------+
| 9 | 9999999991 |
+----+------------+
| 10 | 9999999990 |
+----+------------+
The Features table may look something like this:
+----+------+-------+------+
| id | name | delta | skip |
+----+------+-------+------+
| 11 | A | 1 | 1 |
+----+------+-------+------+
| 12 | B | 2 | 1 |
+----+------+-------+------+
| 13 | C | 3 | 0 |
+----+------+-------+------+
| 14 | D | 4 | 1 |
+----+------+-------+------+
| 15 | E | 5 | 1 |
+----+------+-------+------+
| 16 | F | 6 | 0 |
+----+------+-------+------+
The results would look something like this (not including any additional fields that might be needed to achieve my goal):
+----+
| id |
+----+
| 1 |
+----+
| 2 |
+----+
| 13 | (record from the Features table in the third position)
+----+
| 3 |
+----+
| 4 |
+----+
| 16 | (record from the Features table in the sixth position)
+----+
| 5 |
+----+
| 6 |
+----+
| 7 |
+----+
| 8 |
+----+
| 9 |
+----+
| 10 |
+----+
Hope my explanation makes sense. Any ideas?
Thanks,
Howie
I assume that there is a mistake in your example - record id=16 is sixth row in Features table, so should be after id=5 in results, not before.
Try the blelow query. Here is SQLFiddle.
select id from (
select `date`, null delta, id
from Articles
union all
select a.`date`, f.delta, f.id
from (
select (#x:=#x+1) rn, a.*
from Articles a, (select #x:=0) z
order by a.`date` desc
) a
join (
select (#y:=#y+1) rn, f.id, f.delta, f.skip
from Features f, (select #y:=0) z
order by f.delta
) f
on a.rn = f.rn
where f.skip <> 1
order by `date` desc, isnull( delta ), delta
) merge
Looks like this example in SQL Fiddle did it for me.
SELECT id, sort_order FROM (
SELECT `date`, NULL delta, id, (#a_count:=#a_count+1) sort_order
FROM Articles a_main, (SELECT #a_count:=-1) z
UNION ALL
SELECT a.`date`, f.delta, f.id, f.weighted_rn
FROM (
SELECT (#x:=#x+1) rn, a.*
FROM Articles a, (SELECT #x:=-1) z
ORDER BY a.`date` DESC
) a
JOIN (
SELECT (#y:=#y+1) rn, TRUNCATE((f.delta - #y - (1/#y)),2) AS weighted_rn, f.id, f.delta, f.skip
FROM Features f, (SELECT #y:=-1) z
WHERE f.skip <> 1
ORDER BY f.delta
) f
ON a.rn = f.rn
ORDER BY sort_order
) merge
Thanks to Kordirko for the framework.
I'm trying to do something like 'select groupwise maximum', but I'm looking for groupwise order number.
so with a table like this
briefs
----------
id_brief | id_case | date
1 | 1 | 06/07/2010
2 | 1 | 04/07/2010
3 | 1 | 03/07/2010
4 | 2 | 18/05/2010
5 | 2 | 17/05/2010
6 | 2 | 19/05/2010
I want a result like this
breifs result
----------
id_brief | id_case | dateOrder
1 | 1 | 3
2 | 1 | 2
3 | 1 | 1
4 | 2 | 2
5 | 2 | 1
6 | 2 | 3
I think I want to do something like described here MySQL - Get row number on select, but I don't know how I would reset the variable for each id_case.
This will give you how many records are there with this id_case value and a date less than or equal to this date value.
SELECT t1.id_brief,
t1.id_case,
COUNT(t2.*) AS dateOrder
FROM yourtable AS t1
LEFT JOIN yourtable AS t2 ON t2.id_case = t1.id_case AND t2.date <= t1.date
GROUP BY t1.id_brief
Mysql is permissive about columns which can be queries using GROUP BY. With a more stric DBMS you may need GROUP BY t1.id_brief, t1.id_case.
I strongly advise you to have the right indexes on the table:
CREATE INDEX filter1 ON yourtabl (id_case, date)