Summary Query in SQL Server - sql-server

I have following data
ID Status
5bcb5961-20e-41b7-8af0-87ffe127830c 1
140c7f17-dba5-4bc8-1fb-062d1325c399 5
5bcb5961-20e-41b7-8af0-87ffe127830c 5
5bcb5961-20e-41b7-8af0-87ffe127830c 0
I want an output in summary form.
ID Status_1 Status_5 Status_0
5bcb5961-d20e-41b7-8af0-87ffe127830c 1 1 1
140c7f17-dba5-4bc8-b1fb-062d1325c399 0 1 0
Can I get query for this output?

SELECT
ID
, Status_1 = COUNT(CASE WHEN [Status] = 1 THEN 1 END)
, Status_5 = COUNT(CASE WHEN [Status] = 5 THEN 1 END)
, Status_0 = COUNT(CASE WHEN [Status] = 0 THEN 1 END)
FROM tbl
GROUP BY ID

Related

Convert Rows to columns using 'Pivot' Top 5

I want a pivot table from a SQL table with top 3 records only with filter based on user.
ID PrOjID User piD
------- ---- ------ ------
1 Prj1 ABC 1
2 Prj2 XYZ 2
3 Prj3 ABC 3
4 Prj4 ABC 4
5 Prj5 PQR 5
6 Prj1 XYZ 1
7 Prj3 PQR 3
8 Prj8 ABC 8
9 Prj1 PQR 1
10 Prj10 ABC 10
11 Prj11 ABC 11
12 Prj12 ABC 12
Result I want when user is ABC
ID1 PROJID1 piD1 ID2 PROJID2 piD2 ID3 PROJID3 piD3
1 prj1 1 3 PRJ3 3 4 prj4 4
If USER IS xyz
ID1 PROJID1 piD1 ID2 PROJID2 piD2 ID3 PROJID3 piD3
2 prj2 2 1 PRJ1 1 null null null
We can handle this using a pivot with the help of ROW_NUMBER:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY [User] ORDER BY piD) rn
FROM yourTable
)
SELECT
[User],
MAX(CASE WHEN rn = 1 THEN ID END) AS ID1,
MAX(CASE WHEN rn = 1 THEN PrOjID END) AS PROJID1,
MAX(CASE WHEN rn = 1 THEN piD END) AS piD1,
MAX(CASE WHEN rn = 2 THEN ID END) AS ID2,
MAX(CASE WHEN rn = 2 THEN PrOjID END) AS PROJID2,
MAX(CASE WHEN rn = 2 THEN piD END) AS piD2,
MAX(CASE WHEN rn = 3 THEN ID END) AS ID3,
MAX(CASE WHEN rn = 3 THEN PrOjID END) AS PROJID3,
MAX(CASE WHEN rn = 3 THEN piD END) AS piD3
FROM cte
WHERE
[User] = 'ABC'
GROUP BY
[User];
Demo

T-SQL Pivot multiple columns duplicates results

I have a table with aggregated data that I would like to pivot. Here is a sample of the data:
Unit# EffectiveDay RequestCount ResponseCount
5 1 0 0
5 2 8 8
5 3 4 4
5 4 4 4
5 5 2 2
5 6 0 0
5 7 2 2
6 1 0 0
6 2 0 0
6 3 0 0
6 4 0 0
6 5 0 0
6 6 0 0
6 7 0 0
I can successfully pivot the RequestCount column, but when I add the ResponseCount column it's as if the rows increased exponentially. Instead of two rows (one for each unit#), I get 14.
select *
from (
select Unit#
,concat('Request', EffectiveDay) AS RequestDay
,RequestCount
,concat('Response', EffectiveDay) As ResponseDay
,ResponseCount
from #tmpLogPayConnexion
) as P
pivot (
sum(RequestCount)
for RequestDay in ([Request1], [Request2], [Request3], [Request4], [Request5], [Request6], [Request7])
) as pvtRequest
pivot (
sum(ResponseCount)
for ResponseDay in ([Response1], [Response2], [Response3], [Response4], [Response5], [Response6], [Response7])
) as pvtResponse
I know this is possible, but I am stumped.
Thank you.
This seems like a time for a crosstab, or conditional aggregation.
select [Unit#]
, Request1 = max(case when EffectiveDay = 1 then RequestCount end)
, Request2 = max(case when EffectiveDay = 2 then RequestCount end)
, Request3 = max(case when EffectiveDay = 3 then RequestCount end)
, Request4 = max(case when EffectiveDay = 4 then RequestCount end)
, Request5 = max(case when EffectiveDay = 5 then RequestCount end)
, Request6 = max(case when EffectiveDay = 6 then RequestCount end)
, Request7 = max(case when EffectiveDay = 7 then RequestCount end)
, Response1 = max(case when EffectiveDay = 1 then ResponseCount end)
, Response2 = max(case when EffectiveDay = 2 then ResponseCount end)
, Response3 = max(case when EffectiveDay = 3 then ResponseCount end)
, Response4 = max(case when EffectiveDay = 4 then ResponseCount end)
, Response5 = max(case when EffectiveDay = 5 then ResponseCount end)
, Response6 = max(case when EffectiveDay = 6 then ResponseCount end)
, Response7 = max(case when EffectiveDay = 7 then ResponseCount end)
from #tmpLogPayConnexion
group by [Unit#]
order by [Unit#]

Need to add a subquery in a CASE expression that is part of an aggregate function

I need to add the number of records in which the status is set to 'reopened'. But the 'reopened' status has several IDs.
This is the subquery that will Id the 'reopen' statuses:
SELECT (CASE WHEN s.sr_status_recid = 1 THEN 1 ELSE 0 END) AS Reopened
from v_rpt_service s
where vsrv.sr_status_recid in
(select distinct SR_Status_RecID from SR_Status where [Description] like '%Re-opened%'))
This is the main query that the above query needs to be a part:
SELECT DATEPART(WK, vsrv.date_entered) as WkNumber,
COUNT(vsrv.TicketNbr) AS OpenedIssues, --total ticket count
SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS ClosedIssues, --sum of tickets with closed_flag = 1
(SELECT SUM(CASE WHEN s.sr_status_recid = 1 THEN 1 ELSE 0 END)
from v_rpt_service s
where vsrv.sr_status_recid in
(select distinct SR_Status_RecID from SR_Status where [Description] like '%Re-opened%')) AS ReopenedIssues,
SUM(CASE WHEN vsrvy.Surveys_Completed = 1 THEN 1 ELSE 0 END) AS SurveysCompletedWithConnectWise, -- Surveys_Completed flag in view is 1
SUM(CASE WHEN Source = 'Portal' THEN 1 ELSE 0 END) AS IssueLoggedPortal,
SUM(CASE WHEN Source = 'Email Connector' THEN 1 ELSE 0 END) AS IssueLoggedEmai
FROM v_rpt_service vsrv LEFT OUTER JOIN v_rpt_SurveysByTicket vsrvy ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID
WHERE vsrv.company_name <> 'XYZ Test Company' AND vsrv.date_entered BETWEEN '01/01/2016' AND '10/07/2016'
GROUP BY DATEPART(WK, vsrv.date_entered)
ORDER BY WkNumber
How can I have a subquery that uses a CASE statement and the CASE statement is aggregated?
You can use CROSS APPLY
SELECT DATEPART(WK, vsrv.date_entered) as WkNumber,
COUNT(vsrv.TicketNbr) AS OpenedIssues, --total ticket count
SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS ClosedIssues, --sum of tickets with closed_flag = 1
SUM(CountReopen.YesNo) AS NumberOfReopen,
SUM(CASE WHEN vsrvy.Surveys_Completed = 1 THEN 1 ELSE 0 END) AS SurveysCompletedWithConnectWise, -- Surveys_Completed flag in view is 1
SUM(CASE WHEN Source = 'Portal' THEN 1 ELSE 0 END) AS IssueLoggedPortal,
SUM(CASE WHEN Source = 'Email Connector' THEN 1 ELSE 0 END) AS IssueLoggedEmail
FROM v_rpt_service vsrv
LEFT OUTER JOIN v_rpt_SurveysByTicket vsrvy
ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID
CROSS APPLY (
SELECT IIF(COUNT(*) > 0,1,0) YesNo
FROM SR_Status
where [Description] like '%Re-opened%'
AND SR_Status_ID = vsrv.sr_status_recid
) CountReopen(YesNo)
WHERE vsrv.company_name <> 'XYZ Test Company'
AND vsrv.date_entered BETWEEN '01/01/2016' AND '10/07/2016'
GROUP BY DATEPART(WK, vsrv.date_entered)
ORDER BY WkNumber

join multiple queries result in to seperate columns

hello everyone i have 3 queries it gives when i used union All its give me 3 rows i want to convert 3 rows into columns how can i do that please help me
here is my query
( select count(*) As TotalCount from Detail_User
where userkey = 172 )
--union
( select count(*) As ICount1 from Detail_User
where Parent_Name = 'A' and userkey = 172 )
--union
( select count(*) As ICount2 from Detail_User
where Parent_Name = 'B' and userkey = 172 )
its give me some thing like this
TotalCount
2
3
5
i want something like this
TotalCount ICount1 ICount2
2 3 5
Don't do a UNION, use a CASE WHEN in your SELECT like this
select
count(*) As TotalCount,
SUM(CASE WHEN Parent_Name = 'A' THEN 1 ELSE 0 END) as ICount1 ,
SUM(CASE WHEN Parent_Name = 'B' THEN 1 ELSE 0 END) as ICount2
from Detail_User
where userkey = 172

Add the column's with Alias name in sql server

I have the below query -
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;
The result of the query looks like -
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
Now i want a column "Total_Count" which should be the sum of New+Active+Resolved+Closed. The output should look like -
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
I tried the below query, but it would not help. Please look into the same.
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;
Regards.
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

Resources