2011-07-12 52 views
1

嗨,也許有人可以幫助我在這裏... 我有一個SQL語句的小問題。 (在MS - SQL Server 2008中) 所以我有6張桌子看起來像這樣SQL - 多表加入

ID /公司/月/ ClosedTimeStamp /不同的信息

現在我需要(preferrable在一個聲明:P)數據集的計數從按公司和月份分組的每個表看起來像這樣。 還有另一件事不是所有的表需要擁有該公司及該月數據,因此可以有0的結果爲COUNT(*)

SELECT COUNT(*) as c, Month, Company 
FROM Table1 WHERE ClosedTimeStamp IS NULL 
GROUP BY Company, Month 
ORDER BY Company 

我可以爲所有的表做到這一點,隨便挑出來每家公司的結果......那麼,如果有人有任何想法,我真的將不勝感激:)

對不起忘了什麼......結果應該是這樣的:

公司/月/ CountTable1/CountTable2/CountTable3/..... 測試02 1 0 50

如果在一個陳述中不可能,那麼我必須讓它以另一種方式工作。 :)

感謝

+0

你有六個表幾乎相同的結構?而你上面的'ORDER BY'可能應該由'Company'而不是'Firma'命令;) – Jacob

+0

啊對不起:P忘記替換 – Lim

+0

當然,在一個聲明中是可以的。檢查我的答案 –

回答

1

如果您的數據庫已標準化,查詢將會簡單得多。

因爲,你companyMonth跨6臺傳播,我們需要做這些表的union,以獲取所有company + month的不同數據集,因爲這樣的:

select company, month from table1 
union 
select company, month from table2 
union 
select company, month from table3 
union 
select company, month from table4 
union 
select company, month from table5 
union 
select company, month from table6 

。注意,我們需要union,而不是union all,因爲我們不希望重複同一公司+月份對。

然後,就用這個數據集來查詢數量爲每個表:

select t.company, t.month, 
    (select count(*) from table1 
     where company = t.company 
     and month = t.month 
     and ClosedTimeStamp is null) as qt1, 
    (select count(*) from table2 
     where company = t.company 
     and month = t.month 
     and ClosedTimeStamp is null) as qt2, 
    (select count(*) from table3 
     where company = t.company 
     and month = t.month 
     and ClosedTimeStamp is null) as qt3, 
    (select count(*) from table4 
     where company = t.company 
     and month = t.month 
     and ClosedTimeStamp is null) as qt4, 
    (select count(*) from table5 
     where company = t.company 
     and month = t.month 
     and ClosedTimeStamp is null) as qt5, 
    (select count(*) from table6 
     where company = t.company 
     and month = t.month 
     and ClosedTimeStamp is null) as qt6 
from (
    select company, month from table1 
    union 
    select company, month from table2 
    union 
    select company, month from table3 
    union 
    select company, month from table4 
    union 
    select company, month from table5 
    union 
    select company, month from table6 
) t 
order by t.company 
+0

哇謝謝,這工作:) – Lim

2

UNION ALL錶行,然後做計數

SELECT COUNT(*) as c, Month, Company 
FROM 
(
SELECT Month,Company FROM Table1 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company FROM Table2 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company FROM Table3 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company FROM Table4 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company FROM Table5 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company FROM Table6 WHERE ClosedTimeStamp IS NULL 
) AS t 
GROUP BY Company, Month 
ORDER BY Company 

如果你想總的每個表,公司在一排

SELECT SUM(t1) t1,SUM(t2) t2,SUM(t3) t3,SUM(t4) t4,SUM(t5) t5,SUM(t6) t6, Month, Company 
FROM 
(
SELECT Month,Company, 1 t1,0 t2, 0 t3, 0 t4, 0 t5, 0 t6 FROM Table1 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company, 0 t1,1 t2, 0 t3, 0 t4, 0 t5, 0 t6 FROM Table2 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company, 0 t1,0 t2, 1 t3, 0 t4, 0 t5, 0 t6 FROM Table3 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company, 0 t1,0 t2, 0 t3, 1 t4, 0 t5, 0 t6 FROM Table4 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company, 0 t1,0 t2, 0 t3, 0 t4, 1 t5, 0 t6 FROM Table5 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company, 0 t1,0 t2, 0 t3, 0 t4, 0 t5, 1 t6 FROM Table6 WHERE ClosedTimeStamp IS NULL 
) AS t 
GROUP BY Company, Month 
ORDER BY Company 
+0

這只是讓我一個計數所有表/ companys不是我要找的結果應該看起來像這樣 公司/月/ CountTable1/CountTable2/CountTable3/..... 測試02 1 0 50 – Lim

+0

已更新。請參閱備選 – niktrs

+0

您忘記更正表格的名稱。它並不總是'表1' –