乙個按年季度查詢資料的
sql語句熱29
已有1527
次閱讀2009-06-03 15:52
今天在csdn
上回答的別人乙個問題,就是關於按年季度求和查詢資料
我給出了
2種資料庫的解決方案:
mssql
的:code:
select season = case when month(
日期) in (1,2,3) then '
一季度'
when month(
日期) in (4,5,6) then '
二季度'
when month(
日期) in (7,8,9) then '
三季度'
when month(
日期) in (10,11,12) then '
四季度' end ,
count(*)
from tablename
group by case when month(
日期) in (1,2,3) then '
一季度'
when month(
日期) in (4,5,6) then '
二季度'
when month(
日期) in (7,8,9) then '
三季度'
when month(
日期) in (10,11,12) then '
四季度' end
access
的code:
select iif(month(addtime) in (1,2,3),'
一季度',iif(month(addtime) in (4,5,6),'
二季度',iif(month(addtime) in (7,8,9),'
三季度','
四季度'))) as season, sum(num), year(addtime)
from riq
group by year(addtime), iif(month(addtime) in (1,2,3),'
一季度',iif(month(addtime) in (4,5,6),'
二季度',iif(month(addtime) in (7,8,9),'
三季度','
四季度')));
並給了我測試
access
用的一例子:
code:
dim conn
dim db
dim connstr
db = "db1.mdb"
on error resume next
set fgetcnnstr = server.createobject("adodb.connection")
fgetcnnstr.open connstr
if err then
err.clear
set fgetcnnstr = nothing
response.write " error......"
response.end()
end if
strsql ="select iif(month(addtime) in (1,2,3),'
一季度',iif(month(addtime) in (4,5,6),'
二季度',iif(month(addtime) in (7,8,9),'
三季度','
四季度'))) as season, sum(num) as znum, year(addtime) as pyear from riq group by year(addtime), iif(month(addtime) in (1,2,3),'
一季度',iif(month(addtime) in (4,5,6),'
二季度',iif(month(addtime) in (7,8,9),'
三季度','
四季度'))) order by year(addtime) desc , iif(month(addtime) in (1,2,3),'
一季度',iif(month(addtime) in (4,5,6),'
二季度',iif(month(addtime) in (7,8,9),'
三季度','
四季度')))"
set rs = server.createobject("adodb.recordset")
rs.open strsql,fgetcnnstr,1,1
if not rs.bof then
olddate=0
do while not rs.eof
if rs("pyear")<>olddate then response.write rs("pyear")"
年的統計結果
"""
response.write rs("season")":"&rs("znum")":""
"
olddate=rs("pyear")
rs.movenext
loop
end if
該資料庫中就只有二個字段:即
addtime
、znum
Mysql 查詢天 周,月,季度 年的資料
今天 select from 表名 where to days 時間欄位名 to days now 昨天 select from 表名 where to days now to days 時間欄位名 1近7天 select from 表名 where date sub curdate interva...
Mysql按年按月按日按周統計查詢
select date format created date,y m d as time,sum money money from o finance detail where org id 1000 group by time select date format created date,y ...
oracle 按每天,每月,每日,年查詢資料量
select from tablename md 按天統計 select to char md.createtime,yyyy mm dd as xx,count md.billmaindataid as 每天運算元量,sum md.naturalamount from tablename md w...