Get the output using select sum case when - php

I am trying to display output in below format :
Error :
Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') as minrange, sum(case when DATE(holddate) = CURDATE()) as holddate, sum(case' at
code :
<?php
$sql =
"select
sum(case when DATE(reattemptdate) = CURDATE()) as minrange,
sum(case when DATE(holddate) = CURDATE()) as holddate,
sum(case when DATE(reattemptdate) = DATE(NOW() - INTERVAL 1 DAY)) as prev_reattemptdate,
sum(case when DATE(holddate) = DATE(NOW() - INTERVAL 1 DAY)) as prev_holddate
from orders
";
$results = $db_handle->runSelectQuery($sql);
$numrowsresult =$results[0]['count'];
echo $numrowsresult;
?>

I needed this : THEN 1 ELSE 0 END
select sum(case when DATE(reattemptdate) = CURDATE() THEN 1 ELSE 0 END) as reattemptdate,
sum(case when DATE(holddate) = CURDATE() THEN 1 ELSE 0 END) as holddate,
sum(case when DATE(reattemptdate) = DATE(NOW() - INTERVAL 1 DAY) THEN 1 ELSE 0 END) as prev_reattemptdate,
sum(case when DATE(holddate) = DATE(NOW() - INTERVAL 1 DAY) THEN 1 ELSE 0 END) as prev_holddate
from orders;

You have a CASE WHEN but you never specify the THEN (and ELSE).
Therefore the syntax is missing something.

Related

Mysql sum records which have tomorrow's date and after tomorrow etc

In my Mysql DB I have a column called 'mydate' with stored unix timestamps.
I need to get the SUM of all records, that:
has the tomorrow's date (the time shouldn't be taken into consideration, only date)
has today +2 days
has today +3 days
etc., but max. today +7 days
I did it this way (just an example for 2 upcoming days):
$sqld = "SELECT
sum(case when mydate= (CURDATE() + INTERVAL 1 DAY) then 1 else 0 end) AS p1,
sum(case when mydate= (CURDATE() + INTERVAL 2 DAY) then 1 else 0 end) AS p2
FROM mytable";
$rsld = $conn->Execute($sqld);
$x1=$rsld->fields['p1'];$x2=$rsld->fields['p2'];
But when I echo the $x1 and $x2, there is only 0 for both. Definitely there are records for tomorrow and after tomorrow in my table.
Am I doing it wrong?
If you are storing the datetimes as unix timestamps then you need to convert to datetime first and then remove the time portion. Try this
select
sum(case when date(FROM_UNIXTIME(mydate))=DATE_ADD(CURDATE(),INTERVAL 1 DAY) then 1 else 0 end) p1,
sum(case when date(FROM_UNIXTIME(mydate))=DATE_ADD(CURDATE(),INTERVAL 2 DAY) then 1 else 0 end) p2,
sum(case when date(FROM_UNIXTIME(mydate))=DATE_ADD(CURDATE(),INTERVAL 3 DAY) then 1 else 0 end) p3,
sum(case when date(FROM_UNIXTIME(mydate))=DATE_ADD(CURDATE(),INTERVAL 4 DAY) then 1 else 0 end) p4,
sum(case when date(FROM_UNIXTIME(mydate))=DATE_ADD(CURDATE(),INTERVAL 5 DAY) then 1 else 0 end) p5,
sum(case when date(FROM_UNIXTIME(mydate))=DATE_ADD(CURDATE(),INTERVAL 6 DAY) then 1 else 0 end) p6,
sum(case when date(FROM_UNIXTIME(mydate))=DATE_ADD(CURDATE(),INTERVAL 7 DAY) then 1 else 0 end) p7
from mytable
where date(FROM_UNIXTIME(mydate))>DATE_ADD(CURDATE(),INTERVAL 1 DAY) and date(FROM_UNIXTIME(mydate))<=DATE_ADD(CURDATE(),INTERVAL 7 DAY);
Ok, I think I found an easier solution.
I just define tomorrows and others date as variables:
$date = date("Y-m-d");
$d1 = strtotime('+1 day', strtotime($date));
$d2 = strtotime('+2 day', strtotime($date));
and then just simply compare in Mysql:
sum(case when mydate = $d1 then 1 else 0 end) AS p1
It looks like you're actually looking to count the number of rows instead of sum a particular field. You can use WHERE and GROUP BY to make it easier
SELECT mydate, count(id) as total
FROM mytable
WHERE STR_TO_DATE(mydate, '%Y-%m-%d') BETWEEN DATE_ADD(CURDATE(),INTERVAL 1 DAY) AND DATE_ADD(CURDATE(),INTERVAL 7 DAY)
This does turn it into multiple rows instead of a single row as your original query.

How to convert this complex query into active record CODEIGNITER PHP?

I have these queries in my ORACLE SQL:
SELECT dep.airport AS DEPAIRPORT,
dep.scheduled AS SCHEDULED,
dep.ontime AS ONTIME,
arr.arrontime AS ARRONTIME FROM (SELECT AIRPORT,sum(FIRSTFLIGHT) AS FIRSTFLIGHT, SUM(OTPFIRSTFLIGHT) as OTPFIRSTFLIGHT, SUM(ZMDFIRSTFLIGHT) as ZMDFIRSTFLIGHT,
COUNT(SCHEDULED) AS SCHEDULED,COUNT(ONTIME) AS ONTIME, COUNT(SCHEDARR) AS SCHEDARR FROM (SELECT DEPAIRPORT as AIRPORT,
MAX(case when A.STATUS = 'Scheduled' and
A.ACTUAL_BLOCKOFF is not null then 1 else NULL END) as SCHEDULED,
MAX(case when ((A.ACTUAL_BLOCKOFF+ interval '7' hour) - (A.SCHEDULED_DEPDT+ interval '7' hour))*24*60 <= '+000000015 00:00:00.000000000' and
A.ACTUAL_BLOCKOFF is not null then 1 else NULL END) as ONTIME,
MAX(case when A.STATUS = 'Scheduled' and A.ACTUAL_BLOCKON is not null then 1 else NULL END) as SCHEDARR
FROM TABLEA A left join TABLEB B ON A.FLIGHTLEGREF = B.FLIGHTLEGREF
where A.SERVICETYPE IN ('J','G') and to_char(SCHEDULED_DEPDT + interval '7' hour, 'yyyy-mm-dd') between '2018-07-15' and '2018-07-21'
and A.LATEST_ARRIVALAIRPORT != A.SCHED_DEPARTUREAIRPORT and (SUFFIX != 'R' or SUFFIX IS NULL)
group by DEPAIRPORT, AIRCRAFTREG,SCHEDULED_DEPDT,ACTUAL_BLOCKOFF,ACTUAL_BLOCKON,AIRCRAFTTYPE,SCHEDULED_ARRDT)GROUP BY AIRPORT) dep
left join (SELECT sched_arrivalairport AS airport,
count(CASE
WHEN( ( a.actual_blockon + interval '7' hour ) - (
a.scheduled_arrdt + interval '7' hour ) ) *
24 *
60
<=
'+000000015 00:00:00.000000000'
AND a.actual_blockon IS NOT NULL THEN 1
END) AS arrontime
FROM TABLEA A
where A.SERVICETYPE IN ('J','G') and to_char(SCHEDULED_DEPDT + interval '7' hour, 'yyyy-mm-dd') between '2018-07-15' and '2018-07-21'
and A.LATEST_ARRIVALAIRPORT != A.SCHED_DEPARTUREAIRPORT and (SUFFIX != 'R' or SUFFIX IS NULL)
GROUP BY sched_arrivalairport)arr
ON ( dep.airport = arr.airport )order by dep.airport `
and those codes works perfectly when I compiled it in oracle sql developer.
but when I converted to active record in code igniter the code didnt works.
here is my converted code in php:
$CI->db->select("DEP.AIRPORT AS DEPAIRPORT,
DEP.scheduled AS SCHEDULED,
DEP.ontime AS ONTIME,
ARR.arrontime AS ARRONTIME");
$CI->db->from("(");
$subq3 = $CI->db->get_compiled_select();
//$CI->db->reset_query();
$CI->db->select("DEPAIRPORT,sum(FIRSTFLIGHT) AS FIRSTFLIGHT, SUM(OTPFIRSTFLIGHT) as OTPFIRSTFLIGHT, SUM(ZMDFIRSTFLIGHT) as ZMDFIRSTFLIGHT,"
. "SUM(SCHEDULED) AS SCHEDULED,SUM(ONTIME) AS ONTIME");
$CI->db->from("(SELECT DEPAIRPORT as DEPAIRPORT,
MAX(case when A.STATUS = 'Scheduled' and
A.ACTUAL_BLOCKOFF is not null then 1 else NULL END) as SCHEDULED,
MAX(case when ((A.ACTUAL_BLOCKOFF+ interval '7' hour) - (A.SCHEDULED_DEPDT+ interval '7' hour))*24*60 <= '+000000015 00:00:00.000000000' and
A.ACTUAL_BLOCKOFF is not null then 1 else NULL END) as ONTIME FROM TABLEA A");$CI->db->join('TABLEB B', 'A.FLIGHTLEGREF = B.FLIGHTLEGREF','left');
$CI->db->where("A.LATEST_ARRIVALAIRPORT != A.SCHED_DEPARTUREAIRPORT"); $CI->db->where(divertStatus());
if ($stnService == "dom"){
$CI->db->where("A.SCHED_DEPARTUREAIRPORT IN (".$domstn.")");
}
if ($stnService == "int"){
$CI->db->where("A.SCHED_DEPARTUREAIRPORT IN (".$intstn.")");
}
if ($aptMgmt == "ap1"){
$CI->db->where_in("A.SCHED_DEPARTUREAIRPORT",$AP1);
}
if ($aptMgmt == "ap2"){
$CI->db->where_in("A.SCHED_DEPARTUREAIRPORT",$AP2);
}
if(!is_null($date1) && !is_null($date2))
$CI->db->where("to_char(SCHEDULED_DEPDT + interval '7' hour, 'yyyy-mm-dd') between '".$date1."' and '".$date2."'");
else $CI->db->where($timeLimit);
$CI->db->where("A.SERVICETYPE IN ('J','G')");
$CI->db->group_by("DEPAIRPORT, AIRCRAFTREG,SCHEDULED_DEPDT,ACTUAL_BLOCKOFF,ACTUAL_BLOCKON,AIRCRAFTTYPE,SCHEDULED_ARRDT) GROUP BY DEPAIRPORT) DEP ");
$subq1 = $CI->db->get_compiled_select();
//$CI->db->reset_query();
$CI->db->select("SCHED_ARRIVALAIRPORT AS AIRPORT,
count(CASE
WHEN( ( A.ACTUAL_BLOCKON + interval '7' hour ) - (
A.SCHEDULED_ARRDT + interval '7' hour ) ) *
24 *
60
<=
'+000000015 00:00:00.000000000'
AND A.ACTUAL_BLOCKON IS NOT NULL THEN 1
END) AS arrontime ");
$CI->db->from("TABLEA A ");
if(!is_null($date1) && !is_null($date2))
$CI->db->where("to_char(SCHEDULED_DEPDT + interval '7' hour, 'yyyy-mm-dd') between '".$date1."' and '".$date2."'");
else $CI->db->where($timeLimit);
$CI->db->where("A.SERVICETYPE IN ('J','G')");
$CI->db->where("A.LATEST_ARRIVALAIRPORT != A.SCHED_DEPARTUREAIRPORT"); $CI->db->where(divertStatus());
$CI->db->group_by("SCHED_ARRIVALAIRPORT)arr ON DEP.AIRPORT = ARR.AIRPORT");
$CI->db->order_by("DEP.DEPAIRPORT ASC");
$subq2 = $CI->db->get_compiled_select();
//$CI->db->reset_query();
$CI->db->query("$subq3 $subq1 LEFT JOIN ($subq2");
when I tried to compile the converted code it always produces result like this
Error Number: 904
ORA-00904: "DEP"."AIRPORT": invalid identifier
Did I miss something in my converted code ?
Any suggestion would be appreciated. Thankss
You can try to use Codeigniter query() function. Try this:
$your_query = "paste here your ORACLE SQL";
$result = $CI->db->query($your_query)->result_array();
This function return array of results. Docs here https://www.codeigniter.com/userguide3...

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]);
As per your question title, here is union reference for Laravel.
But seeing your problem as you have used native sql functions such as extract(), I will advise you to run query as raw query.
For heads up:
You use: $var = DB::connection()->getPdo()->quote($var);
to escape any variable you are using in query.
Prepare your raw query and put it in a variable like:
$query = 'SELECT ....';
Then you run it like:
$result = DB::select($query,[]);
Dont forget to include DB class at top of controller:
use Illuminate\Support\Facades\DB;
I am sorry I cannot give a lot of time to actually make a laravel query for you, so I suggested here which might help you go on.Good Luck!

Get open and closed posts count group by date with 1 week interval?

I need to get Posts count of closed and open posts grouped by date and with 1 week interval. what i'm getting right now is.
what i want is, like this.
sorry for poor explanation.
Thanks!
Please give it a try:
SELECT
date_posts - INTERVAL WEEKDAY(date_posts) DAY AS date_start,
date_posts + INTERVAL (6 - WEEKDAY(date_posts)) DAY AS date_start,
COUNT(*) totalPosts,
COUNT(CASE WHEN Close_count = 1 THEN 1 END) closeCount,
COUNT(CASE WHEN Open_count = 1 THEN 1 END) openCount
FROM your_table
GROUP BY YEARWEEK(date_posts,1);
Note:
YEARWEEK(date) function assumes the start date is Sunday.
YEARWEEK(date,1) function assumes the start date is Monday.
WEEKDAY(date) function returns the index of the date of the corresponding week assuming Monday as the start day of a week. It returns 0 for Monday, 1 for Tuesday ..... and 6 for Sunday.
EDIT:
For date range search:
SELECT
date_posts - INTERVAL WEEKDAY(date_posts) DAY AS date_start,
date_posts + INTERVAL (6 - WEEKDAY(date_posts)) DAY AS date_start,
COUNT(*) totalPosts,
COUNT(CASE WHEN Close_count = 1 THEN 1 END) closeCount,
COUNT(CASE WHEN Open_count = 1 THEN 1 END) openCount
FROM your_table
WHERE date_posts >= YOUR_START_DATE - INTERVAL WEEKDAY(YOUR_START_DATE) DAY
AND date_posts <= YOUR_END_DATE + INTERVAL (6 - WEEKDAY(YOUR_END_DATE)) DAY
GROUP BY YEARWEEK(date_posts,1);
So, based on this if you want to grab the result of last 5 weeks including current week you need to run the following query:
SELECT
date_posts - INTERVAL WEEKDAY(date_posts) DAY AS date_start,
date_posts + INTERVAL (6 - WEEKDAY(date_posts)) DAY AS date_start,
COUNT(*) totalPosts,
COUNT(CASE WHEN Close_count = 1 THEN 1 END) closeCount,
COUNT(CASE WHEN Open_count = 1 THEN 1 END) openCount
FROM your_table
WHERE date_posts >= (CURDATE() - INTERVAL 5 WEEK) - INTERVAL WEEKDAY(CURDATE() - INTERVAL 5 WEEK) DAY
AND date_posts <= CURDATE() + INTERVAL (6 - WEEKDAY(CURDATE())) DAY
GROUP BY YEARWEEK(date_posts,1);
SELECT DATE(i.time_added - INTERVAL WEEKDAY(i.time_added)DAY) AS date_start,
DATE(i.time_added + INTERVAL (6 - WEEKDAY(i.time_added))DAY ) AS date_end,
COUNT(DISTINCT i.lead_id) AS Leads_count,
COUNT(DISTINCT CASE WHEN i.`stage` IN
('6','8') THEN i.`lead_id`
ELSE NULL END) AS Close_count
,COUNT(DISTINCT CASE WHEN i.`stage` IN ('1','2','3','4','5','7','9')
THEN i.`lead_id` ELSE NULL END) AS Open_count
FROM tbl i
INNER JOIN tbl s ON i.stage = s.status_id
INNER JOIN tbl u ON i.assigned_to = u.userid
INNER JOIN tbl p ON p.purpose_id = i.purpose
WHERE 1=1 AND i.STATUS = 'on' AND i.time_added >= DATE_SUB(NOW(),INTERVAL 5 WEEK) -
INTERVAL WEEKDAY(DATE_SUB(NOW(),INTERVAL 5 WEEK)) DAY
AND i.time_added <= NOW() + INTERVAL (6 - WEEKDAY(NOW())) DAY
GROUP BY YEARWEEK(i.time_added,1)

Get today,this week,this month and this year data in single query in MySQL?

I am retrieving today's data from db,I want to retrieve this month,this year data as well,Is it possible in single query? or should I go with separate query for each items?
My query given below,
select sum(it.rate * it.quantity)as sales from invoices i
join invoice_items it on i.id = it.invoice_id
where i.invoice_date > DATE_SUB(NOW(), INTERVAL 1 DAY)
Use conditional aggregation:
select sum(case when i.invoice_date > DATE_SUB(NOW(), INTERVAL 1 DAY) then it.rate * it.quantity
end) as sales_1day,
sum(case when i.invoice_date > DATE_SUB(NOW(), INTERVAL 7 DAY) then it.rate * it.quantity
end) as sales_7day,
sum(case when i.invoice_date > DATE_SUB(NOW(), INTERVAL 1 MONTH) then it.rate * it.quantity
end) as sales_1month,
sum(case when i.invoice_date > DATE_SUB(NOW(), INTERVAL 1 YEAR) then it.rate * it.quantity
end) as sales_1year
from invoices i join
invoice_items it
on i.id = it.invoice_id

Categories