2017-02-01 1 views
2

데이터를 CSV로 내보내려고하고 있고 두 번째 테이블의 여러 열을 행으로 변환해야합니다. 부팅하려면 두 번째 테이블에 고정 된 수의 행이 없지만 내 내보내기에서 고정 된 수의 필드를 사용해야합니다. 다음은 내 데이터 샘플입니다 (제 질문을 지우 길 바랍니다). StudentID에 대한 데이터 조인. MS SQL Server를 사용하여 2014 년두 테이블에서 선택하여 한 테이블의 행을 열로 변환하십시오.

사용자 표

StudentID | Last Name | First Name | Middle Name/Initial | Is Active | Grade | School Name 
___________________________________________________________________________________________ 
1   | Pinciati | Donna  | J     | Y   | 10 | High   
2   | Hyde  | Steven  | K     | Y   | 11 | Middle  
3   | Foreman | Eric  | L     | Y   | 10 | High   
4   | Kelso  | Michael | G     | Y   | 11 | High   

경고 표

ID | StudentID | Alert Type  | Alert Comments 
___________________________________________________ 
1 | 2   | Asthma   | NULL 
2 | 2   | Allergy   | Bee Sting 
3 | 3   | Allergy   | Peanut 
4 | 4   | Medial Equipment | Crutches 
5 | 4   | Allergy   | Bee Sting 
설정은 다음과 같이 할 필요가

내 결과 :

StudentID | Last Name | First Name | Middle Name/Initial | Is Active | Grade | School Name | Alert Type 1  | Alert Comments 1 | Alert Type 2 | Alert Comments 2 | Alert Type 3 | Alert Comments 3  
1   | Pinciati | Donna  | J     | Y   | 10 | High  | NULL    | NULL    | NULL   | NULL    | NULL   | NULL 
2   | Hyde  | Steven  | K     | Y   | 11 | Middle  | Asthma   | NULL    | Allergy  | Bee Sting  | NULL   | NULL 
3   | Foreman | Eric  | L     | Y   | 10 | High  | Allergy   | Peanut   | NULL   | NULL    | NULL   | NULL 
4   | Kelso  | Michael | G     | Y   | 11 | High  | Medial Equipment | Crutches   | Allergy  | Bee Sting  | NULL   | NULL 

을 내가 더 경고를 알고 내 데이터에있는 것보다, 비록 내가 바꿀 수없는 템플릿의 필드가 고정되어 있기 때문에 NULL이더라도 모든 경고 필드를 채우십시오.
도와주세요 Obi-Wan Kenobi; 너는 내 유일한 희망이야! , 당신은 StudentId에 가입하고 행 번호 (rn)에 따라 골재 남아 있습니다 row_number()와 하위 쿼리 또는 common table expression를 사용

+0

학생당 3 건의 경고가 있습니까? 아니면 실제 상황에서 더 많은 열이 필요합니까? – Fritz

+0

내 최종 수출에는 경고장 10 개가 있어야합니다.하지만 현재 학생이 가지고있는 대부분의 경고는 6입니다. 제 질문에 데이터 피로를 줄이지 않기 위해 축약했습니다. – user2625842

답변

1

.

select 
    s.StudentID 
    , s.LastName 
    , s.FirstName 
    , s.MiddleName 
    , s.IsActive 
    , s.Grade  
    , s.SchoolName 
    , AlertType_1  = max(case when rn = 1 then al.AlertType else null end) 
    , AlertComments_1 = max(case when rn = 1 then al.AlertComments else null end) 
    , AlertType_2  = max(case when rn = 2 then al.AlertType else null end) 
    , AlertComments_2 = max(case when rn = 2 then al.AlertComments else null end) 
    , AlertType_3  = max(case when rn = 3 then al.AlertType else null end) 
    , AlertComments_3 = max(case when rn = 3 then al.AlertComments else null end) 
    from dbo.student as s 
    left join (
     select StudentId, AlertType, AlertComments 
     , rn = row_number() over (
      partition by StudentId 
      order by AlertType, AlertComments 
     ) 
     from dbo.Alert 
    ) as al on s.StudentId = al.StudentId 
    group by 
    s.StudentID 
    , s.LastName 
    , s.FirstName 
    , s.MiddleName 
    , s.IsActive 
    , s.Grade  
    , s.SchoolName 

rextester : http://rextester.com/YMDQI47779

테스트 설정 :

create table Student (
    StudentID int 
    , LastName nvarchar(32) 
    , FirstName nvarchar(32) 
    , MiddleName nvarchar(32) 
    , IsActive char(1) 
    , Grade  int 
    , SchoolName nvarchar(32) 
); 
insert into dbo.Student values 
(1,'Pinciati','Donna','J','Y',10,'High') 
,(2,'Hyde','Steven','K','Y',11,'Middle') 
,(3,'Foreman','Eric','L','Y',10,'High') 
,(4,'Kelso','Michael','G','Y',11,'High'); 

create table dbo.Alert (
    ID   int 
    , StudentID  int 
    , AlertType  nvarchar(64) 
    , AlertComments nvarchar(64) 
); 
insert into dbo.Alert values 
(1,2,'Asthma','NULL') 
,(2,2,'Allergy','Bee Sting') 
,(3,3,'Allergy','Peanut') 
,(4,4,'Medial Equipment','Crutches') 
,(5,4,'Allergy','Bee Sting'); 

쿼리

;with al as (
    select StudentId, AlertType, AlertComments 
    , rn = row_number() over (
     partition by StudentId 
     order by AlertType, AlertComments 
    ) 
from dbo.Alert 
) 
select 
    s.StudentID 
    , s.LastName 
    , s.FirstName 
    , s.MiddleName 
    , s.IsActive 
    , s.Grade  
    , s.SchoolName 
    , AlertType_1  = max(case when rn = 1 then al.AlertType else null end) 
    , AlertComments_1 = max(case when rn = 1 then al.AlertComments else null end) 
    , AlertType_2  = max(case when rn = 2 then al.AlertType else null end) 
    , AlertComments_2 = max(case when rn = 2 then al.AlertComments else null end) 
    , AlertType_3  = max(case when rn = 3 then al.AlertType else null end) 
    , AlertComments_3 = max(case when rn = 3 then al.AlertComments else null end) 
    from dbo.student as s 
    left join al on s.StudentId = al.StudentId 
    group by 
    s.StudentID 
    , s.LastName 
    , s.FirstName 
    , s.MiddleName 
    , s.IsActive 
    , s.Grade  
    , s.SchoolName 

결과 :

+-----------+----------+-----------+------------+----------+-------+------------+-------------+-----------------+------------------+-----------------+-------------+-----------------+ 
| StudentID | LastName | FirstName | MiddleName | IsActive | Grade | SchoolName | AlertType_1 | AlertComments_1 | AlertType_2 | AlertComments_2 | AlertType_3 | AlertComments_3 | 
+-----------+----------+-----------+------------+----------+-------+------------+-------------+-----------------+------------------+-----------------+-------------+-----------------+ 
|   1 | Pinciati | Donna  | J   | Y  | 10 | High  | NULL  | NULL   | NULL    | NULL   | NULL  | NULL   | 
|   2 | Hyde  | Steven | K   | Y  | 11 | Middle  | Allergy  | Bee Sting  | Asthma   | NULL   | NULL  | NULL   | 
|   3 | Foreman | Eric  | L   | Y  | 10 | High  | Allergy  | Peanut   | NULL    | NULL   | NULL  | NULL   | 
|   4 | Kelso | Michael | G   | Y  | 11 | High  | Allergy  | Bee Sting  | Medial Equipment | Crutches  | NULL  | NULL   | 
+-----------+----------+-----------+------------+----------+-------+------------+-------------+-----------------+------------------+-----------------+-------------+-----------------+ 
+0

위대한, 감사 SqlZim 효과가! – user2625842

0

이것을 시도하십시오 :

with alert as (
select f0.*, 
row_number() over(partition by f0.studientID order by f0.ID) rang from Alert_TAble f0 
) 
select f0.*, 
     f1.AlertType as AlertType1, f1.AlertComment as AlertComment1, 
     f2.AlertType as AlertType2, f2.AlertComment as AlertComment2, 
     f3.AlertType as AlertType3, f3.AlertComment as AlertComment3 
from User_Table f0 
left outer join alert f1 on f0.studientID=f1.studientID and f1.rang=1 
left outer join alert f2 on f0.studientID=f2.studientID and f2.rang=2 
left outer join alert f3 on f0.studientID=f3.studientID and f3.rang=3 
관련 문제