Home > database > Number of days per month complex SQL queries
Number of days per month complex SQL queries
Time:10-04
Table structure such as
The results to be like this
Can use SQL directly write?
CodePudding user response:
As
with t(select 'zhang' names, 'at 10:30 btime,' 11:30 etime, 'a' type, '2016/11/1 rq from dual union all Select 'zhang' names, '12:30 btime,' and 'etime,' a 'type,' 2016/11/1 rq from dual union all Select 'zhang' names, 'yea btime,' 11:30 etime, 'a' type, '2016/11/3 rq from dual union all Select 'zhang' names, '18:30 btime,' 7:30 etime, 'a' type, '2016/11/4 rq from dual) The select a.n Ames, a. time, a.e time, a.t ype, b.b time, b.e time, b.t ype from (select m. *, row_number () over (partition by names order by rq, btime) rn The from t m where rq='2016/11/1) a Full outer join (select m. *, row_number () over (partition by names order by rq, btime) rn The from t m where rq='2016/11/3) b On a.n Ames=b.n Ames and a.r n=b.r n ;
Very trouble, give you an idea, help you wrote 2 days
CodePudding user response:
Very trouble, give you an idea, help you wrote 2 days, Every day you have to create a child query The (select m. *, row_number () over (partition by names order by rq, btime) rn The from t m where rq='2016/11/1'), and then the second subquery associated rq='2016/11/2', with a full outer join associations, a.n Ames=b.n Ames and a.r n=b.r n match,
Other only 2 days with a full outer join, if you are then connected to the third day, there will be some problems, so you will need a lot of nested sub-queries and,