我尝试了多种不同的解决方案,但我仍未从SQL Server
查询中获得我想要的结果。我有两个表PRIMARY_CATEGORY
,其中Category_Id
为 PK ,每个表为Category_Name
。有4个不同的类别。另一个表格为PRIMARY_SUBCATEGORY
,其中Subcategory_Id
(PK),Subcategory_Name
,Category_Id
为 FK 到PRIMARY_CATEGORY
表。
我现在有一个select语句,它从一个大列中的Subcategory_Name
返回整个列表,但是我希望它将这些列分别为每个Category_Names的4个不同的列。所以它应该是(带〜显示省略的值7-49)
Column 1 | Column 2 | Column 3 | Column 4
Value A1 | Value B1 | Value C1 | Value D1
Value A2 | Value B2 | Value C2 | Value D2
Value A3 | Value B3 | Value C3 | Value D3
Value A4 | Value B4 | Value C4 | Value D4
Value A5 | Value B5 | Value C5 | Value D5
Value A6 | Value B6 | Value C6 | Value D6
~ | ~ | ~ | ~
Value A50| Value B50| Value C50| Value D50
这是我拥有的,但是当我运行时,我没有得到任何结果。它按照我想要的方式格式化列。
SELECT
psc1.Subcategory_Name as 'Column 1',
psc2.Subcategory_Name as 'Column 2',
psc3.Subcategory_Name as 'Column 3',
psc4.Subcategory_Name as 'Column 4'
FROM PRIMARY_CATEGORY pc
INNER JOIN PRIMARY_SUBCATEGORY psc1 ON psc1.Category_Id = pc.Category_Id
INNER JOIN PRIMARY_SUBCATEGORY psc2 ON psc2.Category_Id = pc.Category_Id
INNER JOIN PRIMARY_SUBCATEGORY psc3 ON psc3.Category_Id = pc.Category_Id
INNER JOIN PRIMARY_SUBCATEGORY psc4 ON psc4.Category_Id = pc.Category_Id
WHERE
psc1.Category_Id IN (select Category_Id from PRIMARY_CATEGORY where Category_Name = 'Category 1')
psc2.Category_Id IN (select Category_Id from PRIMARY_CATEGORY where Category_Name = 'Category 2')
psc3.Category_Id IN (select Category_Id from PRIMARY_CATEGORY where Category_Name = 'Category 3')
psc4.Category_Id IN (select Category_Id from PRIMARY_CATEGORY where Category_Name = 'Category 4')
我是否需要这么多内连接才能做到这一点,或者这只能用一个连接完成吗?每列应基本上只显示与Category关联的Subcategory_Names。
我觉得这可能是一个过于复杂的查询来完成这项任务,但我不确定一个更好的方法来做到这一点。
使用MAX值运行雷达所建议但没有分组,我得到类似的东西
Column 1 | Column 2 | Column 3 | Column 4
Value A50 | Value A50 | Value C50 | Value D50
当我使用Subcategory_Id
运行时,我得到了这个
Category_Id | Column 1 | Column 2 | Column 3 | Column 4
1 | [Blank] | [Blank] | Value C50 | [Blank]
2 | [Blank] | Value A50 | [Blank] | [Blank]
3 | [Blank] | [Blank] | [Blank] | Value D50
4 | Value A50 | [Blank] | [Blank] | [Blank]
答案 0 :(得分:2)
您可以使用case based aggregation
您也可以使用pivot
SELECT pc.category_id,
MAX(case when
pc.Category_Name ='Category 1'
then psc.Subcategory_Name
else NULL end) as 'Column 1',
MAX(case when
pc.Category_Name ='Category 2'
then psc.Subcategory_Name
else NULL end) as 'Column 2',
MAX(case when
pc.Category_Name ='Category 3'
then psc.Subcategory_Name
else NULL end) as 'Column 3',
MAX(case when
pc.Category_Name ='Category 4'
then psc.Subcategory_Name
else NULL end) as 'Column 4',
FROM PRIMARY_CATEGORY pc
INNER JOIN PRIMARY_SUBCATEGORY psc ON pc.category_id = psc.category_id
GROUP BY pc.category_id
答案 1 :(得分:1)
您可以使用pivot操作符获取所需的结果集,如下所示:
SELECT pivotedCataegories.[Category 1]
, pivotedCataegories.[Category 2]
, pivotedCataegories.[Category 3]
, pivotedCataegories.[Category 4]
FROM (
SELECT pc.Category_Name
, ROW_NUMBER() OVER (PARTITION BY pc.Category_Id ORDER BY psc.Subcategory_Id) AS subCatNumber
, psc.Subcategory_Name
FROM PRIMARY_CATEGORY pc
INNER JOIN PRIMARY_SUBCATEGORY psc ON psc.Category_Id = pc.Category_Id
) AS categories
PIVOT (
MAX(s.Subcategory_Name)
FOR s.Category_Name IN ([Category 1], [Category 2], [Category 3], [Category 4])
) pivotedCataegories
PIVOT运算符为s.Category_Name IN ([Categoasry 1], [Category 2], [Category 3], [Category 4])
指定的每个值创建一列结果集,并为唯一值s.subCatNumber
创建一行。每个类别列的值为MAX(s.Subcategory_Name) WHERE s.Category_Name = 'Category X' GROUP BY s.subCatNumber
此查询等同于
SELECT MAX(case when
categories.Category_Name = 'Category 1'
then categories.catDesc
else NULL end) as 'Category 1',
MAX(case when
categories.Category_Name = 'Category 2'
then categories.Subcategory_Name
else NULL end) as [Category 2],
MAX(case when
categories.Category_Name = 'Category 3'
then categories.Subcategory_Name
else NULL end) as [Category 3],
MAX(case when
categories.Category_Name = 'Category 4'
then categories.Subcategory_Name
else NULL end) as [Category 4]
FROM (
SELECT pc.Category_Name
, ROW_NUMBER() OVER (PARTITION BY pc.Category_Id ORDER BY psc.Subcategory_Id) AS subCatNumber
, psc.Subcategory_Name
FROM PRIMARY_CATEGORY pc
INNER JOIN PRIMARY_SUBCATEGORY psc ON psc.Category_Id = pc.Category_Id
) categories
GROUP BY subCatNumber