1

我在 SQL Server 2008 中有表,例如:

主题表

 TopicID: nvarchar  (Primary Key)
 ProgID: nvarchar
 topic1: bit
 topic2: bit
 topic3: bit
 topic4: bit

主题表如下所示:

 TopicID      ProgID        topic1        topic2       topic3        topic4

 topic001     prog001         1             1             0             0
 topic002     prog002         1             0             1             1
 topic003     prog003         1             0             0             0
 topic004     prog004         1             1             1             1

节目表:

 ProgID: nvarchar  (Primary Key)
 ProgramName: nvarchar

程序表如下所示:

 ProgID         ProgramName         

 prog001        programA            
 prog002        programB
 prog003        programC
 prog004        programD

我想创建一个视图来获取如下输出:

   ProgID         ProgramName          Topic

   prog001        programA             topic1,topic2
   prog002        programB             topic1,topic3,topic4
   prog003        programB             topic1
   prog004        programD             topic1,topic2,topic3,topic4

请有人可以帮助我如何得到这个。谢谢你。

4

2 回答 2

1

它看起来像这样:

;WITH cteTopics AS (
SELECT T.ProgID
  ,STUFF((
    SELECT T1.TopicID + ','
    FROM TopicTable T1
    WHERE T1.ProgID = T.ProgID
    FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR (MAX)')
    ,1,0,'') [Topics]
FROM TopicTable T
GROUP BY T.ProgID)

SELECT P.ProgID, P.ProgramName, T.Topics
FROM Program P
LEFT JOIN cteTopics T ON T.ProgID = P.ProgID

http://sqlfiddle.com/#!3/b9e4a/4

于 2013-11-01T17:26:17.293 回答
0

您将在这里有一些额外的逗号,但您可以稍微调整代码以消除额外的逗号。这是我到目前为止为你准备的

CREATE Table Topic (Topic NVARCHAR(20), Programe NVARCHAR(20), Topic1 bit, Topic2 bit,Topic3 bit,Topic4 bit)
GO
INSERT INTO Topic
 VALUES 
 ('topic001','prog001',1,1,0,0),
 ('topic002','prog002',1,0,1,1),
 ('topic003','prog003',1,0,0,0),
 ('topic004','prog004',1,1,1,1)
 GO


CREATE TABLE Programe (P_ID NVARCHAR(20) , Name NVARCHAR(20))
GO
INSERT INTO Programe VALUES
 ('prog001','programA'),            
 ('prog002','programB'),
 ('prog003','programC'),
 ('prog004','programD')
 GO

查看定义

CREATE VIEW vw_ViewName
AS
 SELECT P_ID, Name, ISNULL(STUFF(L1.Topic1L, 1, 1 , '') + ', ', '') + ISNULL(STUFF(L2.Topic2L, 1, 1, '') + ', ', '') 
                                    +  ISNULL( STUFF(L3.Topic3L, 1, 1, '')+ ', ', '') + ISNULL(STUFF(L4.Topic4L, 1, 1, '')+ ', ', '')  AS Topics
 FROM Programe P CROSS APPLY (
                            SELECT ' ' + CASE WHEN Topic1 = 1 THEN 'Topic1' ELSE NULL END [text()]
                            FROM Topic
                            WHERE Programe = P.P_ID
                            FOR XML PATH('')
                            )L1(Topic1L)
                CROSS APPLY (
                            SELECT ', ' + CASE WHEN Topic2 = 1 THEN 'Topic2' ELSE NULL END [text()]
                            FROM Topic
                            WHERE Programe = P.P_ID
                            FOR XML PATH('')
                            )L2(Topic2L)
               CROSS APPLY (
                            SELECT ', ' + CASE WHEN Topic3 = 1 THEN 'Topic3' ELSE NULL END [text()]
                            FROM Topic
                            WHERE Programe = P.P_ID
                            FOR XML PATH('')
                            )L3(Topic3L)
               CROSS APPLY (
                            SELECT ', ' + CASE WHEN Topic4= 1 THEN 'Topic4' ELSE NULL END [text()]
                            FROM Topic
                            WHERE Programe = P.P_ID
                            FOR XML PATH('')
                            )L4(Topic4L)

结果集

P_ID    Name        Topics
prog001 programA    Topic1,  Topic2, 
prog002 programB    Topic1,  Topic3,  Topic4, 
prog003 programC    Topic1, 
prog004 programD    Topic1,  Topic2,  Topic3,  Topic4, 
于 2013-11-01T17:12:56.707 回答