0

我有这个查询,我知道它不起作用,但我把它保留为伪代码,以帮助解释我在做什么。我正在尝试按过去一周的星期几从预订表中获取“预订”和“销售”总计。因此,Mon1B = 星期一的预订,Mon1S = 星期一的销售额。

SELECT  
   CASE WEEKDAY(b.created)
   WHEN 0 THEN (SELECT COUNT(uuid) as Mon1B, SUM(amount) as Mon1S)
   WHEN 1 THEN (SELECT COUNT(uuid) as Tue1B, SUM(amount) as Tue1S)
   WHEN 2 THEN (SELECT COUNT(uuid) as Wed1B, SUM(amount) as Wed1S)
   WHEN 3 THEN (SELECT COUNT(uuid) as Thu1B, SUM(amount) as Thu1S)
   WHEN 4 THEN (SELECT COUNT(uuid) as Wed1B, SUM(amount) as Wed1S)
   WHEN 5 THEN (SELECT COUNT(uuid) as Wed1B, SUM(amount) as Wed1S)
   WHEN 6 THEN (SELECT COUNT(uuid) as Wed1B, SUM(amount) as Wed1S)
END CASE
FROM Bookings b
WHERE b.created > '#week1Start#' and b.created <= '#week1End#'

如何在 MySQL 中完成这样的事情?

4

3 回答 3

3

是的,但是 case 只能返回一个值。你可以这样做:

SELECT sum(CASE when WEEKDAY(b.created) = 0 then 1 else 0 end) as Mon1b,
       sum(case when weekday(b.created) = 0 then amount else 0 end) as Mon1S,
       ...
FROM Bookings b
WHERE b.created > '#week1Start#' and b.created <= '#week1End#'

不过,您可能会发现 7 行更容易:

select WEEKDAY(b.created), count(*) as cnt, sum(amount) as amt
from Bookings b
WHERE b.created > '#week1Start#' and b.created <= '#week1End#'
group by WEEKDAY(b.created)
order by 1
于 2012-07-26T23:50:59.023 回答
1

我想你想要这样的东西:

SELECT COUNT(IF(WEEKDAY(b.created)=0,uuid,NULL)) AS Mon1B
     , SUM(IF(WEEKDAY(b.created)=0,amount,NULL)) AS Mon1S
     , COUNT(IF(WEEKDAY(b.created)=1,uuid,NULL)) AS Tue1B
     , SUM(IF(WEEKDAY(b.created)=1,amount,NULL)) AS Tue1S

或者,如果您更喜欢等效(但更长)的 CASE 表达式:

SELECT COUNT(CASE WEEKDAY(b.created) WHEN 0 THEN uuid END) AS Mon1B
     , SUM(CASE WEEKDAY(b.created) WHEN 0 THEN amount END) AS Mon1S
     , COUNT(CASE WEEKDAY(b.created) WHEN 1 THEN uuid END) AS Tue1B
     , SUM(CASE WEEKDAY(b.created) WHEN 1 THEN amount END) AS Tue1S

CASE 表达式的结果是一个标量;它不能返回多个值。

于 2012-07-26T23:54:40.190 回答
0
SELECT  WEEKDAY(b.created),
   CASE 
   WHEN b.weekday='Monday' THEN (SELECT COUNT(b.uuid) as Mon1B, SUM(s.amount) as Mon1S from bookings b,sales s      where b.day='Monday' and s.day='Monday')
   WHEN b.weekday='Tuesday' THEN (SELECT COUNT(b.uuid) as Tue1B, SUM(s.amount) as Tue1 from bookings b,sales s      where b.day='Tuesday' and s.day='tuesday')
   WHEN b.weekday='Wednesday' THEN (SELECT COUNT(b.uuid) as Wed1B, SUM(s.amount) as Wed1S from bookings b,sales s      where b.day='wednesday' and s.day='wednesday')
   WHEN b.weekday='Thursday' THEN (SELECT COUNT(b.uuid) as Thu1B, SUM(s.amount) as Thu1S from bookings b,sales s      where b.day='Thursday' and s.day='Thursday')
   WHEN b.weekday='Friday' THEN (SELECT COUNT(b.uuid) as Fri1B, SUM(s.amount) as Fri1S from bookings b,sales s      where b.day='Friday' and s.day='Friday')
   WHEN b.weekday='saturaday' THEN (SELECT COUNT(b.uuid) as Sat1B, SUM(s.amount) as sat1S from bookings b,sales s      where b.day='Saturaday' and s.day='Saturaday')
   WHEN b.weekday='Sunday' THEN (SELECT COUNT(b.uuid) as sun1B, SUM(s.amount) as sun1S from bookings b,sales s      where b.day='Sunday' and s.day='Sunday')
END CASE
FROM Bookings b
WHERE b.created > '#week1Start#' and b.created <= '#week1End#'
于 2013-03-13T04:26:19.127 回答