1

背景:我正在尝试显示一个带有标题的表格: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
4

2 回答 2

2

你需要更具体。这是针对什么数据库?它以什么方式不起作用?是发生错误还是没有返回您需要的结果?'我需要的表中的数据并非全部存在' 是什么意思?

如果每人有多个记录会发生什么,您希望如何聚合数据?

我猜你可能想要这些方面的东西(缩短):

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

这按月给出了那个人的统计数据。

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

于 2012-11-05T23:56:11.460 回答
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 
于 2012-11-05T09:19:35.897 回答