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
你有六個表幾乎相同的結構?而你上面的'ORDER BY'可能應該由'Company'而不是'Firma'命令;) – Jacob
啊對不起:P忘記替換 – Lim
當然,在一個聲明中是可以的。檢查我的答案 –