创建水平列

时间:2017-05-02 15:23:11

标签: sql reporting-services

我有一张桌子,格式如下:

Staff      Client   Problems Status
    1        101      a        1
    1        101      b        0
    1        101      e        1
    2        102      g        0
    2        102      k        1

我需要根据它创建一个报告,格式如下。

Staff        Client   Problem1 Status  Problem2  Status Problem3 Status.....
1            101         a       1           b       0      e      1
2            102         g       0           k       1

更新

在SqlZim的帮助下,我得到如下结果:

Staff      Client     Category  Problems Status
        1        101    Problem1    a        1
        1        101    Problem2    b        0
        1        101    Problem3    e        1
        2        102    Problem1    g        0
        2        102    Problem2    k        1

我创建了一个矩阵SSRS报告,并将Category设置为列组,将Client设置为Row Groups,将问题设置为值。

我收到了错误。

你可以帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:2)

在SQL Server中:

您可以使用row_number()对行进行编号,并连接“问题”

select 
    Staff
  , Client
  , 'Problem'+convert(varchar(10),
      row_number() over (partition by Staff, Client order by Problems)
      ) as Category
  , Problems
  , Status
from t

在SQL Server 2012+中,您可以使用concat()来连接值,而不必将row_number()显式转换为varchar()

select 
    Staff
  , Client
  , concat('Problem',
      row_number() over (partition by Staff, Client order by Problems)
      ) as Category
  , Problems
  , Status
from t

rextester演示:Equirectangular projection with a "normal" photo

返回:

+-------+--------+----------+----------+--------+
| Staff | Client | Category | Problems | Status |
+-------+--------+----------+----------+--------+
|     1 |    101 | Problem1 | a        |      1 |
|     1 |    101 | Problem2 | b        |      0 |
|     1 |    101 | Problem3 | e        |      1 |
|     2 |    102 | Problem1 | g        |      0 |
|     2 |    102 | Problem2 | k        |      1 |
+-------+--------+----------+----------+--------+