0

我有一张桌子,例如假设这个设置

MyTable有各种列IdUserId、 col1 、 col2 col3 ,包括名为Stuff的列。

我想通过查询从 MyTable 输出某些列,但我想拆分“Stuff”列,以便在查询中显示 2 个新列

我可以定义硬编码的类别,我不确定如何在 sql 中表示

Categoy1 = "alpha, bravo, delta, gamma';
Categoy2 = "charlie, echo, hotel';



MyTable

ID |    UserID |      Stuff       | Other Cols....
----------------------------------------------------------
1          1          alpha
2          2          hotel
3          1          charlie
4          1          echo
5          1          gamma
6          2          bravo
7          2          delta

我希望显示选择查询

UserId  |  Category1           |  Catergory2
----------------------------------------------------------
1            alpha, gamma            charlie, echo
---------------------------------------------------------
2            bravo, delta            hotel
----------------------------------------------------------

即根据 stuff 列是否包含来自 category1 或 category2 的项目,基于不同的 userId 生成 2 个列,类别内容可以用逗号分隔,如上所示

请你能说明如何做到这一点

希望这是有道理的。

谢谢

4

2 回答 2

1

My try, the technique I learned from Stack Overflow!... Please check:

DECLARE @Categoy1 NVARCHAR(MAX) = 'alpha, bravo, delta, gamma',
        @Categoy2 NVARCHAR(MAX) = 'charlie, echo, hotel'
SELECT 
    UserID,      
    STUFF((SELECT  ', ' + display_term
       FROM sys.dm_fts_parser('"'+ ',' + @Categoy1  + '"', 1033, NULL, 0) INNER JOIN 
            YourTable T on display_term=[Stuff]
            WHERE T.UserID= x.UserID
        FOR XML PATH(''), TYPE
    ).value('.', 'NVARCHAR(MAX)'), 1, 2, '') Category1,
    STUFF((SELECT  ', ' + display_term
       FROM sys.dm_fts_parser('"'+ ',' + @Categoy2  + '"', 1033, NULL, 0) INNER JOIN 
            YourTable T on display_term=[Stuff]
            WHERE T.UserID= x.UserID
        FOR XML PATH(''), TYPE
    ).value('.', 'NVARCHAR(MAX)'), 1, 2, '') Category2
FROM YourTable x
GROUP BY UserID
于 2013-02-05T11:57:09.847 回答
1

您可以使用 xml 扩展来连接字符串,然后将类别硬编码到每个子查询中:

CREATE TABLE #T (ID INT, UserID INT, [Stuff] VARCHAR(300))
INSERT #T VALUES
    (1, 1, 'alpha'),
    (2, 2, 'hotel'),
    (3, 1, 'charlie'),
    (4, 1, 'echo'),
    (5, 1, 'gamma'),
    (6, 2, 'bravo'),
    (7, 2, 'delta');

SELECT  UserID,
        [Category1] = STUFF((   SELECT  ', ' + [Stuff]
                                FROM    #T t2
                                WHERE   [Stuff] IN ('alpha', 'bravo', 'delta', 'gamma')
                                AND     t.UserID = t2.UserID
                                FOR XML PATH(''), TYPE
                            ).value('.', 'NVARCHAR(MAX)'), 1, 2, ''),
        [Category2] = STUFF((   SELECT  ', ' + [Stuff]
                                FROM    #T t2
                                WHERE   [Stuff] IN ('charlie', 'echo', 'hotel')
                                AND     t.UserID = t2.UserID
                                FOR XML PATH(''), TYPE
                            ).value('.', 'NVARCHAR(MAX)'), 1, 2, '')
FROM    (   SELECT  DISTINCT UserID
            FROM    #T
        ) t

SQL Fiddle 示例

您可以在 CTE ( Categories) 的开头定义您的类别以提高可读性:

WITH Categories AS
(   SELECT  Category, Name
    FROM    (VALUES
                (1, 'alpha'), 
                (1, 'bravo'), 
                (1, 'delta'), 
                (1, 'gamma'),
                (2, 'charlie'),
                (2, 'echo'), 
                (2, 'hotel')
            ) t (Category, Name)
), Data AS
(   SELECT  UserID, [Stuff], Category
    FROM    T
            INNER JOIN Categories c
                ON c.Name = T.[Stuff]
)
SELECT  UserID,
        [Category1] = STUFF((   SELECT  ', ' + [Stuff]
                                FROM    Data t2
                                WHERE   Category = 1
                                AND     t.UserID = t2.UserID
                                FOR XML PATH(''), TYPE
                            ).value('.', 'NVARCHAR(MAX)'), 1, 2, ''),
        [Category2] = STUFF((   SELECT  ', ' + [Stuff]
                                FROM    Data t2
                                WHERE   Category = 2
                                AND     t.UserID = t2.UserID
                                FOR XML PATH(''), TYPE
                            ).value('.', 'NVARCHAR(MAX)'), 1, 2, '')
FROM    (   SELECT  DISTINCT UserID
            FROM    T
        ) t

SQL Fiddle 示例

于 2013-02-05T11:35:36.097 回答