Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于数据透视结果计数的T-SQL数据透视_Sql_Sql Server_Tsql_Pivot - Fatal编程技术网

基于数据透视结果计数的T-SQL数据透视

基于数据透视结果计数的T-SQL数据透视,sql,sql-server,tsql,pivot,Sql,Sql Server,Tsql,Pivot,我有以下数据,我想透视,并根据透视结果获得计数 DECLARE @tempMusicSchoolStudent TABLE (school VARCHAR(50), studentname VARCHAR(50), instrumentname VARCHAR(255), expertise INT) INSERT INTO @tempMusicSchoolStudent(school, studentname, instrumentname, expertise) SELECT '

我有以下数据,我想透视,并根据透视结果获得计数

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
选择
t、 学校,t.仪器名称,t.专业知识,
t1.仪器名称,t1.专业知识,
计数(不同的t.studentname)[DistinctStudentCount]
从…起
@临时音乐学校学生
左连接
@临时音乐学校学生t1在t1上。学校=t学校
和t1.studentname=t.studentname
和t.instrumentname t1.instrumentname
分组
t、 学校,t.instrumentname,t.Experience,t1.instrumentname,t1.Experience
订购人
t、 学校,t.instrumentname,t.Experience,t1.instrumentname,t1.Experience

如果有人对我如何能以一种更干净的方式做到这一点有任何想法,而不是让我自己加入这个表格,我将不胜感激。谢谢。

您只需要条件聚合:

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
行的来源。

您只需要条件聚合:

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
行来自何处。

您可以尝试将其动态化以用于多个仪器

输出:

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

您可以尝试将其动态化,以用于多种仪器

输出:

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

这是我一直在寻找的解决方案,我必须使用unpivot+pivot

我正在努力解决的实际问题是为正在旋转的列选择多个值,而不是最大值

因此,在本例中,我希望在给定的“仪器专业知识”列下有多个“专业知识”数字。不仅仅是该仪器的最高专业水平

理解该解决方案的第一个关键是pivot语句正在对所选列执行隐式分组。因此,为了在数据透视列下获得多个值,您必须通过包含某种类型的密集秩/秩/行数来保持分组列的完整性。这基本上表示正在以数据透视的列的值的更改,然后数据透视将其包含在隐式组中,这将导致在数据透视列中获得多个值,而不仅仅是最大值

因此,在下面的代码中,“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]

这是我一直在寻找的解决方案,我必须使用unpivot+pivot

我正在努力解决的实际问题是为正在旋转的列选择多个值,而不是最大值

因此,在本例中,我希望在给定的“仪器专业知识”列下有多个“专业知识”数字。不仅仅是该仪器的最高专业水平

理解该解决方案的第一个关键是pivot语句正在对所选列执行隐式分组。因此,为了在数据透视列下获得多个值,您必须通过包含某种类型的密集秩/秩/行数来保持分组列的完整性。这基本上表示正在以数据透视的列的值的更改,然后数据透视将其包含在隐式组中,这将导致在数据透视列中获得多个值,而不仅仅是最大值

因此,在下面的代码中,“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]

这是我需要的一种笛卡尔结果。举个例子,我得到的第一行结果是,告诉我福斯特学院每一个既会弹吉他又会弹键盘的学生的数量,他们的专业水平是5把吉他,专业水平是8把键盘。然后,结果将沿着福斯特所有吉他专业知识和交叉连接的线,这将是福斯特所有键盘专业知识,并得到这些乐器的这两种专业知识的不同学生的计数。因此,期望结果第2行中的空值是因为福斯特有一个孩子只弹吉他,而不是键盘,专业知识是10。这是我需要的一种笛卡尔结果。举个例子,我得到的第一行结果是,告诉我福斯特学院每一个既会弹吉他又会弹键盘的学生的数量,他们的专业水平是5把吉他,专业水平是8把键盘。然后,结果将沿着福斯特所有吉他专业知识和交叉连接的线,这将是福斯特所有键盘专业知识,并得到这些乐器的这两种专业知识的不同学生的计数。因此,期望结果第2行中的空值是因为福斯特有一个孩子只弹吉他,而不是键盘,专业知识为10。在输出中,行1和行3之间有什么区别?没有区别,但这是数据的预期模式。如果我可以删除这些重复,那就太好了,但是用户现在还可以,因为他们想要数据的完全连接。输出中的第1行和第3行之间有什么区别?没有区别,但这是数据的预期模式。如果我可以删除这些重复,那就太好了,但是用户现在还可以,因为他们想要数据的完整连接。