Oracle three practical statistics SQL scenarios, oracle statistics SQL scenarios
When using oracle for statistics, we often encounter the following scenarios:
1. Convert vertical columns to horizontal Columns
2. Group and merge a column as the result set
3. Obtain the first record for grouping and sorting
We useSimplifiedTo demonstrate how to use SQL to solve these three scenarios.
Business scenario: one table records employees' attendance records
Business Requirements: (corresponding to the preceding three scenarios)
1. count the number of monthly attendance records of employees in a certain year
2. query the attendance records of each person
3. obtain the attendance records for each employee on the first day of work.
First, create a test data table and test data.
SQL code
- -- Create an attendance record table
- Create table T_ATTENDANCE_LOG
- (
- Id_varchar (36 ),
- USERNAME _ VARCHAR (255 ),
- LOGDATE _ VARCHAR (100)
- )
- -- Initialize some test data
- Insert into T_ATTENDANCE_LOG (ID _, USERNAME _, LOGDATE _) VALUES ('1', 'zhang san', '2017-02-01 ');
- Insert into T_ATTENDANCE_LOG (ID _, USERNAME _, LOGDATE _) VALUES ('2', 'zhang san', '2017-02-02 ');
- Insert into T_ATTENDANCE_LOG (ID _, USERNAME _, LOGDATE _) VALUES ('3', 'zhang san', '2017-02-03 ');
- Insert into T_ATTENDANCE_LOG (ID _, USERNAME _, LOGDATE _) VALUES ('4', 'zhang san', '2017-02-04 ');
- Insert into T_ATTENDANCE_LOG (ID _, USERNAME _, LOGDATE _) VALUES ('5', 'zhang san', '2017-02-05 ');
- Insert into T_ATTENDANCE_LOG (ID _, USERNAME _, LOGDATE _) VALUES ('6', 'zhang san', '2017-02-06 ');
- Insert into T_ATTENDANCE_LOG (ID _, USERNAME _, LOGDATE _) VALUES ('11', 'lily', '2017-03-01 ');
- Insert into T_ATTENDANCE_LOG (ID _, USERNAME _, LOGDATE _) VALUES ('12', 'lily', '2017-04-01 ');
- Insert into T_ATTENDANCE_LOG (ID _, USERNAME _, LOGDATE _) VALUES ('13', 'lily', '2017-05-01 ');
- Insert into T_ATTENDANCE_LOG (ID _, USERNAME _, LOGDATE _) VALUES ('21', '王', '2017-02-15 ');
- Insert into T_ATTENDANCE_LOG (ID _, USERNAME _, LOGDATE _) VALUES ('22', '王', '2017-03-15 ');
- -- Query
- Select t. *, T. rowid from T_ATTENDANCE_LOG T;
Result:
1. Count the monthly attendance of employees in 2014
SQL code
- With sql1
- (
- Select USERNAME _, substr (LOGDATE _, 0, 7) as a, count (LOGDATE _) as B from T_ATTENDANCE_LOG
- Group by USERNAME _, substr (LOGDATE _, 0, 7)
- )
- Select USERNAME _,
- Sum (case A when '2014-01 'then B end) August 1, 2014,
- Sum (case A when '2014-02 'then B end) February 2014,
- Sum (case A when '2014-03 'then B end) February 2014,
- Sum (case A when '2017-04 'then B end) August 1, 2014,
- Sum (case A when '2017-05 'then B end) February 2014,
- Sum (case A when '2017-06 'then B end) February 2014,
- Sum (case A when '2014-07 'then B end) February 2014,
- Sum (case A when '2017-08 'then B end) February 2014,
- Sum (case A when '2014-09 'then B end) August 1, 2014,
- Sum (case A when '2017-10' then B end) February 2014,
- Sum (case A when '2017-11' then B end) February 2014,
- Sum (case A when '2017-12' then B end) August 1, 2014
- From sql1 group by USERNAME _
Here we use the "SQL statistics Tool" --.
Result:
2. query the attendance records of each person
SQL code
- Select USERNAME _ as employee, wmsys. wm_concat (LOGDATE _) as attendance record from T_ATTENDANCE_LOG t group by USERNAME _
Result:
However, we found that the statistical results are out of order.
SQL code
- Select USERNAME _ as employee, max (r) as attendance record from (
- Select USERNAME _, wmsys. wm_concat (LOGDATE _) OVER (partition by username _ order by logdate _) r
- From T_ATTENDANCE_LOG t
- ) Group by USERNAME _
Transformation Result:
3. obtain the attendance records for each employee on the first day of work.
SQL code
- SELECT * FROM
- (
- -- Group sorting and serial number
- Select USERNAME _, LOGDATE _, ROW_NUMBER () OVER (partition by username _ order by logdate _) r
- From T_ATTENDANCE_LOG t
- Group by USERNAME _, LOGDATE _
- ) Where R = 1
Result: