2016-06-27 17 views
-4

シンプルなクロスによってSQL Serverで2つの列を行として表示するにはどうすればよいですか?

enter image description here

+4

はStackOverflowのへようこそ。 [ask]をお読みください。 –

+1

ピボットを使用して列を列に変換するだけです – Jesus

+0

[複数の列集約を持つSQL Serverピボットテーブル]の複製が可能です(http://stackoverflow.com/questions/14694691/sql-server-pivot-table-with-multiple-column-集約) –

答えて

0

が適用SQL Serverで行に2列に変換

DECLARE @Table1 TABLE 
    (ID int,installdate varchar(20),uninstalldate varchar(20)) 
; 

INSERT INTO @Table1 
    (ID,installdate,uninstalldate) 
VALUES 
    (1,'15/06/2016','18/06/2016'), 
    (2,'20/06/2016','25/06/2016') 

スクリプト:

select COL AS [Instal OR Uninstall],VAL AS [Date] from @Table1 
     CROSS APPLY 
    (VALUES 
    ('installdate',installdate), 
    ('uninstalldate',installdate)) 
    CS(COL,VAL) 
0

シンプルUNPIVOTやるべき事:

SELECT [DATES], 
     [VALUES] 
FROM MyTable 
UNPIVOT (
    [VALUES] FOR [DATES] IN (InstallDate,UnInstallDate) 
) as unpvt 

出力:

DATES   VALUES 
InstallDate  2016-06-15 
UnInstallDate 2016-06-18 
InstallDate  2016-06-20 
UnInstallDate 2016-06-25 
+0

ありがとうございます。それは本当に素晴らしい仕事です。 – Rohit

+0

私の喜び!答えが役に立ったなら、upvote/acceptを自由に感じてください:)それはあなたが*ありがとうございました〜と言う最善の方法です:) – gofr1

0

あなたが行に列をUNPIVOTことができます。

DECLARE @Data TABLE (
    Id INT, 
    InstallDate DATE, 
    UnInstallDate DATE 
) 
INSERT @Data VALUES (1,'6/15/2016', '6/18/2016'),(2,'6/20/2016', '6/25/2016') 

SELECT 
    ActivityType, 
    ActivityDate 
FROM @Data 
    UNPIVOT (ActivityDate FOR ActivityType IN (InstallDate, UnInstallDate)) T 

これは、次の行を生成します。

ActivityType    ActivityDate 
------------------------- ------------ 
InstallDate    2016-06-15 
UnInstallDate    2016-06-18 
InstallDate    2016-06-20 
UnInstallDate    2016-06-25 
関連する問題