簡體   English   中英

帶動態列的SQL查詢(PIVOT)

[英]SQL query with dynamic columns (PIVOT)

我正在嘗試編寫一個將轉換此表的SQL查詢:

Start_time    End_time   Instructor    Student  
9:00          9:35       Joe Bob       Andrew
9:00          9:35       Joe Bob       Smith
9:00          9:35       Roberto       Andy
10:00         10:35      Joe Bob       Angelica
11:00         11:20      Roberto       Bob

進入這樣的事情:

Instructor    9:00              10:00         11:00
Joe Bob       Andrew, Smith     Angelica      NULL
Roberto       Andy              NULL          Bob

我認為這是某種PIVOT命令,但我不確定如何編寫SQL查詢。 時間都是動態生成的,所以如果查詢會動態生成第二個表中的列名,我會更喜歡它(例如,如果原始表包含11:30的額外開始時間,那么應該有一個新列為11 :結果中的30)。

提前謝謝你,我已經玩了一段時間但是無法自己動手。 我可以提供SQL INSERT命令,以便在必要時為您提供完整數據。

編輯:將此select語句的結果作為VIEW特別有用。 謝謝!

編輯2:生成第一個表的視圖的代碼是:

CREATE VIEW schedule_view AS SELECT RTRIM(SUBSTRING(students.schedule_first_choice, 1, 5)) AS start_time, RTRIM(SUBSTRING(students.schedule_first_choice, -10, 5) AS end_time, CONCAT(instructors.first_name, ' ', instructors.last_name) AS instructor_name, 
    CONCAT(students.first_name, ' ', students.last_name) AS student_name , students.swim_america_level as class 
    FROM students, instructors WHERE students.instructor_id = instructors.instructor_id AND students.season = 
    (SELECT constant_value FROM constants WHERE constant_name = 'season') AND students.year = 
    (SELECT constant_value FROM constants WHERE constant_name = 'year')
SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'GROUP_CONCAT(case when Start_time = ''',
      Start_time,
      ''' then Student ELSE NULL end) AS ',
      CONCAT('`',Start_time,'`')
    )
  ) INTO @sql
FROM Table1;

SET @sql = CONCAT('SELECT Instructor, ', @sql, ' 
                   FROM Table1 
                   GROUP BY Instructor');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

在這些所有查詢和測試SQL SEREVR

在PIVOT中使用靜止柱

 Select * from  
 (
      select Instructor,Start_time, STUFF((select ',' + student from Table1 a where         
      a.Start_time = b.Start_time and
      a.Instructor=b.Instructor for xml     path('')),1,1,'') as student
      from table1 b ) x 
 PIVOT 
 (
      max(Student) 
      for start_time IN ([9:00],[10:00], [11:00])
 ) p

使用臨時表動態創建PIVOT TABLE

 Declare @tab nvarchar(max) 
 Declare @pivottab nvarchar(max)

 Create table #Table2 (     
        instructor varchar(100),    
        student    varchar(100),    
        start_time varchar(10) 
 ); 

 insert into #Table2 (instructor,student,start_time) 
 select 
       Instructor,  
       STUFF((Select ','+student 
             from Table1 as a
         Where 
             a.Start_time = b.Start_time and 
                 a.Instructor=b.Instructor
             for xml path('')),1,1,''),
       Start_time  
 from table1 as b

 select @tab = coalesce(@tab + ',['+start_time+']' ,'['+start_time+']')  
 from #Table2  
 group by Start_time

 set @pivottab = 
     N' select * from  (
                        select Instructor, Start_time, student from #Table2 
                       ) x 
        PIVOT (
              max(Student) for start_time IN ('+@tab+') 
        ) p'

 execute(@pivottab)

 if exists(select * from #table2)
 Begin
Drop table #table2 
 End

動態創建PIVOT TABLE

Declare @tab nvarchar(max)
Declare @pivottab nvarchar(max)

Select @tab = coalesce(@tab + ',['+start_time+']' , '['+start_time+']') from Table1
group by Start_time

set @pivottab = N'
select *
from 
(
  select Instructor,Start_time,STUFF((select '+ char(39)+char(44)+char(39) + '+ a.student from Table1 as a
where a.Start_time = b.Start_time and a.Instructor=b.Instructor for xml path('''')),1,1,'''') 
as Student from table1 as b

) x
PIVOT
(
  max(Student)
  for start_time IN ('+@tab+')
) p'

execute(@pivottab)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM