HAVING MAX(值)在SQL Server查询中不起作用

时间:2016-03-08 05:51:19

标签: sql-server sql-server-2008

我有一个表ExchangeUserMailbox,其中包含

  • OrganisationID(GUID)
  • MailboxLocationName(STRING)
  • MailboxCode(STRING)
  • DayofMon(INT)
  • UPN(STRING)

DayofMon的可能值为“15”和“30”(我每月的每两天都会有数据进入此表格,例如: - 第14和第30,或第15和第31)< / p>

我正在运行以下SQL脚本

SELECT 
    [OrganisationID] 
    ,[MailboxLocationName] AS Location 
    ,MailboxCode as LocationCode 
    ,DayofMon 
    ,COUNT(DISTINCT [UPN]) AS [Count]
FROM 
    [ExchangeUserMailbox]
GROUP BY 
    [OrganisationID], [MailboxLocationName], DayofMon, MailboxCode
HAVING  
    DayofMon = MAX(DayofMon) 

我的目的是获取最大值为DayofMon的所有记录,并汇总UPN的数量。

但是在输出窗口中我得到了

OrganisationID                              Location        LocationCode  DayofMon Count

2EC8650F-8902-4CFB-BE0E-A218982EDEEC        Diffraction         DIF          1       3
2EC8650F-8902-4CFB-BE0E-A218982EDEEC        Diffraction         DIF          30      10

正如您所见,汇总了dayofmon= 1dayofmon=30。但我只想汇总dayofmon=30

我在这里做错了什么?

4 个答案:

答案 0 :(得分:0)

尝试使用Day_Mon&gt;的hard_code ... 15 ... 我更多的是甲骨文家伙,所以根据我的经验,你不能在拥有的结果中加入一个Wi​​ndowed函数。

答案 1 :(得分:0)

试试这个:选择使用最大功能。

SELECT 
    [OrganisationID] 
    ,[MailboxLocationName] AS Location 
    ,MailboxCode as LocationCode 
    ,Max(DayofMon) as [DayofMon]
    ,COUNT(DISTINCT [UPN]) AS [Count]
FROM 
    [ExchangeUserMailbox]
GROUP BY 
    [OrganisationID], [MailboxLocationName], MailboxCode

答案 2 :(得分:0)

HAVING子句专为通过聚合计算进行过滤而设计,因此它在GROUP BY完成后执行,因此它可以访问聚合值。您需要的是在执行分组之前需要过滤,因此having子句没有帮助。

你可以只过滤DayOfMon所在的数据(28,29,30或31)

SELECT
        OrganisationID
      , MailboxLocationName AS Location
      , MailboxCode         AS LocationCode
      , 30                  AS DayofMon 
      , COUNT(DISTINCT UPN) AS [Count]
FROM [ExchangeUserMailbox]
WHERE DayofMon IN (28,29,30,31)
GROUP BY
        OrganisationID
      , MailboxLocationName
      , MailboxCode

或者你可以像这样使用ROW_NUMBER():

SELECT
        OrganisationID
      , Location
      , LocationCode
      , 30                  AS DayofMon 
      , COUNT(DISTINCT UPN) AS [Count]
FROM (
        SELECT
                OrganisationID
              , MailboxLocationName   AS Location
              , MailboxCode           AS LocationCode
              , UPN
              , ROW_NUMBER() OVER (PARTITION BY OrganisationID
                                                , MailboxLocationName
                                                , MailboxCode
                                   ORDER BY DayofMon DESC) AS rn
        FROM [ExchangeUserMailbox]
        ) AS d
WHERE d.rn = 1
GROUP BY
        OrganisationID
      , Location
      , LocationCode
;

对于每个“分区”

,每个行具有最大DayOfMon的数字为1

答案 3 :(得分:0)

最直接的方法是使用子查询:

SELECT 
    eum.[OrganisationID] 
    ,eum.[MailboxLocationName] AS Location 
    ,eum.MailboxCode as LocationCode 
    ,eum.DayofMon 
    ,COUNT(DISTINCT [eum.UPN]) AS [Count]
FROM 
    [ExchangeUserMailbox] AS eum
WHERE eum.DayofMon =
      (SELECT MAX(eum2.DayofMon)
       FROM   [ExchangeUserMailbox] AS eum2
       WHERE  eum2.[OrganisationID] = eum.[OrganisationID]
       AND    eum2.[MailboxLocationName] = eum.[MailboxLocationName]
       AND    eum2.MailboxCode = eum.MailboxCode
      )
GROUP BY 
    eum.[OrganisationID], eum.[MailboxLocationName], eum.DayofMon, eum.MailboxCode

&#34;硬编码&#34;解决方案有效,但更脆弱。这样,您一定会始终获得具有最高DayofMon值的记录(即使它是一个意外的值),并且您获得DayofMon的实际值而不是硬编码值。