Home > database >  The SQL query problem, will only simple queries
The SQL query problem, will only simple queries

Time:10-03

The original table:
Name the date ZT
Zhang SAN's absence on Monday
Zhang SAN late Thursday
Xiao Ming Monday late
Xiao Ming late Wednesday

The query table:
The name on Monday Tuesday Wednesday Thursday
Zhang SAN's absence late
Xiao Ming was late late

The SQL statement should be how to write?

CodePudding user response:

 select name, 
Max (decode (date, on Monday, ZT)) as W1,
Max (decode (date, 'on Tuesday, ZT)) as W1,
. The rest of your add
The from mytable
Group by name

CodePudding user response:

 with t as (
Select 'zhang' as the name, 'Monday' as date1, 'absence as zt the from dual
Union all select 'zhang' as the name, 'Thursday as date1,' late 'as zt the from dual
Union all select 'Ming' as the name, 'Monday' as date1, 'late' as zt the from dual
Union all select 'Ming' as the name, 'on Wednesday as date1,' late 'as zt the from dual
)
- the first one:
Select the name,
Max (case when date1='Monday' then zt else 'end) on Monday,
Max (case when date1='Tuesday then zt else' end) on Tuesday,
Max (case when date1='Wednesday then zt else' end) on Wednesday,
Max (case when date1='Thursday then zt else' end) on Thursday,
Max (case when date1='Friday then zt else' end) on Friday,
Max (case when date1='Saturday then zt else' end) on Saturday,
Max (case when date1='Sunday then zt else' end) Sunday
The from t
Group by name

The second:
Select * from t
The pivot (Max (zt) for date1 in (' Monday ', 'on Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday' '))
- group by name

And what kind of 1 #, the three will do

CodePudding user response:

The demand of the transfer line column

 
the select * from tThe pivot (Max (zt) for date1 in (' Monday ', 'on Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday' '))
- group by name

This syntax to 11.2 to use in the future

CodePudding user response:

Suggest using pivot function
  • Related