SELECT 表.时间, 表.类型, 表.数据,
表_1.数据 AS 数据1, 表_3.数据 AS 数据2, 表_2.数据 AS 数据3,
表_4.数据 AS 数据4
FROM 表, 表 表_1, 表 表_2,
表 表_3, 表 表_4
WHERE 表.时间 = 表_1.时间 AND 表.时间 = 表_2.时间 AND 表.时间 = 表_3.时间 AND 表.时间 = 表_4.时间
(表.类型 = '费率一') AND(表_1.类型 = '费率二') AND (表_2.类型 = '费率三') AND (表_3.类型 = '费率四') AND
(表_3.类型 = '费率五') AND 时间=2006-5
谢谢!这么多朋友支持!我自己也搞了一下
SELECT 表.时间, 表.类型, 表.数据,
表_1.数据 AS 数据1, 表_3.数据 AS 数据2, 表_2.数据 AS 数据3,
表_4.数据 AS 数据4
FROM 表, 表 表_1, 表 表_2,
表 表_3, 表 表_4
WHERe
(表.类型 = '费率一') AND(表_1.类型 = '费率二') AND (表_2.类型 = '费率三') AND (表_3.类型 = '费率四') AND
(表_3.类型 = '费率五') AND 时间=2006-5
create table t(时间 varchar(10),类型 varchar(10),数据 dec(10,1))
insert into t select '2005-6' ,'费率一' ,11.1
union all select '2005-6' ,'费率二' ,22.2
union all select '2005-6' ,'费率三' ,33.3
union all select '2005-6' ,'费率四' ,44.4
union all select '2005-6' ,'费率五' ,55.5
select distinct [id]=identity(int,1,1),类型 into # from t
declare @sql varchar(8000)
set @sql='select 时间'
select @sql=@sql+',['+类型+']=max(case 类型 when '''+类型+''' then 数据 else 0 end)' from # group by 类型,id order by id
exec(@sql+' from t group by 时间')
不好意思,繁體系統字符有點問題.
create table t(时间 varchar(6),类型 nvarchar(5),数据 decimal(10,1))
insert t select '2005-6',N'one',11.1
union all select '2005-6',N'two',22.2
union all select '2005-6',N'three',33.3
union all select '2005-6',N'four',44.4
union all select '2005-6',N'five',55.5
declare @s nvarchar(4000)
set @s=N'select 时间 '
select @s=@s+',['+类型+N']=sum(case when 类型='''+类型+N''' then 数据 else 0 end)'
from t group by 类型
set @s=@s+N' from t group by 时间 order by 时间 desc'
exec(@s)
drop table t
/*
时间 five four one three two
------ ---------------------------------------------- ----------------------------------------
2005-6 55.5 44.4 11.1 33.3 22.2
Create Table TEST
(时间 Varchar(6),
类型 Varchar(20),
数据 dec
)
--插入數據
Insert TEST Select '2005-6', '费率一', 11.1
Union All Select '2005-6', '费率二', 22.2
union All Select '2005-6', '费率三', 33.3
Union All Select '2005-6', '费率四', 44.4
Union All Select '2005-6', '费率五', 55.5
GO
Declare @S Varchar(1000)
Set @S=''
Select @S=@S+',SUM(Case 类型 When '''+类型+''' Then 数据 Else 0 End) As '+类型 From (Select Distinct 类型 From TEST) A Order By 类型
Set @S='Select 时间 '+@S+' From TEST Group By 时间 Order By 时间'
EXEC(@S)
create table t(时间 varchar(10),类型 varchar(10),数据 dec(10,1))
insert into t select '2005-6' ,'费率一' ,11.1
union all select '2005-6' ,'费率二' ,22.2
union all select '2005-6' ,'费率三' ,33.3
union all select '2005-6' ,'费率四' ,44.4
union all select '2005-6' ,'费率五' ,55.5
select 时间,
费率一=max(case 类型 when '费率一' then 数据 else 0 end),
费率二=max(case 类型 when '费率二' then 数据 else 0 end),
费率三=max(case 类型 when '费率三' then 数据 else 0 end),
费率四=max(case 类型 when '费率四' then 数据 else 0 end),
费率五=max(case 类型 when '费率五' then 数据 else 0 end)
from t
group by 时间
declare @sql varchar(8000)
set @sql='select 时间'
select @sql=@sql+',['+类型+']=max(case 类型 when '''+类型+''' then 数据 else 0 end)' from t group by 类型 order by 类型
exec(@sql+' from t group by 时间')
SELECT
时间=时间,
费率一=max(case when 类型='费率一' then 数据 else 0 end),
费率二=max(case when 类型='费率二' then 数据 else 0 end),
费率三=max(case when 类型='费率三' then 数据 else 0 end),
费率四=max(case when 类型='费率四' then 数据 else 0 end),
费率五=max(case when 类型='费率五' then 数据 else 0 end)
from 表
group by 时间
order by 时间
晕:再改:
select 时间,
费率一=max(case 类型 when '费率一' then 数据 end),
费率二=max(case 类型 when '费率二' then 数据 end),
费率三=max(case 类型 when '费率三' then 数据 end),
费率四=max(case 类型 when '费率四' then 数据 end),
费率五=max(case 类型 when '费率五' then 数据 end)
from 表名
group by 时间
select 时间,
费率一=max(case 类型 when '费率一' then 数据),
费率二=max(case 类型 when '费率二' then 数据),
费率三=max(case 类型 when '费率三' then 数据),
费率四=max(case 类型 when '费率四' then 数据),
费率五=max(case 类型 when '费率五' then 数据)
from 表名
group by 时间
select 时间,
费率一=max(case 类型 when '费率一' then 数据),
费率二=max(case 类型 when '费率二' then 数据),
费率三=max(case 类型 when '费率三' then 数据),
费率四=max(case 类型 when '费率四' then 数据),
费率五=max(case 类型 when '费率五' then 数据),
from 表名
group by 时间