我有以下数据,我想根据轮回结果进行调整并获得计数。
DECLARE @tempMusicSchoolStudent TABLE
(school VARCHAR(50),
studentname VARCHAR(50),
instrumentname VARCHAR(255),
expertise INT)
INSERT INTO @tempMusicSchoolStudent(school, studentname, instrumentname, expertise)
SELECT 'Foster','Matt','Guitar','10'
UNION
SELECT 'Foster','Jimmy','Guitar','5'
UNION
SELECT 'Foster','Jimmy','Keyboard','8'
UNION
SELECT 'Foster','Ryan','Keyboard','9'
UNION
SELECT 'Midlothean','Kyle','Keyboard','10'
UNION
SELECT 'Midlothean','Mary','Guitar','4'
UNION
SELECT 'Midlothean','Mary','Keyboard','7'
原始数据:
我希望结果看起来像下面的数据....
我使用下面的sql查询获得了这些数据。这个查询的问题是我有一个动态数量的工具(为简单起见,我在这个例子中只显示了2个)。我想使用pivot,因为它将是更干净的动态sql。否则,我必须动态地将表连接到每个工具的自身。
SELECT
t.school, t.instrumentname, t.expertise,
t1.instrumentname, t1.expertise,
COUNT(DISTINCT t.studentname) [DistinctStudentCount]
FROM
@tempMusicSchoolStudent t
LEFT JOIN
@tempMusicSchoolStudent t1 ON t1.school = t.school
AND t1.studentname = t.studentname
AND t.instrumentname <> t1.instrumentname
GROUP BY
t.school, t.instrumentname, t.expertise, t1.instrumentname, t1.expertise
ORDER BY
t.school, t.instrumentname, t.expertise, t1.instrumentname, t1.expertise
如果有人对如何以更清洁的方式做到这一点有任何想法,而不是动态地将表连接到自身,那将非常感激。感谢。
答案 0 :(得分:1)
您只需要条件聚合:
SELECT t.school, t.instrumentname, t.expertise, t.instrumentname,
COUNT(DISTINCT t.studentname) as DistinctStudentCount
FROM @tempMusicSchoolStudent t
GROUP BY t.school, t.instrumentname, t.expertise, t.instrumentname;
您的行包含NULL
个值。目前尚不清楚这些来自哪里。你的问题集中在一些“转动”的概念,似乎你只需要聚合。但它没有解释NULL
行的来源。
答案 1 :(得分:1)
您可以尝试将其设置为多重乐器的动态。 Refer
;with cte
as
(
SELECT * from
(SELECT * FROM @tempMusicSchoolStudent t) x
PIVOT
(MAX(expertise) FOR instrumentname in ([Guitar], [Keyboard])) y
)
SELECT school, studentname,
expertise = case when Guitar is not null then 'Guitar' else NULL end,
Guitar AS instrumentname,
expertise = case when Keyboard is not null then 'Keyboard' else NULL end,
Keyboard AS instrumentname,
count(distinct studentname) AS [DistinctStudentCount]
from cte
group by school,studentname, Guitar, Keyboard
输出:
Foster Jimmy Guitar 5 Keyboard 8 1
Foster Matt Guitar 10 NULL NULL 1
Foster Ryan NULL NULL Keyboard 9 1
Midlothean Kyle NULL NULL Keyboard 10 1
Midlothean Mary Guitar 4 Keyboard 7 1
答案 2 :(得分:0)
这是我正在寻找的解决方案,我不得不使用unpivot + pivot。
我正在努力解决的问题是为正在旋转的列选择多个值,而不是最大值。
因此,在这种情况下,我想在给定的“仪器专业知识”栏目下获得多个“专业知识”数字。不仅仅是该仪器的最大专业知识。
理解解决方案的第一个关键是pivot语句正在对所选列进行隐式组。因此,为了在您的透视列下实现多个值,您必须通过包含某种类型的dense_rank / rank / row_number来保持要分组的列的完整性。这基本上表示您正在旋转的列的值的变化,然后由枢轴正在执行的隐式组中包含,这导致在透视列中获取多个值,而不仅仅是最大值。
因此,在下面的代码中,“expertisenum”专栏保持专业知识数据的完整性。
DECLARE @tempMusicSchoolStudent TABLE
(school VARCHAR(50),
studentname VARCHAR(50),
instrumentname VARCHAR(255),
expertise INT)
INSERT INTO @tempMusicSchoolStudent(school, studentname, instrumentname, expertise)
SELECT 'Foster','Matt','Guitar','10'
UNION
SELECT 'Foster','Jimmy','Guitar','5'
UNION
SELECT 'Foster','Jimmy','Keyboard','8'
UNION
SELECT 'Foster','Ryan','Keyboard','9'
UNION
SELECT 'Midlothean','Kyle','Keyboard','10'
UNION
SELECT 'Midlothean','Mary','Guitar','4'
UNION
SELECT 'Midlothean','Mary','Keyboard','7'
SELECT school, [Guitar expertise], [Keyboard expertise], COUNT(*) [Count]
FROM
(
SELECT school,[expertiseNum],
CASE WHEN [Columns]='expertise' THEN instrumentname + ' expertise'
END [Columns1], [Values] AS [Values1]
FROM
(
SELECT school, studentname, instrumentname, DENSE_RANK() OVER(PARTITION BY school,instrumentname ORDER BY expertise) AS [expertiseNum],
CONVERT(VARCHAR(255),expertise) AS [expertise]
FROM @tempMusicSchoolStudent
) x
UNPIVOT (
[Values] FOR [Columns] IN ([expertise])
) unpvt
) p
PIVOT (
MAX([Values1]) FOR [Columns1] IN ([Guitar expertise], [Keyboard expertise])
) pvt
GROUP BY school,[Guitar expertise], [Keyboard expertise]