与数据透视表连接

时间:2013-10-15 19:52:27

标签: sql-server sql-server-2008 sql-server-2012

我有两个像t1和t2的表。 T1

ID                          class name
--                          -----------
1                            basket weaving
2                            Underwater Basketing

t2
ID                            POS     DAYS
--                            ----    -----
1                              1      M
1                              2      T
1                              3      W
1                              4     TH
1                              5      F
2                              1      M
2                              2      W
2                              3      F

我想从两个表中获得以下输出。

1                             class                  DAYS
--                            -----                  -----
1                             BASKET WEAVING         MTWTHF
2                             Underwater basketing   MWF

2 个答案:

答案 0 :(得分:3)

查询1

SELECT ID, [class name], STUFF(List, 1 ,2, '') [DAYS]
    FROM Table1 T1 CROSS APPLY 
                            (
                            SELECT ', ' + [DAYS] [text()]
                            FROM Table2
                            WHERE ID = T1.ID
                            FOR XML PATH('')
                            ) T2(List)

<强> RESULT

ID                            class name             DAYS
--                            -----                  -----
1                             BASKET WEAVING         M,T,W,TH,F
2                             Underwater basketing   M,W,F

如果您想要逗号,它会给你一个逗号分隔的日期列表你可以使用以下
查询2

SELECT ID, [class name], List [DAYS]
FROM Table1 T1 CROSS APPLY 
                        (
                        SELECT  [DAYS] [text()]
                        FROM Table2
                        WHERE ID = T1.ID
                        FOR XML PATH('')
                        ) T2(List)

<强> RESULT

ID                            class name             DAYS
--                            -----                  -----
1                             BASKET WEAVING         MTWTHF
2                             Underwater basketing   MWF

查询3

SELECT ID, [class name], STUFF(List, 1 ,2, '') [DAYS], STUFF(T3.Rooms, 1 ,2, '') [Rooms]
FROM Table1 T1 CROSS APPLY 
                            (
                            SELECT ', ' + [DAYS] [text()]
                            FROM Table2
                            WHERE ID = T1.ID
                            FOR XML PATH('')
                            ) T2(List)
               CROSS APPLY 
                        (
                        SELECT '/' + Rooms [text()]
                        FROM Table2
                        WHERE ID = T1.ID
                        FOR XML PATH('')
                        ) T3(Rooms)

你可以通过做这样的事情来添加另一列......

答案 1 :(得分:0)

在跨知道间隔汇总某些值时,您可能希望使用数据透视表。如果你想要一个具有值组合的字段,我建议使用FOR XML PATH。使用pivot的输出看起来像:

Class ID   Class Name    Monday    Tuesday    Wednesday    Thursday   Friday...
1          Class 1       1         1          1            0          0
2          Class 2       0         0          0            1          0        
相关问题