使用CASE WHEN和GROUP BY

时间:2012-11-05 09:04:12

标签: mysql sql group-by case

背景:我正在尝试显示一个带有标题的表:id,full_name,position_7,position_8,position_9 ...等等,如果一个人是7月和8月的活跃员工,那么2012_Jul和2012_Aug将显示在position_7和8在一行上。如果我不使用GROUP BY,我会获得输出但是在2行。发生这种情况的原因是因为在BLMths表中,由于被雇用超过1个月,一个人将有多个条目。

以下查询将成功获取每个人的id,full_name和月份...如果我不使用GROUP BY。但问题是,如果我忽略使用GROUP BY,我的表中每个人都会有4行(或更多行)。当我使用GROUP BY时,我为每个人提供了所需的1行输出,但表中所需的数据并非全部都存在。

有谁知道我做错了什么?或者有人知道为什么当我使用GROUP BY时,我的一些CASE语句中的数据似乎丢失了吗?

我知道答案必须简单,但我似乎无法解决它。任何帮助表示赞赏。非常感谢提前。

SELECT Blm.Ags, Blmths.Id, Ie.Full_Name, Blmths.Month,
       CASE month
       WHEN "2012-07-01" THEN 1
           ELSE NULL
       END AS Position_7,
       CASE month
       WHEN "2012-08-01" THEN Date_Format(Blmths.Month, '%Y_%b')
           ELSE NULL
       END AS Position_8,
       CASE Blmths.Month
       WHEN "2012-09-01" THEN Date_Format(Blmths.Month, '%Y_%b')
           ELSE NULL
       END AS Position_9,
       CASE month
       WHEN "2012-10-01" THEN Date_Format(Blmths.Month, '%Y_%b')
           ELSE NULL
       END AS Position_10,
       CASE Blmths.Month
       WHEN "2012-11-01" THEN Date_Format(Blmths.Month, '%Y_%b')
           ELSE NULL
       END AS Position_11,
       CASE Blmths.Month
       WHEN "2012-12-01" THEN Date_Format(Blmths.Month, '%Y_%b')
           ELSE NULL
       END AS Position_12,
       CASE month
       WHEN '2013-01-01' THEN Date_Format(Blmths.Month, '%Y_%b')
           ELSE NULL
       END AS Position_1,
       CASE month
       WHEN "2013-02-01" THEN Date_Format(Blmths.Month, '%Y_%b')
           ELSE NULL
       END AS Position_2,
       CASE month
       WHEN "2012-03-01" THEN Date_Format(Blmths.Month, '%Y_%b')
           ELSE NULL
       END AS Position_3,
       CASE Blmths.Month
       WHEN "2013-04-01" THEN Date_Format(Blmths.Month, '%Y_%b')
           ELSE NULL
       END AS Position_4,
       CASE Blmths.Month
       WHEN "2013-05-01" THEN Date_Format(Blmths.Month, '%Y_%b')
           ELSE NULL
       END AS Position_5,
       CASE Blmths.Month
       WHEN "2013-06-01" THEN Date_Format(Blmths.Month, '%Y_%b')
           ELSE NULL
       END AS Position_6
  FROM Bogus_Leadership_Months AS Blmths, Injection_Employees AS Ie,
Bogus_Leadership_Members AS Blm
  WHERE Blmths.Id = Blm.Id
    AND Ie.Ags = Blm.Ags
    AND Ie.Centre_Id = '666'
  GROUP BY Blm.Ags

2 个答案:

答案 0 :(得分:1)

你需要更加具体。这是什么数据库?它以什么方式不起作用?是否发生错误或未返回您需要的结果? “表格中我需要的数据不是全部意味着什么?”

如果每个人有多条记录,您希望汇总数据,会怎样?

我猜你可能想要这些内容(缩短):

SELECT Blm.Ags, Blmths.Id, Ie.Full_Name,
   SUM(CASE Blmths.month
   WHEN "2012-07-01" THEN 1
       ELSE NULL
   END) AS Position_7
FROM Bogus_Leadership_Months AS Blmths, Injection_Employees AS Ie,
Bogus_Leadership_Members AS Blm
  WHERE Blmths.Id = Blm.Id
    AND Ie.Ags = Blm.Ags
    AND Ie.Centre_Id = '666'
  GROUP BY Blm.Ags, Blmths.Id, Ie.Full_Name

这可以按月计算该人的数量。

我看不到您所追踪的数据示例 - 您可以重新发布吗?

答案 1 :(得分:0)

 SELECT max(Blmths.Id), Ie.Full_Name, Blmths.Month,Blm.Ags FROM 
    (SELECT Blm.Ags,Blmths.Id, Ie.Full_Name, Blmths.Month 
    CASE month
           WHEN "2012-07-01" THEN 1
               ELSE NULL
           END AS Position_7,
           CASE month
           WHEN "2012-08-01" THEN Date_Format(Blmths.Month, '%Y_%b')
               ELSE NULL
           END AS Position_8,
           CASE Blmths.Month
           WHEN "2012-09-01" THEN Date_Format(Blmths.Month, '%Y_%b')
               ELSE NULL
           END AS Position_9,
           CASE month
           WHEN "2012-10-01" THEN Date_Format(Blmths.Month, '%Y_%b')
               ELSE NULL
           END AS Position_10,
           CASE Blmths.Month
           WHEN "2012-11-01" THEN Date_Format(Blmths.Month, '%Y_%b')
               ELSE NULL
           END AS Position_11,
           CASE Blmths.Month
           WHEN "2012-12-01" THEN Date_Format(Blmths.Month, '%Y_%b')
               ELSE NULL
           END AS Position_12,
           CASE month
           WHEN '2013-01-01' THEN Date_Format(Blmths.Month, '%Y_%b')
               ELSE NULL
           END AS Position_1,
           CASE month
           WHEN "2013-02-01" THEN Date_Format(Blmths.Month, '%Y_%b')
               ELSE NULL
           END AS Position_2,
           CASE month
           WHEN "2012-03-01" THEN Date_Format(Blmths.Month, '%Y_%b')
               ELSE NULL
           END AS Position_3,
           CASE Blmths.Month
           WHEN "2013-04-01" THEN Date_Format(Blmths.Month, '%Y_%b')
               ELSE NULL
           END AS Position_4,
           CASE Blmths.Month
           WHEN "2013-05-01" THEN Date_Format(Blmths.Month, '%Y_%b')
               ELSE NULL
           END AS Position_5,
           CASE Blmths.Month
           WHEN "2013-06-01" THEN Date_Format(Blmths.Month, '%Y_%b')
               ELSE NULL
           END AS Position_6
      FROM Bogus_Leadership_Months AS Blmths, Injection_Employees AS Ie,
    Bogus_Leadership_Members AS Blm
      WHERE Blmths.Id = Blm.Id
        AND Ie.Ags = Blm.Ags
        AND Ie.Centre_Id = '666') AS GRPBYCOL 
   GROUP BY  Ie.Full_Name, Blmths.Month,Blm.Ags,GRPBYCOL