2013-04-24 71 views
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%' 

正如你所看到的,每一組數據來自一個不同的表格,並沒有關係。我怎樣才能完成我想要的結果集?

謝謝。

+0

使用'Union'從Tableb'。實例'選擇col1,4 SELECT查詢從表A工會選擇COL2,5間 – praveen 2013-04-24 12:08:18

回答

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%' 
+0

感謝@muhmud的迴應,我添加了一個更多的子查詢,它與我的'自殺囚犯'數量來自同一個表。你能在你的答案中包括這個嗎? – etm124 2013-04-24 12:14:42

+1

完成。我也更新了它來使用'pivot',在那裏你有來自相同的from子句計數。 – muhmud 2013-04-24 12:20:21

0

與聯盟試試這個是形式在一個查詢:

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