2013-12-15 5 views
2

나는 다음과 같습니다.TSQL 쿼리 - 열을 행으로

USE [tempdb] 
GO 

CREATE TABLE #Table (id int, row# int, Info varchar(10), 
         clm11 varchar(10), 
         clm21 varchar(10), 
         clm31 varchar(10), 
         clm41 varchar(10), 
         clm51 varchar(10), 
          clm12 varchar(10), 
          clm22 varchar(10), 
          clm32 varchar(10), 
          clm42 varchar(10), 
          clm52 varchar(10), 
           clm13 varchar(10), 
           clm23 varchar(10), 
           clm33 varchar(10), 
           clm43 varchar(10), 
           clm53 varchar(10)) 
INSERT INTO #Table 
SELECT 1, 100, 'Text', 'Col1', 'Col2', 'Col3', 'Col4', 'Col5','Col11', 'Col12', 'Col13', 'Col14', 'Col15','Col21', 'Col22', 'Col23', 'Col24', 'Col25' 
UNION ALL 
SELECT 2, 100, 'Text', 'Col1', 'Col2', 'Col3', 'Col4', 'Col5', NULL, NULL, NULL, NULL, NULL,'Col21', 'Col22', 'Col23', 'Col24', 'Col25' 
UNION ALL 
SELECT 3, 100, 'Text', 'Col1', 'Col2', 'Col3', 'Col4', 'Col5','Col11', 'Col12', 'Col13', 'Col14', 'Col15', NULL, NULL, NULL, NULL, NULL 
UNION ALL 
SELECT 4, 100, 'Text', 'Col1', 'Col2', 'Col3', 'Col4', 'Col5', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 
UNION ALL 
SELECT 5, 100, 'Text', NULL, NULL, NULL, NULL, NULL, 'Col11', 'Col12', 'Col13', 'Col14', 'Col15', 'Col21', 'Col22', 'Col23', 'Col24', 'Col25' 
UNION ALL 
SELECT 6, 100, 'Text', NULL, NULL, NULL, NULL, NULL, 'Col11', 'Col12', 'Col13', 'Col14', 'Col15', NULL, NULL, NULL, NULL, NULL 
UNION ALL 
SELECT 7, 100, 'Text', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'Col21', 'Col22', 'Col23', 'Col24', 'Col25' 

SELECT * FROM #Table 


/* Desired output 

ID Clm1 Clm2 Clm3 Clm4 Clm5 
1 Col1 Col2 Col3 Col4 Col5 
1 Col11 Col12 Col12 Col14 Col15 
1 Col21 Col22 Col23 Col24 Col25 
2 Col1 Col2 Col3 Col4 Col5 
2 Col21 Col22 Col23 Col24 Col25 
3 Col1 Col2 Col3 Col4 Col5 
3 Col11 Col12 Col13 Col14 Col15 
4 Col1 Col2 Col3 Col4 Col5 
5 Col11 Col12 Col13 Col14 Col15 
5 Col21 Col22 Col23 Col24 Col25 
6 Col11 Col12 Col13 Col14 Col15 
7 Col21 Col22 Col23 Col24 Col25 

*/ 


--- My Try 
SELECT Id, FieldCode, FieldValue 

FROM (SELECT id, clm11, clm21, clm31, clm41, clm51, clm12, clm22, clm32, clm42, clm52, clm13, clm23, clm33, clm43, clm53 FROM #Table) MyTable 

UNPIVOT 
(FieldCode FOR FieldCodes IN (clm11, clm21, clm31, clm41, clm12, clm22, clm32, clm42, clm13, clm23, clm33, clm43))AS CODE 

UNPIVOT 
(FieldValue FOR FieldValues IN (clm51, clm52, clm53))AS FieldValues 
WHERE RIGHT(FieldCodes,1) = RIGHT(FieldValues,1) 


DROP TABLE #Table 

내가 ID와 Clm11가 Clm53에 (5 열 세트)를 가지고하려고하고 세트 (5 골의)의 값 (null 이외의 설정)이있는 경우, ID와 함께 별도의 기록에 넣어 "원하는 출력"섹션에 나와 있습니다. 나는 15 열 (Clm11-Clm51, Clm12-Clm52 및 Clm13-Clm53)을 보였으 나 더 많이 설정 될 수 있습니다.

도와주세요.

TEMP 업데이트 :

select * 
from (select id, 
      (case when n = 1 then clm11 
        when n = 2 then clm21 
        when n = 3 then clm31 
        when n = 4 then clm41 
        when n = 5 then clm51 
       end) as col1, 
       (case when n = 1 then clm12 
        when n = 2 then clm22 
        when n = 3 then clm32 
        when n = 4 then clm42 
        when n = 5 then clm52 
       end) as col2, 
       (case when n = 1 then clm13 
        when n = 2 then clm23 
        when n = 3 then clm33 
        when n = 4 then clm43 
        when n = 5 then clm53 
       end) as col3 
     from #table t cross join 
      (select 1 as n union all select 2 union all select 3 union all select 4 union all select 5) n 
    ) t 
where coalesce(col1, col2, col3) is not null; 

답변

3

난 당신이 union all 단지 일련의이 작업을 수행 할 수 있다고 생각 :

select id, Col11, Col12, Col13, Col14, Col15 
from #table 
where col11 is not null or col12 is not null or col13 is not null or 
     col14 is not null or col15 is not null 
union all 
select id, Col21, Col22, Col23, Col24, Col25 
from #table 
where col21 is not null or col22 is not null or col23 is not null or 
     col24 is not null or col25 is not null 
union all 
select id, Col31, Col32, Col33, Col34, Col35 
from #table 
where col31 is not null or col32 is not null or col33 is not null or 
     col34 is not null or col35 is not null; 

편집 :

예, 이것을 처리하는 방법이 단일 테이블 스캔. 아이디어는 별도의 카운터 테이블과 많은 사례 문장을 사용하는 것입니다. 여기에 코드 구조입니다 :이 열을 정의하는 하위 쿼리를 사용

select * 
from (select id, 
      (case when n = 1 then col11 
        when n = 2 then col21 
        when n = 3 then col31 
       end) as col1, 
      . . . 
     from #table t cross join 
      (select 1 as n union all select 2 union all select 3) n 
    ) t 
where coalesce(col1, col2, col3, col4, col5) is not null; 

, 그래서 where 절은 추한 case 논리를 반복 할 필요가 없습니다.

편집 2 :

select * 
from (select id, 
      (case when n = 1 then clm11 
        when n = 2 then clm12 
        when n = 3 then clm13 
       end) as col1, 
       (case when n = 1 then clm21 
        when n = 2 then clm23 
        when n = 3 then clm23 
       end) as col2, 
       (case when n = 1 then clm31 
        when n = 2 then clm32 
        when n = 3 then clm33 
       end) as col3, 
       (case when n = 1 then clm41 
        when n = 2 then clm42 
        when n = 3 then clm43 
       end) as col4, 
       (case when n = 1 then clm51 
        when n = 2 then clm52 
        when n = 3 then clm53 
       end) as col5 
     from t cross join 
      (select 1 as n union all select 2 union all select 3 union all select 4 union all select 5) n 
    ) t 
where coalesce(col1, col2, col3, col4, col5) is not null; 

는 SQL 바이올린은 here입니다 :

전체 쿼리는 세 개의 절을 각보다는 세 case 문에 case 문처럼 보인다.

+0

필요에 따라 작동합니다. 그러나, 나는 이것이 적어도 3 번 테이블을 "스캔"한다고 생각한다. (왜냐하면 지금 우리는 3 세트의 5 컬럼을 가지고 있기 때문이다.) 25 세트 (125 열)가 있다면 테이블을 25 번 스캔합니다. 이 sln은 단순히 아름답고 아름답게 단순하지만 피벗을 사용하거나 쿼리가 조금 더 작은 곳을 사용할 수 있습니까? – 007

+0

귀하의 최신 방법을 검토 한 후에 혼란 스럽습니다. op의 TEMP Update 섹션을 검토하여 내가 시도하고있는 것을 확인하십시오 ... 내가 당신을 쫓고 있다고 생각하지 마십시오. – 007

+0

@ user1569220 . . 이 쿼리는 다중 테이블 스캔을 피하기 위해 내가 생각하고있는 것입니다. –