SSRS 2005查找具有最大值的列的名称

时间:2013-02-15 20:34:30

标签: sql-server sql-server-2005 reporting-services reportingservices-2005 unpivot

最右边的列是我要添加到报告中的内容。这可以不修改查询以使用像Unpivot这样的东西吗?

Step X      Step W      Step A     Step B     Step Y     Last Step
---------------------------------------------------------------------
1/21/2013   1/24/2013   1/3/2013   1/5/2013   1/7/2013   Step W

这是朝着正确方向迈出的一步,但似乎只适用于SSRS 2008:http://www.bigator.com/2012/04/26/spothighlight-minimum-and-maximum-values-in-each-row-in-matrix-report-in-ssrs/

1 个答案:

答案 0 :(得分:2)

您可以使用UNPIVOT功能和CROSS APPLY来获取此信息:

;with cte as
(
  select col, value
  from yourtable
  unpivot
  (
    value
    for col in ([Step X], [Step W], [Step A], [Step B], [Step Y])
  ) unpiv
) 
select [Step X], 
  [Step W], 
  [Step A], 
  [Step B], 
  [Step Y],
  d.col LastStep
from yourtable
cross apply
(
  select c1.col
  from cte c1
  inner join
  (
    select max(value) MaxDate
    from cte
  ) c2
    on c1.value = c2.maxdate
) d

请参阅SQL Fiddle with Demo