2

我有两张像 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
4

2 回答 2

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)

结果

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

如果您想要没有逗号,它会给您一个逗号分隔的日期列表,您可以使用以下
Query2

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)

结果

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)

您可以通过执行以下操作来添加另一列.....

于 2013-10-15T20:03:52.363 回答
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        
于 2013-10-15T20:09:51.147 回答