group by - 计数行,如果不存在,则添加0值的行

时间:2014-03-21 10:17:36

标签: sql

我知道我已经问了一个类似的问题,但是我从零开始......没有给出任何疑问我试过,所以我没有影响你。

如果这是我的表:

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
NULL    NULL    NULL                    NULL        10      24566   1

如果我想要这样的结果,那么查询应该是什么:

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

或者这不可能吗?

编辑: 如果你想看看我尝试过的内容:wrong number in count()

更新: 我如何计算总数: enter image description here

3 个答案:

答案 0 :(得分:1)

我不打算深入了解数据的外观,列名等等,并假设它是“我必须与之合作的情况”。所以给出数据试试

SELECT t.Status, t.Department_ID, t.Department, COALESCE(s.Total, 0) AS Total
FROM (
    SELECT nvarchar5 AS Department, tp_ID AS Department_ID, Status
    FROM My_Table, 
         (SELECT 'new' AS Status
          UNION ALL
          SELECT 'active' AS Status
          UNION ALL
          SELECT 'deleted' AS Status ) AS m
    WHERE tp_ID IN (1,2,3)
) AS t
LEFT JOIN (
    SELECT status AS Status, PGID AS Department_ID, COUNT(1) AS Total
    FROM My_Table
    WHERE PGID IS NOT NULL
    GROUP BY Status, PGID
) AS s 
ON t.Status = s.Status
AND t.Department_ID = s.Department_ID 

答案 1 :(得分:0)

这不是答案,但我认为这也可以帮到你:

declare @t as table (
num varchar(50)  null,
t varchar(50) null,
ord int not null,
pgid int null
)

insert into @t values ('IT',  'Info', 1 ,   null)
insert into @t values ('HR' , 'Human', 2 ,   null)
insert into @t values ('FIN', 'Finance', 3 ,  null )
insert into @t values ('New'    , NULL, 10001,  1 )
insert into @t values ('New'    , NULL, 10002,  1 )
insert into @t values ('New'    , NULL, 10003,  1 )
insert into @t values ('Active' , NULL, 10004,  1 )
insert into @t values ('Deleted', NULL, 10005,  1 )
insert into @t values ('New'    , NULL, 10006,  2 )
insert into @t values ('New'    , NULL, 10007,  2 )
insert into @t values ('Deleted', NULL, 10008,  2 )
insert into @t values ('Deleted', NULL, 10009,  2 )
insert into @t values ('Deleted', NULL, 10010,  2 )
insert into @t values ('Deleted', NULL, 10011,  2 )
insert into @t values ('New'    , NULL, 10012,  3 )
insert into @t values ('New'    , NULL, 10013,  3 )
insert into @t values ('New'    , NULL, 10014,  3 )


select num, pgid, case when pgid = 1 then 'Information technology'
when pgid = 2 then 'Human Recource'
else 'Finance' end,count(num)
from @t
where num in (
'New'    ,
'Active' ,
'Deleted')
group by num, pgid


SELECT case when pgid = 1 then 'Information technology'
when pgid = 2 then 'Human Recource'
else 'Finance' end,
[New]    ,
[Active] ,
[Deleted]
FROM
(SELECT num, pgid 
    FROM @t
    where num in (
'New'    ,
'Active' ,
'Deleted')) AS SourceTable
PIVOT
(
count(num)
FOR num IN ([New],
[Active] ,
[Deleted])
) AS PivotTable;

答案 2 :(得分:0)

//try this query it will help you to solve this problem

select t.satus,t.PGID,u.nvarchar5,t.total from 
    (select status,PGID,count(*) as total from USRData group by status,PGID where PGID is not null) t
    inner join USRData u on(t.PGID=u.tp_ID) where u.PGID is null and status is not null;