2015-09-08 103 views
1

我有一個表,看起來像這樣:摺疊/合併列在TSQL查詢

enter image description here

我怎麼會從該表中選擇使得多個子碼列轉化爲行?不知道這是否是PIVOT問題。請指教。

所需的採樣輸出:

enter image description here

附件是SQL供您參考的數據插入腳本。

USE [TEST] 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
SET ANSI_PADDING ON 
GO 
CREATE TABLE [dbo].[SubCodeReport](
    [ S-ID] [varchar](50) NULL, 
    [AGE] [varchar](50) NULL, 
    [SchoolCode] [varchar](50) NULL, 
    [SubCode] [varchar](50) NULL 
) ON [PRIMARY] 

GO 
SET ANSI_PADDING OFF 
GO 
/****** Object: Table [dbo].[SubCodeReport3] Script Date: 9/8/2015 6:05:30 PM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
SET ANSI_PADDING ON 
GO 
CREATE TABLE [dbo].[SubCodeReport3](
    [ S-ID] [varchar](50) NULL, 
    [AGE] [varchar](50) NULL, 
    [SchoolCode] [varchar](50) NULL, 
    [SubCode1] [varchar](50) NULL, 
    [SubCode2] [varchar](50) NULL, 
    [SubCode3] [varchar](50) NULL, 
    [SubCode4] [varchar](50) NULL, 
    [SubCode5] [varchar](50) NULL 
) ON [PRIMARY] 

GO 
SET ANSI_PADDING OFF 
GO 
INSERT [dbo].[SubCodeReport] ([ S-ID], [AGE], [SchoolCode], [SubCode]) VALUES (N'25', N'23', N'KEN-009', N'ENG') 
INSERT [dbo].[SubCodeReport] ([ S-ID], [AGE], [SchoolCode], [SubCode]) VALUES (N'26', N'21', N'DLK-009', N'ENG') 
INSERT [dbo].[SubCodeReport] ([ S-ID], [AGE], [SchoolCode], [SubCode]) VALUES (N'27', N'25', N'DLK-006', N'MAT') 
INSERT [dbo].[SubCodeReport] ([ S-ID], [AGE], [SchoolCode], [SubCode]) VALUES (N'27', N'25', N'DLK-006', N'ENG') 
INSERT [dbo].[SubCodeReport] ([ S-ID], [AGE], [SchoolCode], [SubCode]) VALUES (N'27', N'25', N'DLK-006', N'STAT') 
INSERT [dbo].[SubCodeReport] ([ S-ID], [AGE], [SchoolCode], [SubCode]) VALUES (N'28', N'21', N'HLI-005', N'ENG') 
INSERT [dbo].[SubCodeReport] ([ S-ID], [AGE], [SchoolCode], [SubCode]) VALUES (N'29', N'22', N'NUI-002', N'ENG') 
INSERT [dbo].[SubCodeReport] ([ S-ID], [AGE], [SchoolCode], [SubCode]) VALUES (N'29', N'22', N'NUI-002', N'MAT') 
INSERT [dbo].[SubCodeReport] ([ S-ID], [AGE], [SchoolCode], [SubCode]) VALUES (N'30', N'22', N'INN-009', N'ENG') 
INSERT [dbo].[SubCodeReport] ([ S-ID], [AGE], [SchoolCode], [SubCode]) VALUES (N'30', N'22', N'INN-009', N'MAT') 
INSERT [dbo].[SubCodeReport] ([ S-ID], [AGE], [SchoolCode], [SubCode]) VALUES (N'30', N'22', N'INN-009', N'ZOO') 
INSERT [dbo].[SubCodeReport] ([ S-ID], [AGE], [SchoolCode], [SubCode]) VALUES (N'30', N'22', N'INN-009', N'GEO') 
INSERT [dbo].[SubCodeReport3] ([ S-ID], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3], [SubCode4], [SubCode5]) VALUES (N'25', N'23', N'KEN-009', N'ENG', N'', N'', N'', N'') 
INSERT [dbo].[SubCodeReport3] ([ S-ID], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3], [SubCode4], [SubCode5]) VALUES (N'26', N'21', N'DLK-009', N'ENG', N'', N'', N'', N'') 
INSERT [dbo].[SubCodeReport3] ([ S-ID], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3], [SubCode4], [SubCode5]) VALUES (N'27', N'25', N'DLK-006', N'MAT', N'ENG', N'STAT', N'', N'') 
INSERT [dbo].[SubCodeReport3] ([ S-ID], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3], [SubCode4], [SubCode5]) VALUES (N'28', N'21', N'HLI-005', N'ENG', N'', N'', N'', N'') 
INSERT [dbo].[SubCodeReport3] ([ S-ID], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3], [SubCode4], [SubCode5]) VALUES (N'29', N'22', N'NUI-002', N'ENG', N'MAT', N'', N'', N'') 
INSERT [dbo].[SubCodeReport3] ([ S-ID], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3], [SubCode4], [SubCode5]) VALUES (N'30', N'22', N'INN-009', N'ENG', N'MAT', N'ZOO', N'GEO', N'') 
+0

是很有幫助的標籤同時與相應的軟件數據庫的問題(MySQL和甲骨文,DB2,...)和版本,例如'的SQL服務器2014'。語法和功能的差異往往會影響答案。 – HABO

回答

3

您可以使用unpivot來獲得所需的結果。

Fiddle with sample data from the question

select [ S-ID], age, schoolcode, u.subcode 
from subcodereport3 
unpivot 
(subcode 
for x in (subcode1,subcode2,subcode3,subcode4,subcode5)) u 
where u.subcode <> ' '