count()中的错误数字

时间:2014-03-20 10:38:57

标签: sql sql-server count

我已经发布了两个关于我的问题的问题,而我几乎就在那里。

这是我以前的帖子:

  1. use array/variable in sql-query
  2. extend SQL result - add row if not exist
  3. 通过一个例子更容易解释它,所以这是一个虚构的例子:

    我的表看起来像这样(我只显示我需要的行,此表包含+ 100k行)

    status  PGID    nvarchar5               nvarchar10  CatId   tp_ID   isActive
    IT      NULL    Information technology  NULL        1       1       1
    HR      NULL    Human Recource          NULL        1       2       1
    FIN     NULL    Finance                 NULL        1       3       1
    New        1    NULL                    1354        2       10001   1
    New        1    NULL                    464         2       10002   1
    New        1    NULL                    13465       2       10003   1
    Active     1    NULL                    79846       2       10004   1
    Deleted    1    NULL                    132465      2       10005   1
    New        2    NULL                    79847       2       10006   1
    New        2    NULL                    341         2       10007   1
    Deleted    2    NULL                    465         2       10008   1
    Deleted    2    NULL                    132         2       10009   1
    Deleted    2    NULL                    465         2       10010   1
    Deleted    2    NULL                    1           2       10011   1
    New        3    NULL                    465         2       10012   1
    New        3    NULL                    1465        2       10013   1
    New        3    NULL                    132         2       10014   1
    NULL    NULL    NULL                    NULL        3       20136   1
    NULL    NULL    NULL                    NULL        4       22165   1
    NULL    NULL    NULL                    NULL        3       24566   1
    

    我使用此查询:

    SELECT stat.status
          ,d.[PGID] as PGID
          ,pg.[nvarchar5] as PGName
          ,COUNT(d.[nvarchar10])
    FROM 
        (select distinct [status]
         from [content].[dbo].[USRData] 
         where  CatId = '1') stat cross Join
          [content].[dbo].[USRData]  d INNER JOIN
          [content].[dbo].[USRData]  pg on d.[PGID] = pg.[tp_ID]
    where d.CatId = '1'
          AND d.[nvarchar10] is not null
          AND stat.status is not null
          AND d.[isActive] = 1
          AND pg.[CatId] = '2'
    group by stat.status, 
             pg.[nvarchar5], 
             d.[PGID]
    order by PGName
    

    然后我得到了这个结果:

    status  PGID    nvarchar5               total
    new     1       Information technology  5
    active  1       Information technology  5
    deleted 1       Information technology  5
    new     2       Human Recource          6
    active  2       Human Recource          6
    deleted 2       Human Recource          6
    new     3       Finance                 3
    active  3       Finance                 3
    deleted 3       Finance                 3
    

    但我希望得到这样的结果:

    status  PGID    nvarchar5               total
    new     1       Information technology  3
    active  1       Information technology  1
    deleted 1       Information technology  1
    new     2       Human Recource          2
    active  2       Human Recource          0
    deleted 2       Human Recource          4
    new     3       Finance                 3
    active  3       Finance                 0
    deleted 3       Finance                 0
    

    我不是SQL的专业人士。也许这是愚蠢的事情,但我被卡住了...欢迎任何帮助! :)

1 个答案:

答案 0 :(得分:0)

我注意到,在您正在寻找的结果中,total列中的行数少于给定的结果,可能是由于重复行,我猜CROSS JOIN负责为此,你为什么不在INNER JOIN

上尝试status
(select distinct [status]
     from [content].[dbo].[USRData] 
      where  CatId = '1') stat 
     INNER JOIN [content].[dbo].[USRData]  d on stat.[status]=d.[status]
     INNER JOIN [content].[dbo].[USRData]  pg on d.[PGID] = pg.[tp_ID]