SQL - 多表连接

时间:2011-07-12 08:55:16

标签: sql sql-server-2008

嗨,也许有人可以帮助我... 我对SQL语句有一点问题。 (在MS上 - SQL Server 2008) 所以我有6个表看起来像这样

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

现在我需要(在一个声明中优先:P)每个表中按公司和月份分组的数据集的数量,看起来像这样。 还有一件事情并非所有表都需要为该公司和那个月提供数据,因此计数结果可以为0(*)

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

如果在一个声明中不可能,那么我必须让它以另一种方式工作。 :)

由于

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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