在SQL的帮助下的摘要报告

时间:2016-02-25 02:54:23

标签: sql sql-server

我想借助下表

制作摘要报告

表1:Account

+----+------+---------------+
| Emp| T Amt|    Bill Id    |
+----+------+---------------+
|  12| 100  | 101           |
|  11| 200  | 102           |
|  12| 50   | 103           |
+----+------+---------------+

预期输出: -

+----+------+---------------+
| Emp| T Amt|    Bill Id    |
+----+------+---------------+
|  12| 150  | 101,103       |
|  11| 200  | 102           |
+----+------+---------------+

1 个答案:

答案 0 :(得分:0)

select 
      Emp,
      sum([T Amt]),
      stuff((select ','+ [Bill Id] from Account a where a.Emp = b.Emp for xml path('')),1,1, '')  bill_ids
from Account b
group by Emp,bill_ids
order by Emp desc;