我需要一次性查看以下四个查询的总数。
SELECT count(companyid) FROM [CoreReferenceStaging].[dbo].[FinData2000_1]
SELECT count(companyid) FROM [CoreReferenceStaging].[dbo].[FinData2000_2]
SELECT count(companyid) FROM [CoreReferenceStaging].[dbo].[FinData2000_3]
SELECT count(companyid) FROM [CoreReferenceStaging].[dbo].[FinData2000_4]
我尝试了以下操作,但是我收到了错误#39;)'。
附近的语法不正确SELECT count(companyid) FROM
(
SELECT companyid FROM [CoreReferenceStaging].[dbo].[FinData2000_1]
union all
SELECT companyid FROM [CoreReferenceStaging].[dbo].[FinData2000_2]
union all
SELECT companyid FROM [CoreReferenceStaging].[dbo].[FinData2000_3]
union all
SELECT companyid FROM [CoreReferenceStaging].[dbo].[FinData2000_4]
)
答案 0 :(得分:4)
你快到了
SELECT sum(c) FROM
(
SELECT count(companyid) c FROM [CoreReferenceStaging].[dbo].[FinData2000_1]
union all
SELECT count(companyid) c FROM [CoreReferenceStaging].[dbo].[FinData2000_2]
union all
SELECT count(companyid) c FROM [CoreReferenceStaging].[dbo].[FinData2000_3]
union all
SELECT count(companyid) c FROM [CoreReferenceStaging].[dbo].[FinData2000_4]
) a
ps,有很多方法可以为这只猫提供皮肤:
SELECT
(SELECT count(companyid) c FROM [CoreReferenceStaging].[dbo].[FinData2000_1]) +
(SELECT count(companyid) c FROM [CoreReferenceStaging].[dbo].[FinData2000_2]) +
(SELECT count(companyid) c FROM [CoreReferenceStaging].[dbo].[FinData2000_3]) +
(SELECT count(companyid) c FROM [CoreReferenceStaging].[dbo].[FinData2000_4])
SELECT a+b+c+d FROM
(SELECT count(companyid) a FROM [CoreReferenceStaging].[dbo].[FinData2000_1]) aa
CROSS JOIN
(SELECT count(companyid) b FROM [CoreReferenceStaging].[dbo].[FinData2000_2]) bb
CROSS JOIN
(SELECT count(companyid) c FROM [CoreReferenceStaging].[dbo].[FinData2000_3]) cc
CROSS JOIN
(SELECT count(companyid) d FROM [CoreReferenceStaging].[dbo].[FinData2000_4]) dd
SELECT count(companyid) FROM
(
SELECT companyid FROM [CoreReferenceStaging].[dbo].[FinData2000_1]
union all
SELECT companyid FROM [CoreReferenceStaging].[dbo].[FinData2000_2]
union all
SELECT companyid FROM [CoreReferenceStaging].[dbo].[FinData2000_3]
union all
SELECT companyid FROM [CoreReferenceStaging].[dbo].[FinData2000_4]
) a
要记住的关键事项: