将不相关的查询组合到一个查询中以生成计数

时间:2013-04-24 12:04:59

标签: sql sql-server sql-server-2008

我希望每天运行一个存储过程,生成一个计数报告。

例如,.csv看起来像这样:

    Daily,1
    Deaths,0
    In-House EKG,4
    In-House Xray,2
    Suicidal Patients,12
    HIV,0

他们的个人查询看起来像这样:

-- Daily and Death Counts
select
    SUM(CASE WHEN location != '[OUT]' THEN 1 ELSE 0 END) as 'Daily',
    SUM(CASE WHEN death = 1 THEN 1 ELSE 0 END) as 'Deaths'
from
    patient_data


-- In-House Tasks
select 
    SUM(CASE WHEN cat_id = 72 THEN 1 ELSE 0 END) as 'In-House EKG',
    SUM(CASE WHEN cat_id = 73 THEN 1 ELSE 0 END) as 'In-House XRay',    
from
    organizer_tasks

-- Suicidal Patients
select 
    count(distinct(pid)) as 'Suicidal Inmates'
from 
    problems pr 
        inner join problem_list pl on pl.id = pr.problem_list_id 
where 
    pr.status = 'open'
    and pl.title like '%suicide%'

-- HIV

select 
    count(distinct(pid)) as 'HIV'
from 
    problems pr 
        inner join problem_list pl on pl.id = pr.problem_list_id 
        inner join patient_data pd on pr.pid = pd.pid
where 
    pr.status = 'open'
    and pl.title like '%hiv%'

如您所见,每组数据来自不同的表,并且没有关系。如何完成我想要的结果集?

感谢。

2 个答案:

答案 0 :(得分:2)

-- Daily and Death Counts
select * from (
    select
        SUM(CASE WHEN location != '[OUT]' THEN 1 ELSE 0 END) as 'Daily',
        SUM(CASE WHEN death = 1 THEN 1 ELSE 0 END) as 'Deaths'
    from
        patient_data
) tmp unpivot (Number for Type in ([Daily], [Deaths])) t

union all

-- In-House Tasks
select * from (
    select 
        SUM(CASE WHEN cat_id = 72 THEN 1 ELSE 0 END) as 'In-House EKG',
        SUM(CASE WHEN cat_id = 73 THEN 1 ELSE 0 END) as 'In-House XRay'
    from
        organizer_tasks
) tmp unpivot (Number for Type in ([In-House EKG], [In-House XRay])) t

union all

-- Suicidal Patients
select 'Suicidal Inmates',
    count(distinct(pid))
from 
    problems pr 
        inner join problem_list pl on pl.id = pr.problem_list_id 
where 
    pr.status = 'open'
    and pl.title like '%suicide%'

union all

-- HIV

select 'HIV',
    count(distinct(pid))
from 
    problems pr 
        inner join problem_list pl on pl.id = pr.problem_list_id 
        inner join patient_data pd on pr.pid = pd.pid
where 
    pr.status = 'open'
    and pl.title like '%hiv%'

答案 1 :(得分:0)

尝试使用Union,它是一个Query中的形式:

    select
        SUM(CASE WHEN location != '[OUT]' THEN 1 ELSE 0 END) as 'Daily'   
    from
        patient_data    
UNION ALL
    select   
        SUM(CASE WHEN death = 1 THEN 1 ELSE 0 END) as 'Deaths'
    from
    patient_data

UNION ALL
-- In-House Tasks
    select 
        SUM(CASE WHEN cat_id = 72 THEN 1 ELSE 0 END) as 'In-House EKG'  
    from
    organizer_tasks
UNION ALL
    select
        SUM(CASE WHEN cat_id = 73 THEN 1 ELSE 0 END) as 'In-House XRay'
    from
        organizer_tasks 
UNION ALL
-- Suicidal Patients
    select 
        count(distinct(pid)) as 'Suicidal Inmates'
    from 
        problems pr 
            inner join problem_list pl on pl.id = pr.problem_list_id 
    where 
        pr.status = 'open'
        and pl.title like '%suicide%'