计数 - 我可以在一个查询中获得多个计数吗?

时间:2018-01-09 13:29:17

标签: sql sql-server

我正在尝试为服务台构建指标表。

基本上,我希望选择然后插入表格 用户名 门票数量 门票超过7天 门票超过30天

我有一个查询 - 见下文。我可以计算,计算超过7天,并且通过运行3次轻松计算超过30天,但有更简单的方法吗?此查询正在提取超过30天的故障单数

    select xxx.Assignee, count(*) as 'All Assigned'
    from (
    --All Incidents With Assignments
    select i.ticket_number, i.status_1 as Status, i.title, i.description,  
    u.user_login as 'Assignee' from 
    [fpscdb008_system].[asgnmt] a, [fpscdb008_system].[app_user] u, 
    [fpscdb008_ws_004].[incidents] i
    where i.id = a.item_id
    and a.item_defn_id = 12610
    and u.app_user_id = a.app_user_id
    and i.soft_delete_id = 0 
    and i.status_1 not in ('Closed','Resolved','Cancelled')
    and i.created_on <= GETDATE() - 30

    Union
    --All SRs With Assignments
    select s.ticket_number, s.status_1 as Status, s.title, s.description,  
    u.user_login as 'Assignee' from 
    [fpscdb008_system].[asgnmt] a, [fpscdb008_system].[app_user] u, 
    [fpscdb008_ws_004].[service_request] s
    where a.app_user_id = u.app_user_id
    and a.item_defn_id = 7861
    and s.id = a.item_id
    and s.soft_delete_id = 0 
    and s.status_1 not in ('Closed','Resolved','Cancelled')
    and s.created_on <= GETDATE() - 30

    ) as xxx
    group by xxx.Assignee
    order by xxx.Assignee

2 个答案:

答案 0 :(得分:1)

使用CASE语句计算:

select xxx.Assignee, count(*) as 'All Assigned', 
SUM(CASE WHEN created_on <= GETDATE() - 7 THEN 1 ELSE 0 END) as 'Older than 7 days',
SUM(CASE WHEN created_on <= GETDATE() - 30 THEN 1 ELSE 0 END) as 'Older than 30 days'

from (
    --All Incidents With Assignments
    select i.ticket_number, i.status_1 as Status, i.title, i.description,  
    u.user_login as 'Assignee', s.created_on
    from [fpscdb008_system].[asgnmt] a, [fpscdb008_system].[app_user] u, 
    [fpscdb008_ws_004].[incidents] i
    where i.id = a.item_id
    and a.item_defn_id = 12610
    and u.app_user_id = a.app_user_id
    and i.soft_delete_id = 0 
    and i.status_1 not in ('Closed','Resolved','Cancelled')

    Union
    --All SRs With Assignments
    select s.ticket_number, s.status_1 as Status, s.title, s.description,  
    u.user_login as 'Assignee', s.created_on
    from [fpscdb008_system].[asgnmt] a, [fpscdb008_system].[app_user] u, 
    [fpscdb008_ws_004].[service_request] s
    where a.app_user_id = u.app_user_id
    and a.item_defn_id = 7861
    and s.id = a.item_id
    and s.soft_delete_id = 0 
    and s.status_1 not in ('Closed','Resolved','Cancelled')

) as xxx
group by xxx.Assignee
order by xxx.Assignee

答案 1 :(得分:1)

使用CASE语句区分日期范围,也不使用旧的JOIN语法

select xxx.Assignee, count(*) as 'All Assigned'
    ,SUM(CASE WHEN created_on <= GETDATE() - 7 THEN 1 ELSE 0 END) as '> 7 days'
    ,SUM(CASE WHEN created_on <= GETDATE() - 30 THEN 1 ELSE 0 END) as '> 30 days'
from (
    --All Incidents With Assignments
    select i.ticket_number, i.status_1 as Status, i.title, i.description,  
    u.user_login as 'Assignee', i.created_on
    FROM 
        [fpscdb008_system].[asgnmt] a
    INNER JOIN
        [fpscdb008_system].[app_user] u ON u.app_user_id = a.app_user_id
    INNER JOIN
        [fpscdb008_ws_004].[incidents] i ON i.id = a.item_id
    where
        a.item_defn_id = 12610
    and i.soft_delete_id = 0 
    and i.status_1 not in ('Closed','Resolved','Cancelled')

    UNION
    --All SRs With Assignments
    select s.ticket_number, s.status_1 as Status, s.title, s.description,  
    u.user_login as 'Assignee', s.created_on
    FROM 
        [fpscdb008_system].[asgnmt] a
    INNER JOIN
        [fpscdb008_system].[app_user] u ON a.app_user_id = u.app_user_id
    INNER JOIN
        [fpscdb008_ws_004].[service_request] s ON s.id = a.item_id
    WHERE
        a.item_defn_id = 7861
    and s.soft_delete_id = 0 
    and s.status_1 not in ('Closed','Resolved','Cancelled')
) as xxx
GROUP BY xxx.Assignee
ORDER BY xxx.Assignee