我正在尝试创建一个结果,显示某个地区的行业细分。最终结果看起来像这样。
Area Industry Number of sites
000000 All industries 250
000001 Industry 1(11 and 21) 22
000001 Industry 2(23) 25
......
000001 Industry 10 (81) 120
一些背景使以上有意义。在原始数据集中,有6个数字代码供业界使用。我们根据前两位数将它们分解,然后制作自定义组。 11和21是一组,依此类推。我可以想出如何获得这个行的各个行但是很难找到如何在一个结果中获得所有11行。我可以编写并执行11个单独的查询,但我希望得到一个结果。下面是获取其中一行的代码。这是使用JOIN或UNION命令或运算符的地方吗?
SELECT
[Area],
COUNT(*) AS [Number of Sites]
FROM
dbo.sizeclassreport
Where area='000001' and (code like '11%' or code like '21%')
GROUP BY
[area]
答案 0 :(得分:0)
我认为这很简单,但互联网结果令人困惑。所有这一切都是一个" UNION ALL"每个查询之间。
SELECT
[Area],
COUNT(*) AS [Number of Sites]
FROM
dbo.sizeclassreport
Where area='000001' and (code like '11%' or code like '21%')
GROUP BY
[area]
UNION ALL
SELECT
[Area],
COUNT(*) AS [Number of Sites]
FROM
dbo.sizeclassreport
Where area='000001' and (code like '23%')
GROUP BY
[area]
UNION ALL
SELECT
[Area],
COUNT(*) AS [Number of Sites]
FROM
dbo.sizeclassreport
Where area='000001' and (code like '31%' or code like '32%' or code like
'33%')
GROUP BY
[area]