0

我不确定我在这里需要什么,看起来有点像我可以使用枢轴,但我认为它没有那么复杂,如果可以的话,我想避免使用枢轴,因为我没有经常使用它(呃,根本) .

我有这样的数据:

ID      score   notes               CreateDate
1661    9.2     8.0 on Sept 2010    7/22/2010
1661    7.6                         11/4/2010
1661    7.9                         6/10/2011
1661    8.3                         9/28/2011
1661    7.9                         1/20/2012

我想将所有这些数据组织到一行中,最旧的日期排在第一位,然后使用下一个最旧的日期,然后是下一个最旧的日期……直到我使用 4 或 5 个日期。所以最终结果看起来像这样:

ID      score1  notes1              date1       score2  notes2  date2       score3  notes3  date3       score4  notes4  date4
1661    9.2     8.0 on Sept 2010    7/22/2010   7.6     blah    11/4/2010   7.9     blah2   6/10/2011   8.3     blah3   9/28/2011
4

2 回答 2

5

在这种情况下,PIVOT 会很棘手,因为每个测试有多个列(如果您只想显示 Score1、Score2、Score3 等,PIVOT 效果很好)。幸运的是,您可以使用 CASE 语句创建一个简单的(如果冗长的话)解决方案:

select
  ID,

  max(case when RowNum = 1 then Score else null end) as Score1,
  max(case when RowNum = 1 then Notes else null end) as Notes1,
  max(case when RowNum = 1 then CreateDate else null end) as Date1,

  max(case when RowNum = 2 then Score else null end) as Score2,
  max(case when RowNum = 2 then Notes else null end) as Notes2,
  max(case when RowNum = 2 then CreateDate else null end) as Date2,

  max(case when RowNum = 3 then Score else null end) as Score3,
  max(case when RowNum = 3 then Notes else null end) as Notes3,
  max(case when RowNum = 3 then CreateDate else null end) as Date3,

  max(case when RowNum = 4 then Score else null end) as Score4,
  max(case when RowNum = 4 then Notes else null end) as Notes4,
  max(case when RowNum = 4 then CreateDate else null end) as Date4,

  max(case when RowNum = 5 then Score else null end) as Score5,
  max(case when RowNum = 5 then Notes else null end) as Notes5,
  max(case when RowNum = 5 then CreateDate else null end) as Date5

from

(
select 
   *, row_number() over (partition by ID order by CreateDate) as RowNum
from 
   mytable
) tt

group by
  ID

这是硬编码以涵盖 5 个测试。少一点就可以了,但不会显示第六个。您显然可以创建更多 CASE 语句来处理更多测试。

于 2013-09-11T21:04:37.293 回答
3

仅仅因为我喜欢枢轴,我将向您展示如何使用 PIVOT 函数来完成。为了使用 PIVOT 函数获得结果,您首先需要对多个列进行scoreUNPIVOTnotescreatedate. unpivot 过程会将多列转换为多行。

由于您使用的是 SQL Server 2008,因此您可以使用 CROSS APPLY 取消透视数据,查询的第一部分将类似于:

;with cte as
(
  select id, score, notes, createdate,
    row_number() over(partition by id order by createdate) seq
  from yourtable
) 
select id, col, value
from 
(
  select t.id, 
    col = col + cast(seq as varchar(10)),
    value
  from cte t
  cross apply
  (
    values
      ('score', cast(score as varchar(10))),
      ('notes', notes),
      ('date', convert(varchar(10), createdate, 120))
  ) c (col, value)
) d;

请参阅SQL Fiddle with Demo。这样做会使您的数据采用以下格式:

|   ID |    COL |            VALUE |
| 1661 | score1 |             9.20 |
| 1661 | notes1 | 8.0 on Sept 2010 |
| 1661 |  date1 |       2010-07-22 |
| 1661 | score2 |             7.60 |
| 1661 | notes2 |           (null) |
| 1661 |  date2 |       2010-11-04 |
| 1661 | score3 |             7.90 |

现在您可以应用 PIVOT 功能:

;with cte as
(
  select id, score, notes, createdate,
    row_number() over(partition by id order by createdate) seq
  from yourtable
) 
select id, col, value
from 
(
  select t.id, 
    col = col + cast(seq as varchar(10)),
    value
  from cte t
  cross apply
  (
    values
      ('score', cast(score as varchar(10))),
      ('notes', notes),
      ('date', convert(varchar(10), createdate, 120))
  ) c (col, value)
) d
pivot
(
  max(value)
  for col in (score1, notes1, date1, score2, notes2, date2,
              score3, notes3, date3, score4, notes4, date4,
              score5, notes5, date5)
) piv;

请参阅SQL Fiddle with Demo

然后,如果您要为 each 有一个未知数量的值id,您可以实现动态 SQL 来获得结果:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(col + cast(seq as varchar(10))) 
                    from 
                    (
                      select row_number() over(partition by id order by createdate) seq
                      from yourtable
                    ) d
                    cross apply 
                    (
                      select 'score', 1 union all
                      select 'notes', 2 union all
                      select 'date', 3
                    ) c (col, so)
                    group by seq, col, so
                    order by seq, so
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT id, ' + @cols + ' 
            from 
            (
              select t.id, 
                col = col + cast(seq as varchar(10)),
                value
              from
              (
                select id, score, notes, createdate,
                  row_number() over(partition by id order by createdate) seq
                from yourtable
              ) t
              cross apply
              (
                values
                  (''score'', cast(score as varchar(10))),
                  (''notes'', notes),
                  (''date'', convert(varchar(10), createdate, 120))
              ) c (col, value)
            ) x
            pivot 
            (
                max(value)
                for col in (' + @cols + ')
            ) p '

execute sp_executesql @query;

请参阅SQL Fiddle with Demo。两个版本都给出了结果:

|   ID | SCORE1 |           NOTES1 |      DATE1 | SCORE2 | NOTES2 |      DATE2 | SCORE3 | NOTES3 |      DATE3 | SCORE4 | NOTES4 |      DATE4 | SCORE5 | NOTES5 |      DATE5 |
| 1661 |   9.20 | 8.0 on Sept 2010 | 2010-07-22 |   7.60 | (null) | 2010-11-04 |   7.90 | (null) | 2011-06-10 |   8.30 | (null) | 2011-09-28 |   7.90 | (null) | 2012-01-20 |
于 2013-09-11T22:17:06.293 回答