有以下数据
日期 价格名称 结算价 销售价
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 结算价 销售价 结算价 销售价
日期 价格名称 结算价 销售价
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 2005 数据库备份问题
- 这个存储图书类别的表应该怎样设计比较好?
- sql连接到其他服务器上的数据库出错
- 求教一个SQL语句!!!
- ChildCount = conn.Execute("select count(*) from xuq_tree where id1="&id)(0)
- 发现一个诡异的查询问题,大家进来探探密!
- 在SQL Server 2000中,导出的数据能在Excel文件中进行追加吗?
- 如果是你,你选择动态统计还是"静态"统计?!
- 关于 Select 赋值
- mssql数据库,数据库同步,分布式数据库,数据库集群,如何实现
日期,
max(case 价格名称 when '价格1' then 计算价 else 0 end) as 价格1,
max(case 价格名称 when '价格1' then 销售价 else 0 end) as 价格1,
max(case 价格名称 when '价格2' then 计算价 else 0 end) as 价格2,
max(case 价格名称 when '价格2' then 销售价 else 0 end) as 价格2
from
tb
group by
日期
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 TB
group by 日期
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 tb
group by 日期
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 a
set @sql = @sql + ' from tb group by 日期'
exec(@sql)
-- 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 all
select '2012-1-1','价格2',1000,1200 union all
select '2012-1-2','价格1',1000,1200 union all
select '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 a
set @sql = @sql + ' from tb group by 日期'
exec(@sql)
----------------结果----------------------------
/* 日期 价格1 价格1 价格2 价格2
----------------------- ----------- ----------- ----------- -----------
2012-01-01 00:00:00.000 1000 1200 1000 1200
2012-01-02 00:00:00.000 1000 1200 1000 1200(2 行受影响)*/
--> 测试数据:[test]
if object_id('[test]') is not null
drop table [test]
create table [test](
[日期] datetime,
[价格名称] varchar(5),
[结算价] int,
[销售价] int
)
go
insert [test]
select '2012-1-1','价格1',1000,1200 union all
select '2012-1-1','价格2',1000,1200 union all
select '2012-1-2','价格1',1000,1200 union all
select '2012-1-2','价格2',1000,1200
go
if OBJECT_ID('pro_test')is not null
drop proc pro_test
go
create proc pro_test
(
@PriceName varchar(20)
)
as
declare @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
test
where
CHARINDEX([价格名称],@PriceName)>0
group by
[价格名称]
print @str
set @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 1200
2012-01-02 1000 1200
*/
exec pro_test '价格2'
/*
日期 价格2结算价 价格2销售价
2012-01-01 1000 1200
2012-01-02 1000 1200
*/
exec pro_test '价格1,价格2'
/*
日期 价格1结算价 价格1销售价 价格2结算价 价格2销售价
2012-01-01 1000 1200 1000 1200
2012-01-02 1000 1200 1000 1200
*/
/* 日期 价格1 价格1 价格2 价格2 >>
我想问一下能变成这种布局吗
日期 价格1 价格2 价格1 价格2
select
日期,
max(case 价格名称 when '价格1' then 计算价 else 0 end) as 价格1,
max(case 价格名称 when '价格2' then 计算价 else 0 end) as 价格2,
max(case 价格名称 when '价格1' then 销售价 else 0 end) as 价格1,
max(case 价格名称 when '价格2' then 销售价 else 0 end) as 价格2
from
tb
group by
日期