[英]Count orders in price range, and group by week number
i have to get some statistics from a MySQL Database, i need to get the amount of orders in ranges of order size, and then i need to group by WEEK, from the table created_at in orders我必须从 MySQL 数据库中获取一些统计信息,我需要获取订单大小范围内的订单数量,然后我需要从订单中的 created_at 表中按周分组
I hope this makes sense.我希望这是有道理的。
Here's what i have been able to come up with, but i have very little experience in advanced MySQL.这是我能够想出的,但我在高级 MySQL 方面的经验很少。
SELECT
x.Kurv, COALESCE(ordre, 0) AS ordre
FROM (
SELECT "0 - 100" AS Kurv
UNION SELECT "100 - 200"
UNION SELECT "200 - 300"
UNION SELECT "300 - 400"
UNION SELECT "400 - 500"
UNION SELECT "500 - 600"
UNION SELECT "over 600" ) x
LEFT JOIN
(SELECT
CASE when base_total_ex_tax >= 0 and base_total_ex_tax <= 100 then "0 - 100"
when base_total_ex_tax > 100 and base_total_ex_tax <= 200 then "100 - 200"
when base_total_ex_tax > 200 and base_total_ex_tax <= 300 then "200 - 300"
when base_total_ex_tax > 300 and base_total_ex_tax <= 400 then "300 - 400"
when base_total_ex_tax > 400 and base_total_ex_tax <= 500 then "400 - 500"
when base_total_ex_tax > 500 and base_total_ex_tax <= 600 then "500 - 600"
else "over 600"
END AS Kurv,
COUNT(*) as ordre
FROM orders
WHERE
created_at > '2017-01-01 00:00:00'
&&
status_id != 'canceled'
GROUP BY 1)
y ON x.Kurv = y.Kurv
Which outputs the ranges and orders fine, i just need to add the week group.哪个输出范围和订单很好,我只需要添加周组。
Thanks in advance.提前致谢。
You can find the week with CONCAT(YEAR(date), '/', WEEK(date))
.您可以使用
CONCAT(YEAR(date), '/', WEEK(date))
。 Then you can just group on it:然后你可以对它进行分组:
SELECT CONCAT(YEAR(date), '/', WEEK(date)) as wk
, CASE
WHEN amount <= 100 THEN '0 - 100'
WHEN amount <= 200 THEN '100 - 200'
ELSE '> 200'
END as kurve
, COUNT(*)
FROM orderstable
GROUP BY
wk
, kurve
If you want to list all kurves and weeks, even those without orders, you can add all kurves (as you've already done) and all weeks to the right-hand side of a left join.如果您想列出所有 kurves 和周,即使是那些没有订单的,您可以将所有 kurves(正如您已经完成的那样)和所有周添加到左连接的右侧。 That's typically easier to do client side.
这通常更容易做客户端。
Hoping, i understood your problem correctly.希望,我正确理解你的问题。
Please check below query请检查以下查询
SELECT
Y.WEEK_VAL , x.Kurv, COALESCE(ordre, 0) AS ordre
FROM (
SELECT "0 - 100" AS Kurv
UNION SELECT "100 - 200"
UNION SELECT "200 - 300"
UNION SELECT "300 - 400"
UNION SELECT "400 - 500"
UNION SELECT "500 - 600"
UNION SELECT "over 600" ) x
LEFT JOIN
(SELECT WEEK(created_at) WEEK_VAL,
CASE when 5 >= 0 and 5 <= 100 then "0 - 100"
when 5 > 100 and 5 <= 200 then "100 - 200"
when 5 > 200 and 5 <= 300 then "200 - 300"
when 5 > 300 and 5 <= 400 then "300 - 400"
when 5 > 400 and 5 <= 500 then "400 - 500"
when 5 > 500 and 5 <= 600 then "500 - 600"
else "over 600"
END AS Kurv,
COUNT(*) as ordre
FROM orders GROUP BY WEEK(created_at) , CASE when 5 >= 0 and 5 <= 100 then "0 - 100"
when 5 > 100 and 5 <= 200 then "100 - 200"
when 5 > 200 and 5 <= 300 then "200 - 300"
when 5 > 300 and 5 <= 400 then "300 - 400"
when 5 > 400 and 5 <= 500 then "400 - 500"
when 5 > 500 and 5 <= 600 then "500 - 600"
else "over 600"
END)
y ON x.Kurv = y.Kurv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.