SQL计数每次出现的情况都是sqlite

时间:2015-10-09 06:13:15

标签: mysql sqlite

我正在尝试返回2列:星期几和当天发生的次数。我如何返回每天的出现次数?

SELECT case CAST(strftime('%w', HireDate) AS INTEGER)
   WHEN 0 then 'Sunday'
   WHEN 1 then 'Monday'
   WHEN 2 then 'Tuesday'
   WHEN 3 then 'Wednesday'
   WHEN 4 then 'Thursday'
   WHEN 5 then 'Friday'
   ELSE 'Saturday' END AS 'Day of week',  AS 'Hired'
   FROM Employee;

1 个答案:

答案 0 :(得分:0)

只需在group by子句中重写案例:

SELECT 
   ( case CAST(strftime('%w', HireDate) AS INTEGER)
   WHEN 0 then 'Sunday'
   WHEN 1 then 'Monday'
   WHEN 2 then 'Tuesday'
   WHEN 3 then 'Wednesday'
   WHEN 4 then 'Thursday'
   WHEN 5 then 'Friday'
   ELSE 'Saturday' END ) AS 'Day of week', 
   count(*) AS 'Hired'                              --<--here
 FROM 
   Employee
 GROUP BY                                           --<--here
   ( case CAST(strftime('%w', HireDate) AS INTEGER)
   WHEN 0 then 'Sunday'
   WHEN 1 then 'Monday'
   WHEN 2 then 'Tuesday'
   WHEN 3 then 'Wednesday'
   WHEN 4 then 'Thursday'
   WHEN 5 then 'Friday'
   ELSE 'Saturday' END );