[英]How do I return multiple columns and rows in a SQL Count Query?
I am trying to put together the query for a multiple row report. 我正在尝试将多行报告的查询放在一起。 I have this working.
我有这个工作。 Each type is coming from a different database, so I am using subqueries to pull it together and the results are accurate, but all on one row.
每种类型都来自不同的数据库,因此我正在使用子查询将其组合在一起,结果是准确的,但全部都在一行上。 I would like to put incidents on their own row, service requests on their own row, and there are four other tables I want to pull the same aged data from.
我想将事件放在他们自己的行上,将服务请求放在他们自己的行上,另外还有四个我要从中提取相同老化数据的表。 How do I get each set of aged days onto their own row for each type of ticket?
如何将每种类型的机票的每组老龄化日期放到各自的行中?
--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
Not 100% clear without better specifications, but try the following, I'm guessing it will get you on the right track: 如果没有更好的规格,还不是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
This is the final code I wound up using based on Bernd Linde's suggestion: 这是根据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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.