MySQL 获取两个日期和另一个过滤器之间的总值时出错
MySQL error getting total values between two dates and another filter
我想从两个日期之间的 table 中获取 SUM 值,但我还有另一个过滤器。我遇到语法错误,找不到问题所在。这是SQL句:
SUM(IF((date BETWEEN '2020-01-01 00:00:00' AND '2020-01-31 23:59:59') AND ql=1),total,0) AS t1q1,
SUM(IF((date BETWEEN '2020-01-01 00:00:00' AND '2020-01-31 23:59:59') AND ql=2),total,0) AS t1q2,
SUM(IF((date BETWEEN '2019-12-01 00:00:00' AND '2019-12-31 23:59:59') AND ql=1),total,0) AS t2q1,
SUM(IF((date BETWEEN '2019-12-01 00:00:00' AND '2019-12-31 23:59:59') AND ql=2),total,0) AS t2k2
FROM myTable
GROUP BY productID
ORDER BY productID ASC
SELECT
SUM(case when date>='2020-01-01' and date<'2020-02-01' AND ql=1 then total else 0 end) t1q1,
SUM(case when date>='2020-01-01' and date<'2020-02-01' AND ql=2 then total else 0 end) t1q2,
SUM(case when date>='2019-12-01' and date<'2020-01-01' AND ql=1 then total else 0 end) t2q1,
SUM(case when date>='2019-12-01' and date<'2020-01-01' AND ql=2 then total else 0 end) t2q2
FROM myTable
GROUP BY productID
ORDER BY productID ASC
我想从两个日期之间的 table 中获取 SUM 值,但我还有另一个过滤器。我遇到语法错误,找不到问题所在。这是SQL句:
SUM(IF((date BETWEEN '2020-01-01 00:00:00' AND '2020-01-31 23:59:59') AND ql=1),total,0) AS t1q1,
SUM(IF((date BETWEEN '2020-01-01 00:00:00' AND '2020-01-31 23:59:59') AND ql=2),total,0) AS t1q2,
SUM(IF((date BETWEEN '2019-12-01 00:00:00' AND '2019-12-31 23:59:59') AND ql=1),total,0) AS t2q1,
SUM(IF((date BETWEEN '2019-12-01 00:00:00' AND '2019-12-31 23:59:59') AND ql=2),total,0) AS t2k2
FROM myTable
GROUP BY productID
ORDER BY productID ASC
SELECT
SUM(case when date>='2020-01-01' and date<'2020-02-01' AND ql=1 then total else 0 end) t1q1,
SUM(case when date>='2020-01-01' and date<'2020-02-01' AND ql=2 then total else 0 end) t1q2,
SUM(case when date>='2019-12-01' and date<'2020-01-01' AND ql=1 then total else 0 end) t2q1,
SUM(case when date>='2019-12-01' and date<'2020-01-01' AND ql=2 then total else 0 end) t2q2
FROM myTable
GROUP BY productID
ORDER BY productID ASC