所以我有两个 Mysql 表,我很难进行以下查询。
查询:获取每个城市每月的销售额
用户 ID、姓名、城市
销售 *id、user_id、姓名、金额、日期(日期时间)*
我从SELECT MONTHNAME(datetime) AS month GROUP BY month.
提前致谢!
假设您想要任何月份和/或没有任何销售的城市的数字为 0,但是当月其他城市有销售时,则如下所示:-
交叉连接一对子选择,一个获取使用的月份列表,一个获取城市列表,然后将这些与记录相结合以获取该月/城市的金额,并将这些金额相加:-
SELECT Sub1.YearMonth, Sub2.city, SUM(sales.amount)
FROM (SELECT DISTINCT EXTRACT(YEAR_MONTH FROM `date`) AS YearMonth
FROM sales) Sub1
CROSS JOIN (SELECT DISTINCT city FROM users) Sub2
LEFT OUTER JOIN sales ON Sub1.YearMonth = EXTRACT(YEAR_MONTH FROM sales.`date`)
LEFT OUTER JOIN users ON sales.user_id = users.id AND Sub2.city = sales.city
GROUP BY Sub1.YearMonth, Sub2.city
ORDER BY Sub1.YearMonth, Sub2.city
不利的一面是,如果您有一个月没有卖给任何人,那么这个月根本不会出现。为了解决这个问题,您需要更改月份的子选择以取而代之的是开始日期并向其中添加一系列数字以获取每个月。
生成范围的示例如下:-
SELECT Sub1.YearMonth, Sub2.city, SUM(sales.amount)
FROM (SELECT EXTRACT(YEAR_MONTH FROM DATE_ADD('2009-01-01', INTERVAL a.i*100+b.i*10+c.i MONTH)) AS aMonth
FROM (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) a,
(SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) b,
(SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) c
WHERE DATE_ADD('2009-01-01', INTERVAL a.i*100+b.i*10+c.i MONTH) <= '2014-12-01') Sub1
CROSS JOIN (SELECT DISTINCT city FROM users) Sub2
LEFT OUTER JOIN sales ON Sub1.YearMonth = EXTRACT(YEAR_MONTH FROM sales.`date`)
LEFT OUTER JOIN users ON sales.user_id = users.id AND Sub2.city = sales.city
GROUP BY Sub1.YearMonth, Sub2.city
ORDER BY Sub1.YearMonth, Sub2.city
此示例在 2009-01-01 和 2014-12-01 (含)之间给出每个月,然后按城市获取该范围内的所有销售额。它将应对长达 1000 个月的范围。
嘿菜鸟(好名字顺便说一句):D
试试这个,我添加了YEAR()
onGROUP BY
子句,因为月份数字每年重复,我想你不希望同一月份但年份不同的记录的销售额总和。SELECT
如果您不想要,您可以在声明中省略年份。
SELECT YEAR(S.date) AS Year
, MONTHNAME(S.date) AS Month
, U.city
, SUM(S.amount) AS SalesPerMonthPerCity
FROM sales S
INNER JOIN users U ON U.id = S.user_id
GROUP BY YEAR(S.date), MONTH(S.date), U.city
您有一个SQL Fiddle 测试页面,因此您和其他人可以尝试该解决方案。
这是有问题的查询:
SELECT MONTHNAME(`date`) AS Monthly
, `city`
, SUM(`amount`) AS Profit
FROM sales
INNER JOIN users ON userid = idu
GROUP BY MONTHNAME(`date`), `city` WITH ROLLUP
用于WITH ROLLUP
按月添加小计并在末尾添加总计。
PS:我同意这些评论,你应该在解决你的问题上付出更多的努力,你对 SO 并不陌生,所以你应该意识到这一点;)