2017-05-17 11 views
0

私は5つの異なるゾーンの資産テーブルを持っています。私はすべてのテーブルとディスプレイから資産タイプを数えたいと思います。私は、このクエリを使用すると、SQLの同じサーバー内の複数のテーブルからの特定のフィールドの数

select AssetType,count(*) from MA.dbo.asset group by AssetType UNION ALL 
select AssetType,count(*) from VA.dbo.asset group by AssetType UNION ALL 
select AssetType,count(*) from RI.dbo.asset group by AssetType UNION ALL 
select AssetType,count(*) from NC.dbo.asset group by AssetType UNION ALL 
select AssetType,count(*) from VT.dbo.asset group by AssetType 

それは

Cash 1 
Inventory 1 
Cash 3 
Patents 2 
Goodwill 1 
Patents 3 

を表示します。しかし、私は出力が

Cash 4 
Inventory 1 
Patents 5 
Goodwill 1 

ことを期待する誰もがこれを達成するために私を助けることができますか?

+0

もう1つのクエリをもう一度グループに追加できます –

+0

どのDBMSを使用していますか? –

+0

SQL Server。ありがとうございました – user3331421

答えて

2

あなたがしなければならないのは、このように、クエリのソースとしてそのクエリを使用した:

SELECT AssetType, SUM(Count) AS Count 
FROM (
    select AssetType, count(*) as Count from MA.dbo.asset group by AssetType UNION ALL 
    select AssetType, count(*) as Count from VA.dbo.asset group by AssetType UNION ALL 
    select AssetType, count(*) as Count from RI.dbo.asset group by AssetType UNION ALL 
    select AssetType, count(*) as Count from NC.dbo.asset group by AssetType UNION ALL 
    select AssetType, count(*) as Count from VT.dbo.asset group by AssetType 
) SubQ 
GROUP BY AssetType 

注意サブクエリは名前を持っているすべての列を必要とすること。

+0

ありがとうアンドリュー – user3331421

関連する問題