一张表
日期 气温
2001-1-1 15
...............
2008-8-31 152
现在想得到这样的查询结果
年份 春季气温 夏季气温 秋季气温 冬季气温这里
春季指2,3,4月
夏季指5,6,7月
秋季指8,9,10月
冬季指11,12,次年1月
日期 气温
2001-1-1 15
...............
2008-8-31 152
现在想得到这样的查询结果
年份 春季气温 夏季气温 秋季气温 冬季气温这里
春季指2,3,4月
夏季指5,6,7月
秋季指8,9,10月
冬季指11,12,次年1月
select substring(日期,1,4),
sum(addpart(m,日期) in (2,3,4))/ sum(case when addpart(m,日期) in (2,3,4) then 1 else 0 end),
sum(addpart(m,日期) in (5,6,7))/ sum(case when addpart(m,日期) in (5,6,7) then 1 else 0 end),
sum(addpart(m,日期) in (8,9,10))/ sum(case when addpart(m,日期) in (8,9,10) then 1 else 0 end),
sum(addpart(m,日期) in (11,12,1))/ sum(case when addpart(m,日期) in (11,12,1) then 1 else 0 end),
group by substring(日期,1,4)
select substring(日期,1,4),
sum(case when DATEPART(m,日期) in (2,3,4) then 气温 else 0 end)/ sum(case when DATEPART(m,日期) in (2,3,4) then 1 else 0 end) as 春,
sum(case when DATEPART(m,日期) in (5,6,7) then 气温 else 0 end )/ sum(case when DATEPART(m,日期) in (5,6,7) then 1 else 0 end) as 夏,
sum(case when DATEPART(m,日期) in (8,9,10) then 气温 else 0 end)/ sum(case when DATEPART(m,日期) in (8,9,10) then 1 else 0 end) as 秋,
sum(case when DATEPART(m,日期) in (11,12,1) then 气温 else 0 end)/ sum(case when DATEPART(m,日期) in (11,12,1) then 1 else 0 end) as 冬
group by substring(日期,1,4)
(
date datetime,
tmp float
)insert #t select '2008-1-1',1
insert #t select '2008-2-1',2
insert #t select '2008-3-1',3
insert #t select '2008-3-2',15
insert #t select '2008-4-1',4
insert #t select '2008-5-1',5
insert #t select '2008-6-1',6
insert #t select '2008-7-1',7
insert #t select '2008-8-1',8
insert #t select '2008-9-1',9
insert #t select '2008-10-1',9
insert #t select '2008-11-1',12
insert #t select '2008-12-1',12
insert #t select '2009-1-1',1
insert #t select '2009-2-1',2
insert #t select '2009-3-1',3
insert #t select '2009-4-1',4
insert #t select '2009-5-1',5
insert #t select '2009-6-1',6
insert #t select '2009-7-1',7
insert #t select '2009-8-1',8
insert #t select '2009-9-1',9
insert #t select '2009-10-1',9
insert #t select '2009-11-1',12
insert #t select '2009-12-1',12
go
select 年份,sum(春) 春,sum(夏) 夏,sum(秋) 秋,sum(冬) 冬 from
(
select 年份,
case when 季节 = '春' then sum(总温度)/sum(总天数) else 0 end 春,
case when 季节 = '夏' then sum(总温度)/sum(总天数) else 0 end 夏,
case when 季节 = '秋' then sum(总温度)/sum(总天数) else 0 end 秋,
case when 季节 = '冬' then sum(总温度)/sum(总天数) else 0 end 冬
from
(
select left(convert(char(6),date,112),4) 年份,
case when right(convert(char(6),date,112),2) in ('02','03','04') then '春'
when right(convert(char(6),date,112),2) in ('05','06','07') then '夏'
when right(convert(char(6),date,112),2)in ('08','09','10') then '秋'
when right(convert(char(6),date,112),2)in ('11','12','01') then '冬' end 季节,
sum(tmp) 总温度,count(*) 总天数
from #t
group by convert(char(6),date,112)
)tbl
group by 年份,季节
) tbl
group by 年份
sum(case when DATEPART(m,date) in (2,3,4) then tmp else 0 end)/ sum(case when DATEPART(m,date) in (2,3,4) then 1 else 0 end) as 春,
sum(case when DATEPART(m,date) in (5,6,7) then tmp else 0 end )/ sum(case when DATEPART(m,date) in (5,6,7) then 1 else 0 end) as 夏,
sum(case when DATEPART(m,date) in (8,9,10) then tmp else 0 end)/ sum(case when DATEPART(m,date) in (8,9,10) then 1 else 0 end) as 秋,
sum(case when DATEPART(m,date) in (11,12,1) then tmp else 0 end)/ sum(case when DATEPART(m,date) in (11,12,1) then 1 else 0 end) as 冬
from #t
group by substring(convert(char(8),date,112),1,4)
谢谢楼上 疏忽了 应该是
select substring(convert(varchar(8),getdate(),112),1,4)
不要使用
select substring(convert(char(8),getdate(),112),1,4)
因为char会剩余4位 对以后可能造成不好影响
/code]
小弟有点糊涂了,忘指教select substring(convert(char(8),getdate(),112),1,4)
返回结果的类型是char,长度是8吗?
我说的剩余4位不是长度来讲select len(substring(convert(varchar(8),getdate(),112),1,4) )
select len(substring(convert(char(8),getdate(),112),1,4) )
都是返回4Char是固定长度的字符型,如果添加的字符长度不够,SQL自动用空格补齐,
VarChar是不固定长度的字符型(只有最大长度)即可变长度的字符型,添加的字符长度不够时,SQL不会用空格补齐。
sum(case when datepart(qq,日期)=1 then 气温 else 0 end)/sum(case when datepart(qq,日期)=1 then 1 else 0 end) as 春季气温,
sum(case when datepart(qq,日期)=2 then 气温 else 0 end)/sum(case when datepart(qq,日期)=2 then 1 else 0 end) as 夏季气温,
sum(case when datepart(qq,日期)=3 then 气温 else 0 end)/sum(case when datepart(qq,日期)=3 then 1 else 0 end) as 秋季气温,
sum(case when datepart(qq,日期)=4 then 气温 else 0 end)/sum(case when datepart(qq,日期)=4 then 1 else 0 end) as 冬季气温
from 表
group by year(日期)
以2007年为例,冬季指2007年11和12月以及2008年1月
顺便问下面的小问题select * from
(
select .............
)
这种语法是哪种数据库支持的,或是哪一版本的sql在于server支持,我在sql server 2005 express里试了不支持,
sql server 2000不方便试,不在我所在的房间
(
select .............
)A
,min(case when 季度=2 then 平均气温 end) 夏季气温
,min(case when 季度=3 then 平均气温 end) 秋季气温
,min(case when 季度=4 then 平均气温 end) 冬季气温
from
( select year(dateadd(mm,-1,日期)) as 年份,datepart(qq,dateadd(mm,-1,日期)) 季度,avg(气温)平均气温
from 表名 group by year(dateadd(mm,-1,日期)),datepart(qq,dateadd(mm,-1,日期))
)ta group by 年份