如何在SQL Count Query中返回多个列和行?

时间:2015-01-09 16:33:10

标签: sql sql-server tsql

我正在尝试将查询放在一起来进行多行报告。我有这个工作。每种类型都来自不同的数据库,所以我使用子查询将它拉到一起,结果是准确的,但所有都在一行上。我想把事件放在他们自己的行上,服务请求在他们自己的行上,还有其他四个表我想从中提取相同的老化数据。对于每种类型的机票,我如何将每组老化日记录到自己的行中?

--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

2 个答案:

答案 0 :(得分:1)

没有更好的规格,不是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

答案 1 :(得分:0)

根据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