如何在 SQL 计数查询中 return 多个列和行?
How do I return multiple columns and rows in a SQL Count Query?
我正在尝试将多行报告的查询放在一起。我有这个工作。每种类型都来自不同的数据库,所以我使用子查询将它们组合在一起,结果是准确的,但都在一行中。我想将事件放在他们自己的行中,将服务请求放在他们自己的行中,并且我想从其他四个表中提取相同的老化数据。如何将每组过时的日期分别放入每种类型机票的行中?
--SELECT COUNT (*)
Select ai15 as [Incidents Aged 15 Days], as15 as [Service Requests Aged 15 Days],
ai30 as [Incidents Aged 30 Days], as30 as [Service Requests Aged 30 Days],
ai60 as [Incidents Aged 60 Days], as60 as [Service Requests Aged 60 Days],
ai90 as [Incidents Aged 90 Days], as90 as [Service Requests Aged 90 Days],
ai120 as [Incidents Aged 120 Days], as120 as [Service Requests Aged 120 Days]
from
(select count(*) as ai15
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 15
) gt15i,
(select count(*) as ai30
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 30
) gt30i,
(select count(*) as ai60
from IncidentDimVw
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 60
) gt60i,
(select count(*) as ai90
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 90
) gt90i,
(select count(*) as ai120
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 120
) gt120i,
(select count(*) as as15
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 15
) gt15s,
(select count(*) as as30
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 30
) gt30s,
(select count(*) as as60
from ServiceRequestDimVw
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 60
) gt60s,
(select count(*) as as90
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 90
) gt90s,
(select count(*) as as120
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 120
) gt120s
如果没有更好的规范,也不是 100% 清楚,但请尝试以下方法,我猜它会让您走上正轨:
select count(*) as count, 'ai15' as subType, 'Incidents' as mainType
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 15
union all
select count(*) as count, 'ai30' as subType, 'Incidents' as mainType
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 30
union all
select count(*) as count, 'ai60' as subType, 'IncidentDimVw' as mainType
from IncidentDimVw
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 60
union all
select count(*) as count, 'ai90' as subType, 'Incidents' as mainType
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 90
union all
select count(*) as count, 'ai120' as subType, 'Incidents' as mainType
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 120
union all
select count(*) as count, 'as15' as subType, 'SRs' as mainType
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 15
union all
select count(*) as count, 'as30' as subType, 'SRs' as mainType
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 30
union all
select count(*) as count, 'as60' as subType, 'ServiceRequestDimVw' as mainType
from ServiceRequestDimVw
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 60
union all
select count(*) as count, 'as90' as subType, 'SRs' as mainType
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 90
union all
select count(*) as count, 'as120' as subType, 'SRs' as mainType
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 120
这是我根据 Bernd Linde 的建议最终使用的代码:
Select gttype as [Ticket Type],
ai15 as [Incidents Aged 15 Days],
ai30 as [Incidents Aged 30 Days],
ai60 as [Incidents Aged 60 Days],
ai90 as [Incidents Aged 90 Days],
ai120 as [Incidents Aged 120 Days]
from
(select 'Incident' as gttype
) gttyper,
(select count(*) as ai15
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 15
) gt15i,
(select count(*) as ai30
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 30
) gt30i,
(select count(*) as ai60
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 60
) gt60i,
(select count(*) as ai90
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 90
) gt90i,
(select count(*) as ai120
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 120
) gt120i
UNION
Select gttype as [Ticket Type],
as15 as [Service Requests Aged 15 Days],
as30 as [Service Requests Aged 30 Days],
as60 as [Service Requests Aged 60 Days],
as90 as [Service Requests Aged 90 Days],
as120 as [Service Requests Aged 120 Days]
from
(select 'Service Request' as gttype
) gttyper,
(select count(*) as as15
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 15
) gt15s,
(select count(*) as as30
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 30
) gt30s,
(select count(*) as as60
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 60
) gt60s,
(select count(*) as as90
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 90
) gt90s,
(select count(*) as as120
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 120
) gt120s
我正在尝试将多行报告的查询放在一起。我有这个工作。每种类型都来自不同的数据库,所以我使用子查询将它们组合在一起,结果是准确的,但都在一行中。我想将事件放在他们自己的行中,将服务请求放在他们自己的行中,并且我想从其他四个表中提取相同的老化数据。如何将每组过时的日期分别放入每种类型机票的行中?
--SELECT COUNT (*)
Select ai15 as [Incidents Aged 15 Days], as15 as [Service Requests Aged 15 Days],
ai30 as [Incidents Aged 30 Days], as30 as [Service Requests Aged 30 Days],
ai60 as [Incidents Aged 60 Days], as60 as [Service Requests Aged 60 Days],
ai90 as [Incidents Aged 90 Days], as90 as [Service Requests Aged 90 Days],
ai120 as [Incidents Aged 120 Days], as120 as [Service Requests Aged 120 Days]
from
(select count(*) as ai15
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 15
) gt15i,
(select count(*) as ai30
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 30
) gt30i,
(select count(*) as ai60
from IncidentDimVw
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 60
) gt60i,
(select count(*) as ai90
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 90
) gt90i,
(select count(*) as ai120
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 120
) gt120i,
(select count(*) as as15
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 15
) gt15s,
(select count(*) as as30
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 30
) gt30s,
(select count(*) as as60
from ServiceRequestDimVw
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 60
) gt60s,
(select count(*) as as90
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 90
) gt90s,
(select count(*) as as120
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 120
) gt120s
如果没有更好的规范,也不是 100% 清楚,但请尝试以下方法,我猜它会让您走上正轨:
select count(*) as count, 'ai15' as subType, 'Incidents' as mainType
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 15
union all
select count(*) as count, 'ai30' as subType, 'Incidents' as mainType
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 30
union all
select count(*) as count, 'ai60' as subType, 'IncidentDimVw' as mainType
from IncidentDimVw
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 60
union all
select count(*) as count, 'ai90' as subType, 'Incidents' as mainType
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 90
union all
select count(*) as count, 'ai120' as subType, 'Incidents' as mainType
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 120
union all
select count(*) as count, 'as15' as subType, 'SRs' as mainType
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 15
union all
select count(*) as count, 'as30' as subType, 'SRs' as mainType
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 30
union all
select count(*) as count, 'as60' as subType, 'ServiceRequestDimVw' as mainType
from ServiceRequestDimVw
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 60
union all
select count(*) as count, 'as90' as subType, 'SRs' as mainType
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 90
union all
select count(*) as count, 'as120' as subType, 'SRs' as mainType
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 120
这是我根据 Bernd Linde 的建议最终使用的代码:
Select gttype as [Ticket Type],
ai15 as [Incidents Aged 15 Days],
ai30 as [Incidents Aged 30 Days],
ai60 as [Incidents Aged 60 Days],
ai90 as [Incidents Aged 90 Days],
ai120 as [Incidents Aged 120 Days]
from
(select 'Incident' as gttype
) gttyper,
(select count(*) as ai15
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 15
) gt15i,
(select count(*) as ai30
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 30
) gt30i,
(select count(*) as ai60
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 60
) gt60i,
(select count(*) as ai90
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 90
) gt90i,
(select count(*) as ai120
from Incidents
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 120
) gt120i
UNION
Select gttype as [Ticket Type],
as15 as [Service Requests Aged 15 Days],
as30 as [Service Requests Aged 30 Days],
as60 as [Service Requests Aged 60 Days],
as90 as [Service Requests Aged 90 Days],
as120 as [Service Requests Aged 120 Days]
from
(select 'Service Request' as gttype
) gttyper,
(select count(*) as as15
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 15
) gt15s,
(select count(*) as as30
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 30
) gt30s,
(select count(*) as as60
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 60
) gt60s,
(select count(*) as as90
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 90
) gt90s,
(select count(*) as as120
from SRs
where status like '%Active%' and ABS(DATEDIFF(day, CreatedDate, GETDATE())) > 120
) gt120s