2

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

例如,.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%'

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

谢谢。

4

2 回答 2

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%'
于 2013-04-24T12:10:28.243 回答
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%'
于 2013-04-24T12:13:30.780 回答