Transact SQL - 没有GROUP BY可以使用SUM()

时间:2018-03-28 03:31:44

标签: sql-server tsql group-by grouping

我有以下代码(见下文)。 是否有可能重新编写它,以避免使用

分组依据声明

请参阅以下内容:

DECLARE @MonthBOP date = '07-01-2017'
DECLARE @MonthEOP date = '02-28-2018'
DECLARE @GLRegionsWHID int = -1

SELECT
 e.[GLRegion]
,e.[GLRegionWHID]
,e.[GLProgram]
,e.[GLProgramWHID] 
,e.[DOLStatus] 
,e.[DOLStatusWHID]
,e.[TerminationType]
,e.[TerminationReason]

 ,SUM(IIF((e.[DateHired] <= @MonthBOP) AND (e.[DateTerminated] >= @MonthBOP), 1, 0)) AS [CountEmployeesBOP]
,SUM(IIF((e.[DateHired] <= @MonthEOP) AND (e.[DateTerminated] >= @MonthEOP), 1, 0)) AS [CountEmployeesEOP]
,SUM(IIF(e.[DateTerminated] BETWEEN @MonthBOP AND @MonthEOP, 1, 0)) AS [CountTerminations]

FROM 
    [Reports].[vPaycomEmployee] e

WHERE
    ((e.[DateHired] <= @MonthEOP) OR (e.[DateTerminated] > @MonthBOP)) 
AND ((e.[GLRegionWHID] IN (@GLRegionsWHID)) OR (-1 IN (@GLRegionsWHID)))


GROUP BY
 e.[GLRegion]
,e.[GLRegionWHID]
,e.[GLProgram]
,e.[GLProgramWHID] 
,e.[DOLStatus] 
,e.[DOLStatusWHID]
,e.[TerminationType]
,e.[TerminationReason]

HAVING
(SUM(IIF((e.[DateHired] <= @MonthBOP) AND (e.[DateTerminated] >= @MonthBOP), 
     1, 0)) > 0)

我需要避免分组依据,因为它会影响结果计算

1 个答案:

答案 0 :(得分:2)

有几种不同的方法:你可以使用子查询...

这是非常松散的,因为我不完全确定你的意思,“我需要避免Group By,因为它会影响最终的计算”(影响他们如何?你希望得到什么结果?),但是如下所示可能有效(此示例假设您只想列出每条记录,但是按GLRegion计算记录,并且还匹配其余的WHERE标准):

DECLARE @MonthBOP date = '07-01-2017'
DECLARE @MonthEOP date = '02-28-2018'
DECLARE @GLRegionsWHID int = -1

SELECT
     e.[GLRegion]
    ,e.[GLRegionWHID]
    ,e.[GLProgram]
    ,e.[GLProgramWHID] 
    ,e.[DOLStatus] 
    ,e.[DOLStatusWHID]
    ,e.[TerminationType]
    ,e.[TerminationReason]
    ,(SELECT COUNT(*) 
        FROM [Reports].[vPaycomEmployee] e1 
        WHERE (e1.[DateHired] <= @MonthBOP) 
            AND (e1.[DateTerminated] >= @MonthBOP) 
            AND (e1.[GLRegion] = e.[GLRegion])
            AND ((e1.[GLRegionWHID] IN (@GLRegionsWHID)) OR (-1 IN (@GLRegionsWHID)))
        ) AS [CountEmployeesBOP]
    ,(SELECT COUNT(*) 
        FROM [Reports].[vPaycomEmployee] e1 
        WHERE (e1.[DateHired] <= @MonthEOP) 
            AND (e1.[DateTerminated] >= @MonthEOP) 
            AND (e1.[GLRegion] = e.[GLRegion])
            AND ((e1.[GLRegionWHID] IN (@GLRegionsWHID)) OR (-1 IN (@GLRegionsWHID)))
        ) AS [CountEmployeesEOP]
    ,(SELECT COUNT(*) 
        FROM [Reports].[vPaycomEmployee] e1 
        WHERE (e1.[DateTerminated] BETWEEN @MonthBOP AND @MonthEOP) 
            AND (e1.[GLRegion] = e.[GLRegion])
            AND ((e1.[GLRegionWHID] IN (@GLRegionsWHID)) OR (-1 IN (@GLRegionsWHID)))
        ) AS [CountTerminations]
FROM [Reports].[vPaycomEmployee] e
WHERE
    ((e.[DateHired] <= @MonthEOP) OR (e.[DateTerminated] > @MonthBOP)) 
    AND ((e.[GLRegionWHID] IN (@GLRegionsWHID)) OR (-1 IN (@GLRegionsWHID)))
    AND (SELECT COUNT(*) 
        FROM [Reports].[vPaycomEmployee] e1 
        WHERE (e1.[DateHired] <= @MonthBOP) 
            AND (e1.[DateTerminated] >= @MonthBOP)
        ) > 0

(注意我用COUNT替换了SUM,因为这似乎是你想要的,而且它更简单。)