sql - Inner join Logic in mysql -


i using inner join want combine 2 result display in singel table.
have created query check latecoming , earlyleaving want both output displayed 1 singel table.
query latecoming:

    select k.empid,min(k.dateofcheckin) mindate,     case      when min(time(dateofcheckin))<=@i 'ontime'     when min(time(dateofcheckin)) between @i , @j 'late'     when min(time(dateofcheckin)) between @j , @n 'halfday'     when dayofweek(date(dateofcheckin)) =7 ,  min(time(dateofcheckin)) >@j  'halfday'     else 'absent' end loginstatus     checkinlogs k join     (select      @i:=time(r.latecomer) latecoming,     @j:=time(r.latecomingendtime) latecomingend,     @m:=time(lunchstarttime),     @n:=time(lunchendtime),     @o:=time(earlyleavingstarttime),     @p:=time(earlyleavingendtime),      e.empid     employees e join profiles r on e.leaveprofile=r.profilename) h     on k.empid=h.empid     k.dateofcheckin='in'  ,       dayofweek(date(k.dateofcheckin)) != 1 ,     k.branchid=1 , date(dateofcheckin) between '2013-05-1' , '2013-05-29'     group date(k.dateofcheckin),k.empid 

query earlyleaving:

select      empid,date(dateofcheckin) logoutdate,dateofcheckin,max(time(dateofcheckin)) logouttime,     case      when max(time(dateofcheckin))>=@p 'ontime'     when dayofweek(date(dateofcheckin)) =7 ,  max(time(dateofcheckin)) < @m     'halfday'     when dayofweek(date(dateofcheckin)) =7 ,  max(time(dateofcheckin)) > @m  'ontime'     when max(time(dateofcheckin)) between @o , @p 'earlyleaving'     when max(time(dateofcheckin)) between @m  , @o 'halfday'     when max(time(dateofcheckin))<=@m 'absent'     else 'absent' end logoutstatus     checkinlogs k1 join     (select      @i:=time(r.latecomingstarttime) latecoming,     @j:=time(r.latecomingendtime) latecomingend,     @m:=time(lunchstarttime),     @n:=time(lunchendtime),     @o:=time(earlyleavingstarttime),     @p:=time(earlyleavingendtime),      e.empid     employees e join profiles r on e.leaveprofile=r.profilename) h1     on k1.empid=h1.empid     k1.dateofcheckin='out'      , k1.branchid=1     , dayofweek(date(k1.dateofcheckin)) != 1 ,       date(k1.dateofcheckin) between '2013-06-1' , '2013-06-29'     group date(k1.dateofcheckin),k1.empid 

below example of how i'd approach query, this quick copy , past job you'd have fix syntax errors etc. main difference need employee details once @ start of query , add common field between 2 current queries (other employee) join on. in case have used logoutdate

select * (select      @i:=time(r.latecomer) latecoming,     @j:=time(r.latecomingendtime) latecomingend,     @m:=time(lunchstarttime),     @n:=time(lunchendtime),     @o:=time(earlyleavingstarttime),     @p:=time(earlyleavingendtime),      e.empid employees e join profiles r on e.leaveprofile=r.profilename) h join (select          empid, date(dateofcheckin) logoutdate,         dateofcheckin,max(time(dateofcheckin)) logouttime,         case              when max(time(dateofcheckin))>=@p 'ontime'             when dayofweek(date(dateofcheckin)) =7 ,  max(time(dateofcheckin)) < @m                 'halfday'             when dayofweek(date(dateofcheckin)) =7 ,  max(time(dateofcheckin)) > @m                  'ontime'             when max(time(dateofcheckin)) between @o , @p 'earlyleaving'             when max(time(dateofcheckin)) between @m  , @o 'halfday'             when max(time(dateofcheckin))<=@m 'absent'         else 'absent' end logoutstatus       checkinlogs k1           k1.dateofcheckin='out'        , k1.branchid=1       , dayofweek(date(k1.dateofcheckin)) != 1       group date(k1.dateofcheckin), k1.empid) out on out.empid = h.empid join (select k.empid, date(dateofcheckin) logoutdate,                 min(k.dateofcheckin) mindate,                 case                      when min(time(dateofcheckin))<=@i 'ontime'                     when min(time(dateofcheckin)) between @i , @j 'late'                     when min(time(dateofcheckin)) between @j , @n 'halfday'                     when dayofweek(date(dateofcheckin)) =7                          , min(time(dateofcheckin)) >@j  'halfday'                     else 'absent' end loginstatus       checkinlogs k join         k.dateofcheckin='in'  ,           dayofweek(date(k.dateofcheckin)) != 1 ,         group date(k.dateofcheckin),k.empid) in on in.empid = out.empid , in.logoutdate = out.logoutdate 

i consider moving case , group bys top level query.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -