mardi 28 mars 2017

How to use union query in laravel

I have a query in core php but i want to convert into Laravel, i am trying to do but that is giving the error. I don't know the exact method which i can use for this here is the core query

   select  mon,year,
        combo,
       registered,
       forwardedbycmo,
       clarification,
       noaction,
       disposed,mon_srno,undertaken
from monthly_activities
union
select extract('month' from actiondate) as mon,extract('year' from actiondate) as year,
        extract('year' from actiondate)|| '-' ||to_char(to_timestamp (extract('month' from actiondate)::text, 'MM'), 'TMMon') as combo,
       sum(case when actioncode = '00' then 1 else 0 end) as registered,
       sum(case when actioncode = '4T' and fromorg='CMOFF' then 1 else 0 end) as forwardedbycmo,
       sum(case when actioncode = '4D' and fromorg='CMOFF' then 1 else 0 end) as clarification,
       sum(case when actioncode = '10' then 1 else 0 end) as noaction,
       sum(case when actioncode = '50' then 1 else 0 end) as disposed,null as mon_srno,
        sum(case when actioncode = '40' and fromorg='CMOFF' then 1 else 0 end) as undertaken

from actionhistory where extract(month from actiondate)=extract(month from current_date)
and extract(year from actiondate)=extract(year from current_date)
 group by mon,year order by year,mon;

and laravel query is

$first = MonthlyActivity::select('mon','year','combo','registered','forwardedbycmo',
       'clarification',
       'noaction',
       'disposed','mon_srno','undertaken')->get();

        $second = MonthlyActivity::select(extract('month' from actiondate) as mon,extract('year' from actiondate) as year,
        extract('year' from actiondate)|| '-' ||to_char(to_timestamp (extract('month' from actiondate)::text, 'MM'), 'TMMon') as combo,
       sum(case when actioncode = '00' then 1 else 0 end) as registered,
       sum(case when actioncode = '4T' and fromorg='CMOFF' then 1 else 0 end) as forwardedbycmo,
       sum(case when actioncode = '4D' and fromorg='CMOFF' then 1 else 0 end) as clarification,
       sum(case when actioncode = '10' then 1 else 0 end) as noaction,
       sum(case when actioncode = '50' then 1 else 0 end) as disposed,null as mon_srno,
        sum(case when actioncode = '40' and fromorg='CMOFF' then 1 else 0 end) as undertaken

from actionhistory where extract(month from actiondate)=extract(month from current_date)
and extract(year from actiondate)=extract(year from current_date))->groupBy('mon','year')->orderBy('year','mon')->get();
 $result =  $first->union($second);

//$users = DB::table('users')->whereNull('last_name')->union($first)->get();
        return view('show',['monthly' => $result]);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire