0

I've table like this

enter image description here

I need to get a result like this

RT       Team     PI     Committed   Done-Partial   Done-Full
----------------------------------------------------------------
ART1     Team1     10       5           1               3
ART2     Team2     7        5           4               1
-----------------------------------------------------------------------

The way I tried is as follows

;with RecentPI as(
select * from  (
select rt,Team,pi,[Finish Date],DENSE_RANK() over( partition by rt order by [Finish Date] desc) PIRank from Schedule_Manual S inner join TFS_ARTs_Teams T on T.ART=S.RT

group by RT,Team,PI,[Finish Date]
)tbl

 where PIRank=1
 )

 select * from (select  Obj.RT,Obj.[TFS Team], Obj.Type,Obj.PI,[PI Status] from Objectives Obj inner join RecentPI RP on RP.RT=Obj.RT and RP.Team=Obj.[TFS Team] and RP.PI=Obj.PI) as query 
 PIVOT (count(type) for [Type] in ([Committed])) p1
  PIVOT (Count([PI Status]) for [pi status] in ([Done-Partial],[Done-Full])) p2

But it doesnt seems to be correct and also Im not getting full columns in the query. Sorry Im very beginner with SQL Pivot

3

1 Answer 1

2

You can Use this simple Query...your problem will be solved....

select RT,Team,[PI],
 sum(case when [Type] = 'Committed' then 1 else 0 end) AS 'Committed',
  sum(case when PIStatus = 'Done-Partial' then 1 else 0 end) AS 'Done-Partial',
  sum(case when PIStatus = 'Done-Full' then 1 else 0 end) AS 'Done-Full'
from tbl_Pivot
group by RT,Team,[PI]

Output:-

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.