简体   繁体   中英

MySQL CASE Statement with multiple columns in statement list

I have this query, which I know doesn't work, but I've left it as it is as pseudo-code to help explain what I'm doing. I'm trying to get "Booking" and "Sales" totals from a Booking table by day-of-the-week for the past week. Hence, Mon1B = Bookings for Monday and Mon1S = Sales for Monday.

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#'

How can something like this be done in MySQL?

Yes, but case can only return one value. You can do it like this:

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#'

You might find it easier as 7 rows, though:

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

I think you want something like this:

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

Or, if you prefer the equivalent (but lengthier) CASE expression:

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

The result of a CASE expression is a scalar; it can't return more than one value.

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#'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM