0

에서 열 이름 난 SQL 서버의 행에 열을 변환 할 :행에 열을 변환 및 SQL Server

Id Value Jan1 Jan2 
---------------------- 
1  2  25 35 
2  5  45 45 

결과는해야

Id Value Month 1 2 
---------------------- 
1 2  Jan 25 35 
2 5  Jan 45 45 
나는이 결과를 얻을 수있는 방법

?

select Id, Value, 'Jan' as [month], Jan1 as [1], Jan2 as [2] 
from t; 

당신은 기본적으로 그냥 출력에 다른 열을 추가 : 사람은 누구나

+0

샘플 및 원하는 결과가 약간 희박합니다. 요즘이나 연중 계속되는 날입니까? 여러 달이 있니? –

답변

1

당신이 묻는 것은 조금 이상하게 보입니다.

+----+-------+------+------+------+------+ 
| Id | Value | Jan1 | Jan2 | Feb1 | feb2 | 
+----+-------+------+------+------+------+ 
| 1 |  2 | 25 | 35 | 15 | 28 | 
| 2 |  5 | 45 | 45 | 60 | 60 | 
+----+-------+------+------+------+------+ 

트랜스 단지 월 부분 :

select Id, Value, MonthName, MonthValue1, MonthValue2 
from t 
    cross apply (values ('Jan',Jan1,Jan2),('Feb',Feb1,Feb2) 
    ) v (MonthName,MonthValue1,MonthValue2) 

반환 :

내가 Feb1Feb2에 대한 열을 포함하여 예제를 확장한다면, 나는이에서 열을 전치 두 가지 옵션 참조
+----+-------+-----------+-------------+-------------+ 
| Id | Value | MonthName | MonthValue1 | MonthValue2 | 
+----+-------+-----------+-------------+-------------+ 
| 1 |  2 | Jan  |   25 |   35 | 
| 1 |  2 | Feb  |   15 |   28 | 
| 2 |  5 | Jan  |   45 |   45 | 
| 2 |  5 | Feb  |   60 |   60 | 
+----+-------+-----------+-------------+-------------+ 

월 열을 다음과 같이 완전히 바꾸십시오.

select Id, Value, MonthName, MonthValue 
from t 
    cross apply (values ('Jan1',Jan1),('Jan2',Jan2),('Feb1',Feb1),('Feb2',Feb2) 
    ) v (MonthName,MonthValue) 

반환 :

+----+-------+-----------+------------+ 
| Id | Value | MonthName | MonthValue | 
+----+-------+-----------+------------+ 
| 1 |  2 | Jan1  |   25 | 
| 1 |  2 | Jan2  |   35 | 
| 1 |  2 | Feb1  |   15 | 
| 1 |  2 | Feb2  |   28 | 
| 2 |  5 | Jan1  |   45 | 
| 2 |  5 | Jan2  |   45 | 
| 2 |  5 | Feb1  |   60 | 
| 2 |  5 | Feb2  |   60 | 
+----+-------+-----------+------------+ 

rextester 데모 : http://rextester.com/KZV45690 여기

1

이는 것처럼 보일 것 도와주세요.

숫자를 열 이름으로 사용하거나 month과 같은 SQL Server 키워드를 사용하지 않는 것이 좋습니다.

+0

, 월 열을 사용할 수 없습니다. SELECT 절의 월 열을 제거하십시오. – Mansoor

0

하면 최대 365 개 필드

Declare @YourTable table (Id int,Value int,Jan1 int,Jan2 int,Feb1 int, Feb2 int) 
Insert Into @YourTable values 
(1,  2,  25, 35, 100, 101), 
(2,  5,  45, 45, 200, 201) 


Select [Id],[Value],[Month],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31] 
From (
     Select A.Id 
       ,A.Value 
       ,[Month] = Left(C.Item,3) 
       ,[Col]  = substring(C.Item,4,5) 
       ,[Measure] = C.Value 
     From @YourTable A 
     Cross Apply (Select XMLData = cast((Select A.* for XML Raw) as xml)) B 
     Cross Apply (
         Select Item = attr.value('local-name(.)','varchar(100)') 
           ,Value = attr.value('.','int') 
         From B.XMLData.nodes('/row') as A(r) 
         Cross Apply A.r.nodes('./@*') AS B(attr) 
         Where attr.value('local-name(.)','varchar(100)') not in ('ID','Value') 
        ) C 
    ) A 
Pivot (sum(Measure) For [Col] in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31])) p 

반환 지정할 필요가 없습니다 옵션입니다

enter image description here