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

すべてのデータを 1 つの行に整理し、最も古い日付を最初にしてから、次に古い日付を使用し、次に古い日付を使用します... 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

3

私はピボットが大好きなので、PIVOT 関数を使用してこれを行う方法を紹介します。PIVOT 関数で結果を取得するには、まず複数の列を UNPIVOT する必要がscoreありnotesますcreatedate。アンピボット プロセスは、複数の列を複数の行に変換します。

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を参照してください。

次に、各 の値の数が不明な場合は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 に答える