2012-12-10 124 views
1

這些是我的兩個查詢當我分別解僱他們時,我給出了正確的答案,但是當他們合併時他們不會給出相同的答案。請幫忙把它們結合起來如何將這兩個查詢合併爲一個?

SELECT SUM(hours) AS S1 
,operating_company 
FROM Hours WHERE operating_company='AP-OH' GROUP BY operating_company 

SELECT COUNT(*) AS S1,event_type 
,operating_company 
FROM Event WHERE operating_company='AP-OH' AND event_type='OSH Restricted' GROUP BY 
operating_company,event_type 

感謝 RajeshB

+0

隨着不同的分組,結果當然會有所不同。你想要哪個結果? – Raptor

+0

你是否想要做[像這樣](http://stackoverflow.com/questions/2374778/how-do-i-combine-these-sql-select-queries-into-one-select-statement)? – Atik

回答

0

你可以這樣做:

SELECT 
    e.event_type, 
    e.operating_company, 
    SUM(h.hours) AS S1, 
    COUNT(e.*) AS S2 
FROM Hours h 
INNER JOIN event e ON h.operating_company = e.operating_company 
WHERE h.operating_company = 'AP-OH' 
    AND e.event_type  = 'OSH Restricted' 
GROUP BY e.operating_company, e.event_type; 
0

試試這個

SELECT SUM(S1.hours) ,S1.operating_company ,S2.operating_company ,COUNT(S2.*),S2.event_type , S2.operating_company 
    FROM Hours as S1 WHERE operating_company='AP-OH' 

    INNER JOIN event AS S2 
    ON S2.operating_company = S1.operating_company 
    WHERE S2.operating_company=S1.operating_company='AP-OH' AND S2.event_type='OSH Restricted' 
    GROUP BY 
    S2.operating_company,S2.event_type