0
我有一個查詢理應有這樣的輸出:我應該使用全部加入這個多數據庫SQL Server查詢
這裏是我的查詢:
select
table4.categoryNames, -- because only table4 has all the categories
table1.countResult, -- either (DATA) or null
table2.countResult,
table3.countResult,
table4.countResult,
table5.countResult,
table6.countResult,
table7.countResult
from
table1
full join
table2 on table1.categoryNames = table2.CategoryNames
full join
table3 on table2.categoryNames = table3.CategoryNames
full join
table4 on table3.categoryNames = table4.CategoryNames
full join
table5 on table4.categoryNames = table5.CategoryNames
full join
table6 on table5.categoryNames = table6.CategoryNames
full join
table7 on table6.categoryNames = table7.CategoryNames
和每個countResult
是一個查詢本身,按照表someTable
的字段CategoryNames
在其各自的數據庫中計數ID:
with table1 as
(
select
categroyName, count(ID) as countResult
from
database1.someTable
group by
categroyName),
-- table2 from database2.someTable
-- table3 from database3.someTable , and so on
我希望有一個category
表,但沒有。輸出不正確,因爲它顯示名爲NULL
的第7個類別。
我是SQL新手,我只是想知道在這種情況下我應該使用什麼最好的join
。
你需要一個「主類別」表,從您的SQL註釋似乎是'SELECT DISTINCT categoryNames FROM table4'。現在您只需將所有內容都加入到該主子表中即可。通過subtable我的意思是這樣定義的東西:'(SELECT DISTINCT categoryNames FROM table4)MySubtable'你可以在你的查詢中將該表引用爲MySubTable。 –