2011-07-12 40 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' –