T-SQL:以逗号分隔的组合单行创建结果

时间:2017-01-19 07:17:33

标签: tsql sql-server-2016

使用此代码:

SELECT *
FROM
    (SELECT        
         EQUITY_DIVISION_25_NAME Broker, 
         COUNT(tbl_user_details.USER_ID) new, 
         DENSE_RANK() OVER (ORDER BY COUNT(tbl_user_details.USER_ID) DESC) Rank
     FROM            
         TBL_FEES_MASTER RIGHT 
     OUTER JOIN
         TBL_USER_CREATE ON TBL_FEES_MASTER.FEE_ID = TBL_USER_CREATE.FEE_ID 
     LEFT OUTER JOIN
         TBL_USER_DETAILS ON TBL_USER_CREATE.USER_ID = TBL_USER_DETAILS.USER_ID
     WHERE 
         Joined_dt >= (SELECT MAX(last_report_run)
                       FROM new_lance_table
                       WHERE last_report_run < (SELECT MAX(last_report_run) 
                                                FROM new_lance_table)) 
                         AND (joined_dt <= GETDATE()) 
     GROUP BY 
         EQUITY_DIVISION_25_NAME) a

我得到了表格结果:

Broker              new Rank
-----------------------------
Todd Schuster       7   1
Tony Ketterling     7   1
Al Palmonari        4   2
Randall Wall        4   2
Edmund Sperry       3   3
Eric Lee    3       3
Steve Stringham     3   3
Timothy Gulla       2   4
Troy Peterson       2   4
Tuiono Malakai      2   4
Nancy Umbreit       2   4
Steve Goodsell      2   4
Sandy Dunkley       1   5
Gary Rosine         1   5
Ian Chait           1   5
Nancy Pearce-Harris 1   5
William Hochstedler 1   5
Troy C Peterson     1   5

如何进行查询以使结果显示如下:

  • 等级1:领带! Todd Schuster(7),Tony Ketterling(7)
  • 等级2:领带! Al Palmonari(4),Randall Wall(4)
  • 等级3:领带! Edmund Sperry(3),Eric Lee(3),Steve Stringham(3)

2 个答案:

答案 0 :(得分:0)

你可以这样试试:

DECLARE @dummyTbl TABLE(
   Broker VARCHAR(29)
  ,new    VARCHAR(13)
  ,Rank   VARCHAR(8)
);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Todd Schuster',7,1);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Tony Ketterling',7,1);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Al Palmonari',4,2);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Randall Wall',4,2);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Edmund Sperry',3,3);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Eric Lee',3,NULL);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Steve Stringham',3,3);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Timothy Gulla',2,4);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Troy Peterson',2,4);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Tuiono Malakai',2,4);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Nancy Umbreit',2,4);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Steve Goodsell',2,4);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Sandy Dunkley',1,5);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Gary Rosine',1,5);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Ian Chait',1,5);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Nancy Pearce-Harris',1,5);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('William Hochstedler',1,5);
INSERT INTO @dummyTbl(Broker,new,Rank) VALUES ('Troy C Peterson',1,5);

- 我使用声明的表变量和一个简单的SELECT * FROM @dummyTbl模拟查询

WITH CTE AS
(
    /*Place your own query here*/
    SELECT * FROM @dummyTbl
)
SELECT 'Rank ' + CAST(t.Rank AS VARCHAR(10)) + ': Tie! '
      +STUFF(
      (
        SELECT ', ' + x.Broker + ' (' + CAST(x.new AS VARCHAR(10)) + ')'
        FROM CTE AS x
        WHERE x.Rank=t.Rank
        FOR XML PATH('')
       ),1,2,'') 
FROM CTE AS t
GROUP BY t.Rank

结果

NULL
Rank 1: Tie! Todd Schuster (7), Tony Ketterling (7)
Rank 2: Tie! Al Palmonari (4), Randall Wall (4)
Rank 3: Tie! Edmund Sperry (3), Steve Stringham (3)
Rank 4: Tie! Timothy Gulla (2), Troy Peterson (2), Tuiono Malakai (2), Nancy Umbreit (2), Steve Goodsell (2)
Rank 5: Tie! Sandy Dunkley (1), Gary Rosine (1), Ian Chait (1), Nancy Pearce-Harris (1), William Hochstedler (1), Troy C Peterson (1)

简短说明:

简单的SELECT Rank FROM CTE GROUP BY Rank将返回一个不同的排名列表。这些数字由一些常数字符扩展。但是,除非您将Rank添加到GROUP BY,否则您将无法访问t.Rank以外的其他列。

但是:您可以使用子选择,其中x是唯一使用的列。其他列是从对同一个表的第二次调用中获取的,但别名为STUFF()

其余的(FOR XMLStream.of(/* .. some objects .. */) .map(/* some function that returns a boolean */) .reduce(Boolean::logicalOr); 结合使用连接值)相当容易。围绕这种技术有很多问题和答案。

答案 1 :(得分:0)

您可以将当前结果选择到临时表中,然后从1循环到最大(排名)并进行字符串组合。