日期问题,各位请帮忙!!!(50分)

L

lzliang

Unregistered / Unconfirmed
GUEST, unregistred user!
在数据库中有一日期型字段Datetime,请问如何用一句SQL语言,
统计出某年(如1999年)或某月(如1999.12)其他字段的合计值?
 
某年:(1999)
select sum(Ufiled1) as sum1 ,sum(Ufield2) as sum2 ...
from Utable
where (theDate>='1999-1-1') and (theDate<2000-1-1')

某年某月:(1999-12)
select sum(Ufiled1) as sum1 ,sum(Ufield2) as sum2 ...
from Utable
where (theDate>='1999-12-1') and (theDate<2000-1-1')

 
什么数据库,取子串的方法也可以。

select name,substring(date,1,5),sum(a),sum(b) sum(c),sum(d) from
table1 group by name,substring(date,1,5)
 
SQL数据库:
select sum(Ufiled1) sum1 ,sum(Ufield2) sum2 from Table
where datepart(yy,datefiled)=1999 //1999年
//or month
where datepart(mm,datefiled)=5 //5月
 
接受答案了。
 
多人接受答案了。
 
顶部