获取不同记录的总和并比较两个日期范围
get sum of distinct records and compare between two date ranges
我有一个名为 purchases 的 table,包含以下列:名称、金额、城市、日期。
我想return金额之和,按金额之和最大的城市排序,比较两个日期范围。
这是一个fiddle:http://sqlfiddle.com/#!9/6b2017/6
我可以按 sum(amount) desc(第 1 期)的顺序获得第一组城市及其 sum(amount):
select distinct city, sum(amount) as total
from purchases
where date between '2020-07-01' and '2020-10-31'
group by city
order by sum(amount) desc
limit 3
上述 returns 个城市的查询:塔尔萨、纽约、芝加哥
但我也想进入同一个查询,前 3 个月,但使用与上一个查询中 returned 相同的城市,类似这样,但就像我提到的,我想要它 return 仅在一个查询中编辑(第 2 期)
select city, sum(amount) as total
from purchases
where city in ('Tulsa', 'New York', 'Chicago')
and date between '2020-03-01' and '2020-06-31'
group by city
寻找 return 个城市(按照它们在第一个查询中的顺序),每个日期范围都有 sum(amount),所以理论上:
塔尔萨第 1 期:总和(数量)
塔尔萨时期 2:sum(amount)
纽约时期 1:sum(amount)
纽约时期 2:sum(amount)
芝加哥时期 1:sum(amount)
芝加哥时期 2:sum(amount)
感谢任何帮助
使用 LEFT
将您的查询连接到 table 并再次聚合:
SELECT t.city, t.total period1, COALESCE(SUM(p.amount), 0) period2
FROM (
SELECT city, SUM(amount) AS total
FROM purchases
WHERE date BETWEEN '2020-07-01' AND '2020-10-31'
GROUP BY city
ORDER BY total DESC LIMIT 3
) t LEFT JOIN (
SELECT city, amount
FROM purchases
WHERE date BETWEEN '2020-03-01' AND '2020-06-30'
) p ON p.city = t.city
GROUP BY t.city, t.total
ORDER BY t.total DESC;
或者,使用条件聚合:
SELECT city,
SUM(CASE WHEN date BETWEEN '2020-07-01' AND '2020-10-31' THEN amount ELSE 0 END) period1,
SUM(CASE WHEN date BETWEEN '2020-03-01' AND '2020-06-30' THEN amount ELSE 0 END) period2
FROM purchases
GROUP BY city
ORDER BY period1 DESC LIMIT 3;
或:
SELECT city,
SUM(CASE WHEN date >= '2020-07-01' THEN amount ELSE 0 END) period1,
SUM(CASE WHEN date < '2020-07-01' THEN amount ELSE 0 END) period2
FROM purchases
WHERE date BETWEEN '2020-03-01' AND '2020-10-31'
GROUP BY city
ORDER BY period1 DESC LIMIT 3;
参见demo。
我有一个名为 purchases 的 table,包含以下列:名称、金额、城市、日期。
我想return金额之和,按金额之和最大的城市排序,比较两个日期范围。
这是一个fiddle:http://sqlfiddle.com/#!9/6b2017/6
我可以按 sum(amount) desc(第 1 期)的顺序获得第一组城市及其 sum(amount):
select distinct city, sum(amount) as total
from purchases
where date between '2020-07-01' and '2020-10-31'
group by city
order by sum(amount) desc
limit 3
上述 returns 个城市的查询:塔尔萨、纽约、芝加哥
但我也想进入同一个查询,前 3 个月,但使用与上一个查询中 returned 相同的城市,类似这样,但就像我提到的,我想要它 return 仅在一个查询中编辑(第 2 期)
select city, sum(amount) as total
from purchases
where city in ('Tulsa', 'New York', 'Chicago')
and date between '2020-03-01' and '2020-06-31'
group by city
寻找 return 个城市(按照它们在第一个查询中的顺序),每个日期范围都有 sum(amount),所以理论上:
塔尔萨第 1 期:总和(数量) 塔尔萨时期 2:sum(amount) 纽约时期 1:sum(amount) 纽约时期 2:sum(amount) 芝加哥时期 1:sum(amount) 芝加哥时期 2:sum(amount)
感谢任何帮助
使用 LEFT
将您的查询连接到 table 并再次聚合:
SELECT t.city, t.total period1, COALESCE(SUM(p.amount), 0) period2
FROM (
SELECT city, SUM(amount) AS total
FROM purchases
WHERE date BETWEEN '2020-07-01' AND '2020-10-31'
GROUP BY city
ORDER BY total DESC LIMIT 3
) t LEFT JOIN (
SELECT city, amount
FROM purchases
WHERE date BETWEEN '2020-03-01' AND '2020-06-30'
) p ON p.city = t.city
GROUP BY t.city, t.total
ORDER BY t.total DESC;
或者,使用条件聚合:
SELECT city,
SUM(CASE WHEN date BETWEEN '2020-07-01' AND '2020-10-31' THEN amount ELSE 0 END) period1,
SUM(CASE WHEN date BETWEEN '2020-03-01' AND '2020-06-30' THEN amount ELSE 0 END) period2
FROM purchases
GROUP BY city
ORDER BY period1 DESC LIMIT 3;
或:
SELECT city,
SUM(CASE WHEN date >= '2020-07-01' THEN amount ELSE 0 END) period1,
SUM(CASE WHEN date < '2020-07-01' THEN amount ELSE 0 END) period2
FROM purchases
WHERE date BETWEEN '2020-03-01' AND '2020-10-31'
GROUP BY city
ORDER BY period1 DESC LIMIT 3;
参见demo。