1. 程式人生 > 實用技巧 >洛谷 P4059 [Code+#1]找爸爸

洛谷 P4059 [Code+#1]找爸爸

oracle按天,周,月,季度,年查詢排序

天--to_char(t.start_time,'YYYY-MM-DD')
周 --to_char(t.start_time,'YYYY'),to_char(t.start_time,'IW')
月度--to_char(t.start_time,'YYYY-MM')
季度--to_char(t.start_time,'YYYY'),to_char(t.start_time,'Q')
年度--to_char(t.start_time,'YYYY')

按天查詢

select to_char(t.start_time,'YYYY-MM-DD') day ,count(*) from test t 
where to_char(t.start_time,'YYYY')='2019' --條件限制
group by to_char(t.start_time,'YYYY-MM-DD') --分組
order by to_char(t.start_time,'YYYY-MM-DD') --排序

按周查詢

select to_char(t.start_time,'YYYY') year ,to_char(t.start_time,'IW'),count(*) from test t 
where to_char(t.start_time,'YYYY')='2019' --條件限制
group by to_char(t.start_time,'YYYY') year ,to_char(t.start_time,'IW')--分組
order by to_char(t.start_time,'YYYY') year,to_char(t.start_time,'IW') --排序

按月度查詢

select to_char(t.start_time,'YYYY-MM') ,count(*) from test t 
where to_char(t.start_time,'YYYY')='2019' --條件限制
group by to_char(t.start_time,'YYYY-MM') --分組
order byto_char(t.start_time,'YYYY-MM') --排序

按季度查詢

select to_char(t.start_time,'YYYY') year ,to_char(t.start_time,'Q'),count(*) from test t 
where to_char(t.start_time,'YYYY')='2019' --條件限制
group by to_char(t.start_time,'YYYY') ,to_char(t.start_time,'Q')--分組
order byto_char(t.start_time,'YYYY') ,to_char(t.start_time,'Q')--排序

按年度查詢

select to_char(t.start_time,'YYYY') year ,count(*) from test t 
where to_char(t.start_time,'YYYY')='2019' --條件限制
group by to_char(t.start_time,'YYYY') --分組
order by to_char(t.start_time,'YYYY') --排序

知識點擴充套件:oracle 實現按天,周,月,季度,年查詢統計資料

這裡提供了一種方法,挺不錯oracle 實現按周,月,季度,年查詢統計資料 。

還在網上看到用trunc來搞也可以,下面是個例子,兩句SQL效果一樣的.

id有重複的,所以group by搞了兩個欄位.

只在Oracle資料庫裡試過,其它庫沒試過。

 create table CONSUMER_ACC 
 ( 
 ID VARCHAR2(50) not null , 
 ACC_NUM VARCHAR2(10), 
 DATETIME DATE 
 ) 

 select t.id,trunc(t.datetime, 'mm' ) as d, sum (t.acc_num) as n 
 from CONSUMER_ACC t 
 --where 
 group by t.id,trunc(t.datetime, 'mm' ) 
 order by n desc ; 
 select t.id,to_char(t.datetime, 'mm' ) d , sum (t.acc_num) n 
 from CONSUMER_ACC t 
 --where 
 group by t.id,to_char(t.datetime, 'mm' ) 
 order by n desc 
------------------------------------------------------------------------------
//按天統計  
select count(dataid) as 每天運算元量, sum() 
from 
where 
group by trunc(createtime, 'DD')) 
//按自然周統計  
select to_char(date,'iw'),sum()  
from  
where  
group by to_char(date,'iw')  
//按自然月統計  
select to_char(date,'mm'),sum()  
from  
where  
group by to_char(date,'mm')  
//按季統計  
select to_char(date,'q'),sum()  
from  
where  
group by to_char(date,'q')  
//按年統計  
select to_char(date,'yyyy'),sum()  
from  
where  
group by to_char(date,'yyyy') 

總結

以上所述是小編給大家介紹的oracle實現按天,周,月,季度,年查詢排序方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對碼農教程網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!