sql 季度統計

2021-09-06 01:37:31 字數 2107 閱讀 5873

統計季度的sql:

--統計第一季度的資料,festivastartltime欄位是datetime型別.

select

*from

(select

quarter

=datename

(quarter,festivastartltime),

*from

festival) 

asquarterfestival

where

quarter=1

示例:usepubs

select

*from

sale

insert

into

sale 

values('

麵包','

食品',500,'

2009-01-01')

insert

into

sale 

values('

麵包','

食品',500,'

2009-02-01')

insert

into

sale 

values('

麵包','

食品',2000,'

2009-04-01')

insert

into

sale 

values('

麵包','

食品',3000,'

2009-07-01')

insert

into

sale 

values('

麵包','

食品',2000,'

2009-10-01')

insert

into

sale 

values('

核桃','

食品',2000,'

2009-11-01')

insert

into

sale 

values('

轎車','

汽車',2000,'

2009-01-01')

select'年

'=min(

datepart

(year

, saledate)),'季

'=datename

(quarter,saledate),'型別

'=pcat,

'本季銷售量'=

sum(pcount),

'截止本季銷售量'=

sum(pcount)

from

sale

group

bydatename

(quarter,saledate),pcat

--***************

select

datename

(quarter,saledate) 

from

sale

select

*from

sale

selecta.*

,(select

sum(pcount) 

from

sale 

where

year

(saledate)

=a.年

andpcat

=a.型別 

anddatename

(quarter,saledate)

<=

a.季) as'

截止本季銷售量

'from

(select

year

(saledate) as'

年',datename

(quarter,saledate) as'

季',pcat as'

型別',sum

(pcount) as'

本季銷售量

'from

sale

group

byyear

(saledate),

datename

(quarter,saledate),pcat

) as

aorder

by季 

asc

統計 Oracle按季度統計(詳細)

建立表 test cteate table test id number not null,modifiedtime date not null 按季度統計 select to char t.modifiedtime,yyyy year,to char t.modifiedtime,q time,c...

sql 獲取當前季度 (oracle)

笨辦法用case when 做判斷 select case when to char sysdate mm between 01 and 03 then 1 when to char sysdate mm between 04 and 06 then 2 when to char sysdate m...

SQL 按照季度,固定時間段,分組統計資料

最近在工作中接到了乙個需求,要求統計當月以10天為乙個週期,每個週期的資料彙總資訊。假設有一張表如下 表table test中 id amount create date 1 50 2017 01 01 2 50 2017 01 09 3 50 2017 01 11 4 50 2017 01 19 ...