Statistics in Oracle by day, week, month, quarter, and year

Introduction:

       Oracle realizes data statistics according to day, week, month, quarter, and year. In actual business scenarios, such as "reports and the like", "financial business transactions", etc., business often requires us to count the data of the whole year or the whole month, etc. , let's get straight to the point.

illustrate:

  Raw data

 

1. Query by day

1. Fragment

select to_char(x.time, 'yyyy-mm-dd') as day, count(1), sum(x.sum) from XIAO x 
 where to_char(x.time, 'yyyy-mm-dd') = '2023-04-23'
 group by to_char(x.time, 'yyyy-mm-dd')
 order by to_char(x.time, 'yyyy-mm-dd')

2. The result set

day counts 2023-04-23, count(1) counts 2023-04-23 with two data, sum counts 2023-04-23 and adds up the two data.

2. Query by week

1. Fragment

select to_char(x.time, 'yyyy') as week,to_char(x.time, 'IW'), count(1) from XIAO x    
 where to_char(x.time, 'yyyy') = '2023'
 group by to_char(x.time, 'yyyy'),to_char(x.time, 'IW')
 order by to_char(x.time, 'yyyy'),to_char(x.time, 'IW')

2. The result set

3. Query by month

1. Fragment

select to_char(x.time, 'yyyy-MM') as month, count(1) from XIAO x    
 where to_char(x.time, 'yyyy') = '2023'
 group by to_char(x.time, 'yyyy-MM')
 order by to_char(x.time, 'yyyy-MM')

2. The result set

4. Quarterly Statistics

1. Fragment

select to_char(x.time, 'yyyy') as quarter,to_char(x.time, 'Q'), count(1) from XIAO x    
 where to_char(x.time, 'yyyy') = '2023'
 group by to_char(x.time, 'yyyy'),to_char(x.time, 'Q')
 order by to_char(x.time, 'yyyy'),to_char(x.time, 'Q')

2. The result set

5. Statistics by year

1. Fragment

select to_char(x.time, 'yyyy') as year, count(1) from XIAO x    
 where to_char(x.time, 'yyyy') = '2023'
 group by to_char(x.time, 'yyyy')
 order by to_char(x.time, 'yyyy')

2. The result set

attach 

 The difference between oracle date function IW and WW

WW
     January 1st of each year is regarded as the first day of the first week of the year (regardless of the day of the week on January 1st of the year); for example: 
     2011/01/01 is a Saturday, which is defined as 2011 in Oracle the first day of the first week of ; 

select to_char(TO_DATE('20230101', 'YYYYMMDD'), 'yyyyww') as week1 from dual

IW

    Oracle date and time functions  IW and  WW both are used to work with dates and times, but there are some differences between them.

First, WW functions are used to deal with the standard day of the week, and  IW functions are used to deal with the standard format of dates and times. Specifically, WW the function converts a date or time string to a date or time in the ISO 8601 standard format, and the  IW function converts a date or time string to a local date and time format.

Second, WW the function extracts the hours, minutes, and seconds from a date or time string, respectively, and converts them to hours, minutes, and seconds in a 12-hour clock. Instead,  IW the function leaves the hours, minutes, and seconds of the date or time string as-is without conversion.

In addition, WW the function extracts the date part of a date or time string and converts them to the date part in the ISO 8601 standard format. Instead,  IW the function leaves the date portion of the date or time string as-is without conversion.

In short, both WW functions and  IW functions can be used to process dates and times, but the difference between them lies in the standards and formats they deal with. In actual use, it is necessary to select a suitable function according to the specific situation.

append sql fragment

 Oracle asks the current date is the day, week, month, and quarter of this year.

select 
to_char(TO_DATE('20230101','YYYYMMDD') ,'yyyyiw')  as week, --oracle求当年的第几周
to_char(TO_DATE('20230101','YYYYMMDD') ,'yyyyww')  as week1,--oracle求当年的第几周
to_char(TO_DATE('20230101','YYYYMMDD'),'yyyy') as year,--oracle求第几年
to_char(TO_DATE('20230101','YYYYMMDD') ,'yyyymm') as month,--oracle求当年的第几月
to_char(TO_DATE('20230101','YYYYMMDD'),'yyyyddd') as day,--oracle求当年的第几天
to_char(TO_DATE('20230401','YYYYMMDD') ,'yyyyq') as quarter-- oracle求当年的第几季度
from dual

result set

Guess you like

Origin blog.csdn.net/weixin_50002038/article/details/130419040