0

SQL 2005を使用しています。(より適切な単語がないために)転置しようとしています。User列の値を実際の列名として作成し、しきい値を入力します。ユーザー数は動的です。3以上でも、35を超えないようにすることもできます。ご覧のとおり、アクションあたりのユーザー数は常に同じになります。助けてくれてありがとう。

-- tbl_actions
Title   GrpName  Action    User    Threshold
Title1  Group1   Action1    A      180:10:Green
Title1  Group1   Action1    B      180:30:Yellow
Title1  Group1   Action1    C      180:45:Yellow
Title1  Group1   Action2    A      360:10:Green
Title1  Group1   Action2    B      360:23:Yellow
Title1  Group1   Action2    C      360:50:Red
Title1  Group1   Action4    A      180:10:Green
Title1  Group1   Action4    B      180:35:Yellow
Title1  Group1   Action4    C      180:25:Yellow

-- Result sought
Title   GrpName   Action            A               B               C 
Title1  Group1    Action1   180:10:Green    180:30:Yellow   180:45:Yellow
Title1  Group1    Action2   360:10:Green    360:23:Yellow   360:50:Red
Title1  Group1    Action4   180:10:Green    180:35:Yellow   180:25:Yellow 

--For table data
Select 'Title1' as Title, 'Group1' as GroupName, 'Action1' as Action, 'A' as UserName, '180:10:Green:2/20/2013' as Threshold
UNION
Select 'Title1' as Title, 'Group1' as GroupName, 'Action1' as Action, 'B' as UserName, '180:30:Yellow:2/22/2013' as Threshold
UNION
Select 'Title1' as Title, 'Group1' as GroupName, 'Action1' as Action, 'C' as UserName, '180:45:Yellow:2/21/2013' as Threshold
UNION

Select 'Title1' as Title, 'Group1' as GroupName, 'Action2' as Action, 'A' as UserName, '360:10:Green:2/18/2013' as Threshold
UNION
Select 'Title1' as Title, 'Group1' as GroupName, 'Action2' as Action, 'B' as UserName, '360:23:Yellow:2/1/2013' as Threshold
UNION
Select 'Title1' as Title, 'Group1' as GroupName, 'Action2' as Action, 'C' as UserName, '360:50:Red:2/3/2013' as Threshold
UNION

Select 'Title1' as Title, 'Group1' as GroupName, 'Action4' as Action, 'A' as UserName, '180:10:Green:2/5/2013' as Threshold
UNION
Select 'Title1' as Title, 'Group1' as GroupName, 'Action4' as Action, 'B' as UserName, '180:35:Yellow:2/22/2013' as Threshold
UNION
Select 'Title1' as Title, 'Group1' as GroupName, 'Action4' as Action, 'C' as UserName, '180:25:Yellow:2/11/2013' as Threshold

--Result
Select 'Title1' as Title, 'Group1' as GroupName, 'Action1' as Action, '180:10:Green:2/20/2013' as 'A', '180:30:Yellow:2/22/2013' as 'B','180:45:Yellow:2/21/2013' as 'C'  
UNION
Select 'Title1' as Title, 'Group1' as GroupName, 'Action2' as Action, '360:10:Green:2/18/2013' as 'A', '360:23:Yellow:2/1/2013' as 'B','360:50:Red:2/3/2013' as 'C'
UNION
Select 'Title1' as Title, 'Group1' as GroupName, 'Action3' as Action, '180:10:Green:2/5/2013' as 'A', '180:35:Yellow:2/22/2013' as 'B','180:25:Yellow:2/11/2013' as 'C'
4

1 に答える 1

1

これを試して

declare @cols varchar(max)
set @cols = stuff(
                  (select distinct ',' + QUOTENAME(username)
                   from tbl_actions for xml path('')
                  ),1,1,''
                 )
-- print @cols
-- result: [A],[B],[C]

declare @query nvarchar(max)
set @query = 'select title, groupname, action ' + @cols
             + ' from (select * from tbl_actions) v'
             + ' pivot (max(threshold) for username in ('
             + @cols + ')) pvt'

exec(@query)
于 2013-03-02T22:39:03.190 に答える