我有4张桌子。这些表是根据类别计算另一个表中的条目数的结果。这是用于创建这些表的查询:
SELECT Category, COUNT(*) AS Number
FROM DATABASE.dbo.TABLE
GROUP BY Category;
就像我说的,这些表中有4个是从不同的数据库中创建的。那么我怎样才能编写一个以Category1作为Column1的查询,然后接下来的四列是来自其他4个其他数据库的计数结果?
修改
任何不清楚的事情......
SELECT Category, COUNT(*) AS Number1
FROM DATABASE1.dbo.TABLE
GROUP BY Category;
SELECT Category, COUNT(*) AS Number2
FROM DATABASE2.dbo.TABLE
GROUP BY Category;
SELECT Category, COUNT(*) AS Number3
FROM DATABASE3.dbo.TABLE
GROUP BY Category;
SELECT Category, COUNT(*) AS Number4
FROM DATABASE4.dbo.TABLE
GROUP BY Category;
但我想要的是:
| Category | Number1 | Number2 | Number3 | Number4 |
|----------|---------|---------|---------|---------|
| | | | | |
| | | | | |
我试过
SELECT DATABASE1.dbo.TABLE.Category, DATABASE1.dbo.TABLE.COUNT(*), DATABASE2.dbo.TABLE.COUNT(*), DATABASE3.dbo.TABLE.COUNT(*), DATABASE4.dbo.TABLE.COUNT(*)
FROM DATABASE1.dbo.TABLE
JOIN DATABASE2.dbo.TABLE
ON DATABASE1.dbo.TABLE=DATABASE2.dbo.TABLE
JOIN DATABASE3.dbo.TABLE
ON DATABASE2.dbo.TABLE=DATABASE3.dbo.TABLE
JOIN DATABASE4.dbo.TABLE
ON DATABASE3.dbo.TABLE=DATABASE4.dbo.TABLE
但那不起作用。
答案 0 :(得分:2)
我建议union all
和聚合:
select category, sum(t1) as t1, sum(t2) as t2, sum(t3) as t3, sum(t4) as t4
from ((select category, 1 as t1, 0 as t2, 0 as t3, 0 as t4
from DATABASE1.dbo.TABLE
) union all
(select category, 0 as t1, 1 as t2, 0 as t3, 0 as t4
from DATABASE2.dbo.TABLE
) union all
(select category, 0 as t1, 0 as t2, 1 as t3, 0 as t4
from DATABASE3.dbo.TABLE
) union all
(select category, 0 as t1, 0 as t2, 0 as t3, 1 as t4
from DATABASE4.dbo.TABLE t
)
) tt
group by category;
答案 1 :(得分:2)
您可以使用inner join
来执行此操作。
SELECT t1.category,t1.Number1,t2.Number2,t3.Number3,t4.Number4
FROM (select Category,COUNT(*) AS Number1 from DATABASE1.dbo.TABLE GROUP BY Category) t1
join (select Category,COUNT(*) AS Number2 from DATABASE2.dbo.TABLE GROUP BY Category) t2
on t1.category=t2.category
join (select Category,COUNT(*) AS Number3 from DATABASE3.dbo.TABLE GROUP BY Category) t3
on t2.category=t3.category
join (select Category,COUNT(*) AS Number4 from DATABASE4.dbo.TABLE GROUP BY Category) t4
on t3.category=t4.category
如果某个类别在各个表格之间不常见,则为full join
。
SELECT coalesce(t1.category,t2.category,t3.category,t4.category),
coalesce(t1.Number1,0),
coalesce(t2.Number2,0),
coalesce(t3.Number3,0),
coalesce(t4.Number4,0)
FROM (select Category,COUNT(*) AS Number1 from DATABASE1.dbo.TABLE GROUP BY Category) t1
full join (select Category,COUNT(*) AS Number2 from DATABASE2.dbo.TABLE GROUP BY Category) t2
on t1.category=t2.category
full join (select Category,COUNT(*) AS Number3 from DATABASE3.dbo.TABLE GROUP BY Category) t3
on t2.category=t3.category
full join (select Category,COUNT(*) AS Number4 from DATABASE4.dbo.TABLE GROUP BY Category) t4
on t3.category=t4.category
答案 2 :(得分:1)
您可以使用多个Common Table Expression(CTE)
来获取类别和计数,并加入多个CTE
。
您的查询应该是:
with cte1 as
(
select category, count(*) as number1 from DATABASE1.dbo.TABLE group by category
),
cte2 as
(
select category, count(*) as number2 from DATABASE2.dbo.TABLE group by category
),
cte3 as
(
select category, count(*) as number3 from DATABASE3.dbo.TABLE group by category
),
cte4 as
(
select category, count(*) as number4 from DATABASE4.dbo.TABLE group by category
)
select c1.category, c1.number1,c2.number2,c3.number3,c4.number4
from cte1 c1 INNER JOIN cte2 c2 ON c1.category = c2.category
INNER JOIN cte3 c3 ON c1.category = c3.category
INNER JOIN cte4 c4 ON c1.category = c4.category
我不确定此查询的效果,但它可以选择。