首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一个sql语句,看似简单,却竟然都找不到解决方法

2012-02-17 
求助:一个sql语句,看似简单,却竟然都找不到解决办法目的:我希望在查询数据时,就把三条记录合成一条。例如:

求助:一个sql语句,看似简单,却竟然都找不到解决办法
目的:我希望在查询数据时,就把三条记录合成一条。
例如:
数据表结构及数据如下:
          code1     code2       date1                 stat         value        
          2             abc           2007-5-6         最大值         15.0                
          2             abc           2007-5-6         最小值         1.3        
          2             abc           2007-5-6         平均值         8.6        
          4             xyz           2007-5-7         最大值         7.5          
          4             xyz           2007-5-7         最小值         0.3          
          4             xyz           2007-5-7         平均值         4.3          

其中code1,code2,date1,stat联合起来是唯一的(主键)
我希望查出的数据结果类似这样:

    2     abc   2007-5-6     15.0     1.3     8.6
    4     xyz   2007-5-7     7.5       0.3     4.3    

即后三项就是最大、最小、平均值
望各位指点!!


[解决办法]
create table T(code1 int, code2 varchar(100), date1 datetime, stat varchar(100), value decimal(10,2))


insert into T select 2, 'abc ', '2007-5-6 ', '最大值 ',15.0
insert into T select 2, 'abc ', '2007-5-6 ', '最小值 ',1.3
insert into T select 2, 'abc ', '2007-5-6 ', '平均值 ',8.6
insert into T select 4, 'xyz ', '2007-5-7 ', '最大值 ',7.5
insert into T select 4, 'xyz ', '2007-5-7 ', '最小值 ',0.3
insert into T select 4, 'xyz ', '2007-5-7 ', '平均值 ',4.3


select a.code1,a.code2,a.date1,a.value as 最大值,b.value as 最小值,c.value as 平均值
from
(select * from T where stat= '最大值 ') as a
inner join (select * from T where stat= '最小值 ') as b on a.code1=b.code1 and a.code2=b.code2 and a.date1=b.date1
inner join (select * from T where stat= '平均值 ') as c on a.code1=c.code1 and a.code2=c.code2 and a.date1=c.date1


drop table T
[解决办法]
declare @t table(code1 int,code2 varchar(5), date1 varchar(10),stat varchar(10),value decimal(10,1))
insert @t
select 2, 'abc ', '2007-5-6 ', '最大值 ', 15.0 union all
select 2, 'abc ', '2007-5-6 ', '最小值 ', 1.3 union all
select 2, 'abc ', '2007-5-6 ', '平均值 ', 8.6 union all
select 4, 'xyz ', '2007-5-7 ', '最大值 ', 7.5 union all


select 4, 'xyz ', '2007-5-7 ', '最小值 ', 0.3 union all
select 4, 'xyz ', '2007-5-7 ', '平均值 ', 4.3

select code1,code2,date1,
'最大值 ' = max(case stat when '最大值 ' then value end),
'最小值 ' = max(case stat when '最小值 ' then value end),
'平均值 ' = max(case stat when '平均值 ' then value end)
from @t group by code1,code2,date1

/*结果
code1 code2 date1 最大值 最小值 平均值
----------- ----- ---------- ------------ ------------ -------
2 abc 2007-5-6 15.0 1.3 8.6
4 xyz 2007-5-7 7.5 .3 4.3
*/

[解决办法]
--借用ls数据
create table T(code1 int, code2 varchar(20), date1 datetime, stat varchar(20), value decimal(10,2))
insert into T select 2, 'abc ', '2007-5-6 ', '最大值 ',15.0
insert into T select 2, 'abc ', '2007-5-6 ', '最小值 ',1.3
insert into T select 2, 'abc ', '2007-5-6 ', '平均值 ',8.6
insert into T select 4, 'xyz ', '2007-5-7 ', '最大值 ',7.5
insert into T select 4, 'xyz ', '2007-5-7 ', '最小值 ',0.3
insert into T select 4, 'xyz ', '2007-5-7 ', '平均值 ',4.3

select code1,code2,date1,
最大值=max(case when stat= '最大值 ' then value else 0 end),
最小值=max(case when stat= '最小值 ' then value else 0 end),
平均值=max(case when stat= '平均值 ' then value else 0 end)
from T
group by code1,code2,date1

drop table T

2abc2007-05-06 00:00:00.00015.001.308.60
4xyz2007-05-07 00:00:00.0007.50.304.30

热点排行
Bad Request.