如何在Group by Clause中使用SQL Case?

时间:2016-06-28 10:55:00

标签: java sql sql-server

我在select和in group by子句中使用SQL Case,而且我在JAVA工作。每当我执行我的java程序时,它会说:

  

专栏' dbo.JOHN_Dashboard.Log_Date'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

我的查询是:

SELECT EP.Site_Code AS [Site_Code], DB.[Site] AS [Site], DB.[Utility] AS [Utility], 
        CASE ? WHEN 'Raw' THEN dateadd(mi,datediff(mi,0,DB.[log_date]),0) 
        WHEN 'Hour'  THEN dateadd(hh,datediff(hh,0,DB.[log_date]),0)
        WHEN 'Day'   THEN dateadd(dd,datediff(dd,0,DB.[log_date]),0)
        WHEN 'Week'  THEN dateadd(wk,datediff(wk,0,DB.[log_date]),0)
        WHEN 'Month' THEN dateadd(mm,datediff(mm,0,DB.[log_date]),0)
        WHEN 'Year'  THEN dateadd(yy,datediff(yy,0,DB.[log_date]),0)                                                     
        ELSE DB.[log_date]
        END AS [log_date],
        SUM(CASE WHEN DB.[value] >= 0 THEN DB.[value] ELSE 0 END) AS [value],
        SUM(CASE WHEN DB.[Cost] >=0 THEN DB.[cost] ELSE 0 END) AS [Cost],
        SUM(CASE WHEN DB.[CO2] >=0 THEN DB.[CO2] ELSE 0 END) AS [CO],
        MT.[Meter_type_name] AS [Meter Type],
        MN.[Meter_Name] AS [Meter Name],
        U.[Unit_Name] AS [Units],
        EP.EnergyPoint_ID AS [Meter_ID],
        EP.Parent_ID AS [Parent],
        EP.Meter_Description AS [Meter_Description] 
FROM [dbo].[JOHN_Dashboard] DB
INNER JOIN [dbo].[EnergyPoints] EP ON DB.[EnergyPoint_ID] = EP.[EnergyPoint_ID] 
INNER JOIN [dbo].[Meter_Types] MT ON MT.[Meter_Type_ID] = EP.[Meter_Type_ID]
INNER JOIN [dbo].[Meter_Names] MN ON MN.[Meter_Name_ID] = EP.[Meter_Name_ID]
INNER JOIN [dbo].[Units] U ON U.[Unit_ID] = EP.[Unit_id]
WHERE [log_date] >= ? AND [Log_Date] < DATEADD(DAY, 1, ?)
AND ( ? IS NULL OR EP.Energypoint_ID = ?)
GROUP BY EP.Site_Code, DB.[Site], DB.[Utility], MT.[Meter_type_name],           
        MN.[Meter_Name], U.[Unit_Name], EP.[EnergyPoint_ID],
        EP.[Parent_ID], EP.[Meter_Description],
        CASE ? WHEN 'Raw' THEN dateadd(mi,datediff(mi,0,DB.[log_date]),0)
        WHEN 'Hour'  THEN dateadd(hh,datediff(hh,0,DB.[log_date]),0)
        WHEN 'Day'   THEN dateadd(dd,datediff(dd,0,DB.[log_date]),0)
        WHEN 'Week'  THEN dateadd(wk,datediff(wk,0,DB.[log_date]),0)
        WHEN 'Month' THEN dateadd(mm,datediff(mm,0,DB.[log_date]),0)
        WHEN 'Year'  THEN dateadd(yy,datediff(yy,0,DB.[log_date]),0)
        ELSE DB.[log_date] END ;

我传递的参数是:

  1. &#39;周&#39;
  2. &#39; 2016年5月16日&#39;
  3. &#39; 2016年5月22日&#39;
  4. 6044
  5. 6044
  6. &#39;周&#39;
  7. 注意:此查询在SQL Management Studio中运行时没有错误。

1 个答案:

答案 0 :(得分:1)

这里要求的是在分组之前使用子查询重新编写代码的版本。由于我没有你的数据库,我无法保证我的一切都完全正确,但试一试。

我建议当你的group by具有将在select中重复的复杂逻辑时总是使用子查询。有些人可能会放弃第二个标准,并且只要每个小组都有复杂的逻辑就会说。

SELECT  sub.Site_Code, sub.[Site], sub.[Utility], sub.[Meter Type],
        sub.[log_date],
        SUM(sub.[value]) as [value],
        SUM(sub.[Cost]) as [cost],
        SUM(sub.[CO]) as [CO],
        sub.[Meter Name], sub.[Units], sub.[Meter_ID],
        sub.[Parent], sub.[Meter_Description]
FROM    (
        SELECT EP.Site_Code AS [Site_Code], DB.[Site] AS [Site], DB.[Utility] AS [Utility], 
                CASE ? WHEN 'Raw' THEN dateadd(mi,datediff(mi,0,DB.[log_date]),0) 
                WHEN 'Hour'  THEN dateadd(hh,datediff(hh,0,DB.[log_date]),0)
                WHEN 'Day'   THEN dateadd(dd,datediff(dd,0,DB.[log_date]),0)
                WHEN 'Week'  THEN dateadd(wk,datediff(wk,0,DB.[log_date]),0)
                WHEN 'Month' THEN dateadd(mm,datediff(mm,0,DB.[log_date]),0)
                WHEN 'Year'  THEN dateadd(yy,datediff(yy,0,DB.[log_date]),0)                                                     
                ELSE DB.[log_date]
                END AS [log_date],
                CASE WHEN DB.[value] >= 0 THEN DB.[value] ELSE 0 END AS [value],
                CASE WHEN DB.[Cost] >=0 THEN DB.[cost] ELSE 0 END AS [Cost],
                CASE WHEN DB.[CO2] >=0 THEN DB.[CO2] ELSE 0 END AS [CO],
                MT.[Meter_type_name] AS [Meter Type],
                MN.[Meter_Name] AS [Meter Name],
                U.[Unit_Name] AS [Units],
                EP.EnergyPoint_ID AS [Meter_ID],
                EP.Parent_ID AS [Parent],
                EP.Meter_Description AS [Meter_Description] 
        FROM [dbo].[JOHN_Dashboard] DB
        INNER JOIN [dbo].[EnergyPoints] EP ON DB.[EnergyPoint_ID] = EP.[EnergyPoint_ID] 
        INNER JOIN [dbo].[Meter_Types] MT ON MT.[Meter_Type_ID] = EP.[Meter_Type_ID]
        INNER JOIN [dbo].[Meter_Names] MN ON MN.[Meter_Name_ID] = EP.[Meter_Name_ID]
        INNER JOIN [dbo].[Units] U ON U.[Unit_ID] = EP.[Unit_id]
        WHERE [log_date] >= ? AND [Log_Date] < DATEADD(DAY, 1, ?)
        AND ( ? IS NULL OR EP.Energypoint_ID = ?)
        ) sub
GROUP BY sub.Site_Code, sub.[Site], sub.[Utility], sub.[Meter Type],           
        sub.[Meter Name], sub.[Units], sub.[Meter_ID],
        sub.[Parent], sub.[Meter_Description], sub.[log_date];