SQL Server 2008では行を列に、列を列に切り替えるのに問題があり、ソリューションへのクエリを試しましたが、適切な結果が得られませんでした。SQL Server 2008で列を列に、列を列に、SQL Server 2008で列を切り替える方法
私は、次のような表をしました:
declare @tmpTable table (name varchar(20), date_ date, sales_code char(1), sales smallint, earned int) insert into @tmpTable values ('Robert', '2016/8/1', 'A', 2, 30), ('Robert', '2016/8/1', 'B', 3, 45), ('Robert', '2016/8/2', 'B', 1, 15), ('Robert', '2016/8/3', 'B', 2, 30), ('Jhon', '2016/8/1', 'A', 3, 45), ('Jhon', '2016/8/2', 'A', 3, 45), ('Jhon', '2016/8/3', 'B', 2, 30) select * from @tmpTable;
結果:それは結果を表示します
select * from ( select name, 'sales_code' as category, date_, sales_code from ( select * from ( SELECT name, date_ ,STUFF((SELECT ', ' + sales_code [text()] FROM @tmpTable WHERE date_ = t.date_ FOR XML PATH(''), TYPE) .value('.','NVARCHAR(MAX)'),1,2,' ') sales_code ,STUFF((SELECT ', ' + convert(varchar(max), sales) [text()] FROM @tmpTable WHERE date_ = t.date_ FOR XML PATH(''), TYPE) .value('.','NVARCHAR(MAX)'),1,2,' ') sales ,STUFF((SELECT ', ' + convert(varchar(max), earned) [text()] FROM @tmpTable WHERE date_ = t.date_ FOR XML PATH(''), TYPE) .value('.','NVARCHAR(MAX)'),1,2,' ') earned FROM @tmpTable t GROUP BY name, date_ ) as a ) as a ) as a pivot ( max(sales_code) FOR date_ IN ([2016/8/1], [2016/8/2], [2016/8/3]) )as pv union all select * from ( select name, 'sales' as category, date_, sales from ( select * from ( SELECT name, date_ ,STUFF((SELECT ', ' + sales_code [text()] FROM @tmpTable WHERE date_ = t.date_ FOR XML PATH(''), TYPE) .value('.','NVARCHAR(MAX)'),1,2,' ') sales_code ,STUFF((SELECT ', ' + convert(varchar(max), sales) [text()] FROM @tmpTable WHERE date_ = t.date_ FOR XML PATH(''), TYPE) .value('.','NVARCHAR(MAX)'),1,2,' ') sales ,STUFF((SELECT ', ' + convert(varchar(max), earned) [text()] FROM @tmpTable WHERE date_ = t.date_ FOR XML PATH(''), TYPE) .value('.','NVARCHAR(MAX)'),1,2,' ') earned FROM @tmpTable t GROUP BY name, date_ ) as a ) as a ) as a pivot ( max(sales) FOR date_ IN ([2016/8/1], [2016/8/2], [2016/8/3]) )as pv union all select * from ( select name, 'earned' as category, date_, earned from ( select * from ( SELECT name, date_ ,STUFF((SELECT ', ' + sales_code [text()] FROM @tmpTable WHERE date_ = t.date_ FOR XML PATH(''), TYPE) .value('.','NVARCHAR(MAX)'),1,2,' ') sales_code ,STUFF((SELECT ', ' + convert(varchar(max), sales) [text()] FROM @tmpTable WHERE date_ = t.date_ FOR XML PATH(''), TYPE) .value('.','NVARCHAR(MAX)'),1,2,' ') sales ,STUFF((SELECT ', ' + convert(varchar(max), earned) [text()] FROM @tmpTable WHERE date_ = t.date_ FOR XML PATH(''), TYPE) .value('.','NVARCHAR(MAX)'),1,2,' ') earned FROM @tmpTable t GROUP BY name, date_ ) as a ) as a ) as a pivot ( max(earned) FOR date_ IN ([2016/8/1], [2016/8/2], [2016/8/3]) )as pv
:
Name date_ sales_code sales earned ------ ---------- ---------- ----- ------ Robert 2016-08-01 A 2 30 Robert 2016-08-01 B 3 45 Robert 2016-08-02 B 1 15 Robert 2016-08-03 B 2 30 Jhon 2016-08-01 A 3 45 Jhon 2016-08-02 A 3 45 Jhon 2016-08-03 B 2 30
はその後、私は次のクエリを持っている
name category 2016/8/1 2016/8/2 2016/8/3 ------- -------- -------- ------- -------- Jhon sales_code A, B, A B, A B, B Robert sales_code A, B, A B, A B, B Jhon sales 2, 3, 3 1, 3 2, 2 Robert sales 2, 3, 3 1, 3 2, 2 Jhon earned 30, 45, 45 15, 45 30, 30 Robert earned 30, 45, 45 15, 45 30, 30
しかし、私は次のような結果を取得したいと思います:任意の助け
name category 2016/8/1 2016/8/2 2016/8/3 ---- -------- -------- -------- -------- Robert sales_code A, B B B Robert sales 2, 3 1 2 Robert earned 30, 45 15 30 Jhon sales_code A A B Jhon sales 3 3 2 Jhon earned 45 45 30
おかげで多くのことを。
スタックオーバーフローは無料のコード作成サービスではありません。あなたのコード/努力と実際の問題点を示してください。 –
@ChrisPickford、私は私の質問を更新しました、ごめんなさいスタックオーバーフローで新しいです。 –