0

我有以下查询 -

  select TeamProjectSK,     
  sum(case when d.System_State = 'Proposed' then 1 else 0 end) as New,
  sum(case when d.System_State = 'Active' then 1 else 0 end) as Active,
  sum(case when d.System_State = 'Resolved' then 1 else 0 end) as Resolved,
  sum(case when d.System_State = 'Closed' then 1 else 0 end) as Closed     
  from
  (
   select w1.System_Id, w1.TeamProjectSK, w1.System_State, w1.System_Rev,
   row_number() over(partition by w1.System_Id, w1.TeamProjectSK, w1.System_State order by w1.System_Rev desc) rn
   from dbo.DimWorkItem w1       
  ) d
   where rn = 1
   and d.System_Rev = (select max(w2.System_Rev) from dbo.DimWorkItem w2 where w2.System_Id =   d.System_Id)
   group by TeamProjectSK
   order by TeamProjectSK desc;

查询的结果看起来像 -

TeamProjectSK   New Active  Resolved    Closed
157           14    115     1           169
156           0     0       0           0
155           0     0       0           0
154           0     0       0           0
151           2     1       0           1

现在我想要一列“Total_Count”,它应该是 New+Active+Resolved+Closed 的总和。输出应该看起来像 -

TeamProjectSK  Total_Count  New Active  Resolved    Closed
157      289            14  115 1   169
156      0              0   0   0   0
155      0              0   0   0   0
154      0              0   0   0   0
151      4              2   1   0   1

我尝试了以下查询,但无济于事。请查看相同的内容。

    select TeamProjectSK, 
    New + Active + Resolved + Closed as Total_Count,    
    sum(case when d.System_State = 'Proposed' then 1 else 0 end) as New,
    sum(case when d.System_State = 'Active' then 1 else 0 end) as Active,
    sum(case when d.System_State = 'Resolved' then 1 else 0 end) as Resolved,
    sum(case when d.System_State = 'Closed' then 1 else 0 end) as Closed     
    from
    (
    select w1.System_Id, w1.TeamProjectSK, w1.System_State, w1.System_Rev,
     row_number() over(partition by w1.System_Id, w1.TeamProjectSK, w1.System_State order by w1.System_Rev desc) rn
     from dbo.DimWorkItem w1   

    ) d
     where rn = 1
     and d.System_Rev = (select max(w2.System_Rev) from dbo.DimWorkItem w2 where w2.System_Id = d.System_Id)
     group by TeamProjectSK
     order by TeamProjectSK desc;

问候。

4

1 回答 1

1
select TeamProjectSK, New + Active + Resolved + Closed as Total_Count, New Active, Resolved, Closed
from
(
select TeamProjectSK,     
  sum(case when d.System_State = 'Proposed' then 1 else 0 end) as New,
  sum(case when d.System_State = 'Active' then 1 else 0 end) as Active,
  sum(case when d.System_State = 'Resolved' then 1 else 0 end) as Resolved,
  sum(case when d.System_State = 'Closed' then 1 else 0 end) as Closed     
  from
  (
   select w1.System_Id, w1.TeamProjectSK, w1.System_State, w1.System_Rev,
   row_number() over(partition by w1.System_Id, w1.TeamProjectSK, w1.System_State order by w1.System_Rev desc) rn
   from dbo.DimWorkItem w1       
  ) d
   where rn = 1
   and d.System_Rev = (select max(w2.System_Rev) from dbo.DimWorkItem w2 where w2.System_Id =   d.System_Id)
   group by TeamProjectSK
) a
   order by TeamProjectSK desc
于 2013-06-05T18:49:17.180 回答