在 SQL 服务器中将 Pivot 与 Join 结合使用而不进行汇总
Using Pivot with Join Without Summarizing in SQL Server
我在 SQL(版本 2008 R2)中有这个 3 table
Table student
student_id | student_name|
--------------------------
1 | john |
2 | robert |
3 | jenna |
... | ... |
Table subject
subject_id|subject_name |
-------------------------
1 |Mathematics |
2 |Biology |
table score
student_id|subject_id|score|
----------------------------
1 |1 |10 |
1 |2 |8 |
2 |1 |6 |
2 |2 |6 |
3 |1 |8 |
3 |2 |6 |
我进行了查询,生成了这样的视图
student_name|subject_name|score|
--------------------------------
John |Mathematics |10 |
John |Biology |8 |
Robert |Mathematics |6 |
Robert |Biology |6 |
Jenna |Mathematics |8 |
Jenna |Biology |6 |
... |... |... |
但实际上,我想要这个视图
subject_name|John|Robert|Jenna|...
-------------------------------
Mathematics | 10| 6| 8|...
Biology | 8| 6| 6|...
注意:table是动态的,学生人数可以超过3人。
我试图在查询中使用 pivot 但总是产生错误。请帮忙。我坚持这个。还有谢谢。
您可以通过两种方式进行数据透视。
1.静态枢轴
如果预先知道列数或列名(即,在您的情况下 Jenna,John,Robert
。
,您应该执行此操作
SELECT subject_name,[John],[Robert],[Jenna]
FROM
(
SELECT student_name,subject_name,score
FROM #TEMP
) x
PIVOT
(
MIN(score)
FOR student_name IN ([Jenna],[John],[Robert])
) p
ORDER BY subject_name
- Click here 查看结果
2。动态枢轴
有时 student_name
中的列数或值数无法提前得知。在这种情况下,您需要使用动态数据透视表。
首先,您需要将 student_name
中的值保存在一个变量中。
DECLARE @cols NVARCHAR (MAX)
SELECT @cols = COALESCE (@cols + ',[' + student_name + ']', '[' + student_name + ']')
FROM (SELECT DISTINCT student_name FROM #TEMP) PV
ORDER BY student_name
然后应用数据透视表查询。我把里面的逻辑都写好了
DECLARE @query NVARCHAR(MAX)
SET @query = '-- This outer query forms your pivoted result
SELECT * FROM
(
-- Source data for pivoting
SELECT student_name,subject_name,score
FROM #TEMP
) x
PIVOT
(
--Defines the values in each dynamic columns
MIN(score)
-- Get the names from the @cols variable to show as column
FOR student_name IN (' + @cols + ')
) p
ORDER BY subject_name;'
EXEC SP_EXECUTESQL @query
- Click here 查看结果
我在 SQL(版本 2008 R2)中有这个 3 table
Table student
student_id | student_name|
--------------------------
1 | john |
2 | robert |
3 | jenna |
... | ... |
Table subject
subject_id|subject_name |
-------------------------
1 |Mathematics |
2 |Biology |
table score
student_id|subject_id|score|
----------------------------
1 |1 |10 |
1 |2 |8 |
2 |1 |6 |
2 |2 |6 |
3 |1 |8 |
3 |2 |6 |
我进行了查询,生成了这样的视图
student_name|subject_name|score|
--------------------------------
John |Mathematics |10 |
John |Biology |8 |
Robert |Mathematics |6 |
Robert |Biology |6 |
Jenna |Mathematics |8 |
Jenna |Biology |6 |
... |... |... |
但实际上,我想要这个视图
subject_name|John|Robert|Jenna|...
-------------------------------
Mathematics | 10| 6| 8|...
Biology | 8| 6| 6|...
注意:table是动态的,学生人数可以超过3人。 我试图在查询中使用 pivot 但总是产生错误。请帮忙。我坚持这个。还有谢谢。
您可以通过两种方式进行数据透视。
1.静态枢轴
如果预先知道列数或列名(即,在您的情况下 Jenna,John,Robert
。
SELECT subject_name,[John],[Robert],[Jenna]
FROM
(
SELECT student_name,subject_name,score
FROM #TEMP
) x
PIVOT
(
MIN(score)
FOR student_name IN ([Jenna],[John],[Robert])
) p
ORDER BY subject_name
- Click here 查看结果
2。动态枢轴
有时 student_name
中的列数或值数无法提前得知。在这种情况下,您需要使用动态数据透视表。
首先,您需要将 student_name
中的值保存在一个变量中。
DECLARE @cols NVARCHAR (MAX)
SELECT @cols = COALESCE (@cols + ',[' + student_name + ']', '[' + student_name + ']')
FROM (SELECT DISTINCT student_name FROM #TEMP) PV
ORDER BY student_name
然后应用数据透视表查询。我把里面的逻辑都写好了
DECLARE @query NVARCHAR(MAX)
SET @query = '-- This outer query forms your pivoted result
SELECT * FROM
(
-- Source data for pivoting
SELECT student_name,subject_name,score
FROM #TEMP
) x
PIVOT
(
--Defines the values in each dynamic columns
MIN(score)
-- Get the names from the @cols variable to show as column
FOR student_name IN (' + @cols + ')
) p
ORDER BY subject_name;'
EXEC SP_EXECUTESQL @query
- Click here 查看结果