如何可将数据表列输出为表
有以下数据
日期 价格名称 结算价 销售价
2012-1-1 价格1 1000 1200
2012-1-1 价格2 1000 1200
2012-1-2 价格1 1000 1200
2012-1-2 价格2 1000 1200
想输出这种格式
日期 价格1 价格2
2012-1-1 结算价 销售价 结算价 销售价
[解决办法]
- SQL code
select 日期,case 价格名称 when 价格1 then 结算价 else 0 end as 价格1结算价, case 价格名称 when 价格1 then 销售价 else 0 end as 价格1销售价, case 价格名称 when 价格2 then 结算价 else 0 end as 价格2结算价, case 价格名称 when 价格2 then 销售价 else 0 end as 价格2销售价from TBgroup by 日期
[解决办法]
大致如下:
- SQL code
select 日期,case when 价格名称 = 价格1 then 结算价 else null end as 价格1结算价,case when 价格名称 = 价格1 then 销售价 else null end as 价格1销售价,case when 价格名称 = 价格2 then 结算价 else null end as 价格2结算价,case when 价格名称 = 价格2 then 销售价 else null end as 价格2销售价from tbgroup by 日期
[解决办法]
- SQL code
declare @sql varchar(8000)set @sql = 'select 日期 'select @sql = @sql + ' , max(case 价格名称 when ''' + 价格名称 + ''' then 结算价 else 0 end) [' + 价格名称 + '], max(case 价格名称 when ''' + 价格名称 + ''' then 销售价 else 0 end) [' + 价格名称 + ']'from (select distinct 价格名称 from tb) as aset @sql = @sql + ' from tb group by 日期'exec(@sql)
[解决办法]
- SQL code
------------------------------ Author :fredrickhu(小F,向高手学习)-- Date :2012-06-21 10:20:08-- Version:-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) -- Apr 22 2011 11:57:00 -- Copyright (c) Microsoft Corporation-- Enterprise Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)--------------------------------> 测试数据:[tb]if object_id('[tb]') is not null drop table [tb]go create table [tb]([日期] datetime,[价格名称] varchar(5),[结算价] int,[销售价] int)insert [tb]select '2012-1-1','价格1',1000,1200 union allselect '2012-1-1','价格2',1000,1200 union allselect '2012-1-2','价格1',1000,1200 union allselect '2012-1-2','价格2',1000,1200--------------开始查询--------------------------declare @sql varchar(8000)set @sql = 'select 日期 'select @sql = @sql + ' , max(case 价格名称 when ''' + 价格名称 + ''' then 结算价 else 0 end) [' + 价格名称 + '], max(case 价格名称 when ''' + 价格名称 + ''' then 销售价 else 0 end) [' + 价格名称 + ']'from (select distinct 价格名称 from tb) as aset @sql = @sql + ' from tb group by 日期'exec(@sql) ----------------结果----------------------------/* 日期 价格1 价格1 价格2 价格2----------------------- ----------- ----------- ----------- -----------2012-01-01 00:00:00.000 1000 1200 1000 12002012-01-02 00:00:00.000 1000 1200 1000 1200(2 行受影响)*/
[解决办法]
- SQL code
--> 测试数据:[test]if object_id('[test]') is not null drop table [test]create table [test]([日期] datetime,[价格名称] varchar(5),[结算价] int,[销售价] int)goinsert [test]select '2012-1-1','价格1',1000,1200 union allselect '2012-1-1','价格2',1000,1200 union allselect '2012-1-2','价格1',1000,1200 union allselect '2012-1-2','价格2',1000,1200goif OBJECT_ID('pro_test')is not nulldrop proc pro_testgocreate proc pro_test(@PriceName varchar(20))asdeclare @str varchar(2000)set @str=''select @str=@str+',['+价格名称+'结算价]=max(case when [价格名称]=' +QUOTENAME([价格名称],'''')+' then [结算价] else 0 end),[' +价格名称+'销售价]=max(case when [价格名称]='+QUOTENAME([价格名称],'''') +' then [销售价] else 0 end)'from testwhere CHARINDEX([价格名称],@PriceName)>0group by [价格名称]print @strset @str='select convert(varchar(10),[日期],120) as [日期]' +@str+' from test where charindex([价格名称],'+quotename(@PriceName,'''')+')>0 group by convert(varchar(10),[日期],120)'exec(@str)go exec pro_test '价格1'/*日期 价格1结算价 价格1销售价2012-01-01 1000 12002012-01-02 1000 1200*/exec pro_test '价格2'/*日期 价格2结算价 价格2销售价2012-01-01 1000 12002012-01-02 1000 1200*/exec pro_test '价格1,价格2'/*日期 价格1结算价 价格1销售价 价格2结算价 价格2销售价2012-01-01 1000 1200 1000 12002012-01-02 1000 1200 1000 1200*/