join a query multiply the results - php

I have this query and works great. it gives to me some kpis about time, related to an equipment x, and this work selecting the shifts to evaluate. I need to join the information comming from a second query, that works whit the same id of shifts to work, but when i Add the parameters of this query to the first one, the results becomes incremented, for instance, the sum of the firs values gives 24 (24 hrs) but when i relate this query with the second one, te results increments its values and i cant realize whats wrong.
The first query:
SELECT *
,(Efectivo+Demora+Reserva)/(Efectivo+Demora+Reserva+Mantencion+Averia+Accidente)*100 as [Disp_Fisica]
,IsNULL((Efectivo+Demora+Reserva)/NULLIF((Efectivo+Demora+Reserva+Mantencion+Averia),0),0)*100 as [Disp_Mecanica]
,IsNULL((Efectivo+Demora)/NULLIF((Efectivo+Demora+Reserva),0),0)*100 as [Uso_Disponib]
,IsNULL(Efectivo/NULLIF((Efectivo+Demora),0),0)*100 as [Efic_Operacional]
FROM (
SELECT hist_eqmtlist.unit,
hist_statusevents.eqmt,
sum(case when hist_statusevents.category =1 then (hist_statusevents.duration/3600) else 0 end) as [Efectivo],
sum(case when hist_statusevents.category =2 then (hist_statusevents.duration/3600) else 0 end) as [Demora],
sum(case when hist_statusevents.category =3 then (hist_statusevents.duration/3600) else 0 end) as [Reserva],
sum(case when hist_statusevents.category =4 then (hist_statusevents.duration/3600) else 0 end) as [Mantencion],
sum(case when hist_statusevents.category =5 then (hist_statusevents.duration/3600) else 0 end) as [Averia],
sum(case when hist_statusevents.category =6 then (hist_statusevents.duration/3600) else 0 end) as [Accidente]
FROM hist_eqmtlist ,hist_statusevents, hist_exproot
WHERE hist_eqmtlist.unit = 'Pala'
and hist_exproot.shiftindex between '29976' and '29977'
and hist_statusevents.shiftindex = hist_eqmtlist.shiftindex
and hist_statusevents.eqmt = hist_eqmtlist.eqmtid
and hist_exproot.shiftindex = hist_statusevents.shiftindex
GROUP BY hist_statusevents.eqmt, hist_eqmtlist.unit
) A
order by A.eqmt
the result given by this query:
unit eqmt Efectivo Demora Reserva Mantencion Averia Accidente Disp_Fisica Disp_Mecanica Uso_Disponib Efic_Operacional
Camion CAM336 16,8 2,6 4,35 0 0 0,2 99,0 100 81,8 86,4
the second query gives to me the tons by equipment
SELECT hist_exproot.shiftdate , hist_dumps.shiftindex, hist_dumps.truck, sum(hist_dumps.dumptons) AS 'Toneladas'
FROM Powerview.dbo.hist_dumps hist_dumps, Powerview.dbo.hist_exproot hist_exproot
WHERE hist_dumps.shiftindex = hist_exproot.shiftindex
GROUP BY hist_exproot.shiftdate, hist_dumps.truck, hist_dumps.shiftindex
ORDER BY shiftdate
The result given by this query is the amount of tons by shift by equip
shiftdate shiftindex truck Toneladas
2011-01-01 29950 CAM336 8964,00054931641
I Need to join both querys but when i do it, it multiply the values, how shoukd i build the relations?
Im trying adding this on the select part, under the sums:
and on where:
hist_exproot.shiftindex
and the query becomes this:
SELECT *
,(Efectivo+Demora+Reserva)/(Efectivo+Demora+Reserva+Mantencion+Averia+Accidente)*100 as [Disp_Fisica]
,IsNULL((Efectivo+Demora+Reserva)/NULLIF((Efectivo+Demora+Reserva+Mantencion+Averia),0),0)*100 as [Disp_Mecanica]
,IsNULL((Efectivo+Demora)/NULLIF((Efectivo+Demora+Reserva),0),0)*100 as [Uso_Disponib]
,IsNULL(Efectivo/NULLIF((Efectivo+Demora),0),0)*100 as [Efic_Operacional]
FROM (
SELECT hist_eqmtlist.unit,
hist_statusevents.eqmt,
sum(case when hist_statusevents.category =1 then (hist_statusevents.duration/3600) else 0 end) as [Efectivo],
sum(case when hist_statusevents.category =2 then (hist_statusevents.duration/3600) else 0 end) as [Demora],
sum(case when hist_statusevents.category =3 then (hist_statusevents.duration/3600) else 0 end) as [Reserva],
sum(case when hist_statusevents.category =4 then (hist_statusevents.duration/3600) else 0 end) as [Mantencion],
sum(case when hist_statusevents.category =5 then (hist_statusevents.duration/3600) else 0 end) as [Averia],
sum(case when hist_statusevents.category =6 then (hist_statusevents.duration/3600) else 0 end) as [Accidente],
sum (hist_dumps.dumptons) as [Toneladas]
FROM hist_eqmtlist ,hist_statusevents, hist_exproot,hist_dumps
WHERE hist_eqmtlist.unit = 'Pala'
and hist_exproot.shiftindex between '29976' and '29977'
and hist_statusevents.shiftindex = hist_eqmtlist.shiftindex
and hist_statusevents.eqmt = hist_eqmtlist.eqmtid
and hist_exproot.shiftindex = hist_statusevents.shiftindex
and hist_exproot.shiftindex = hist_dumps.shiftindex
GROUP BY hist_statusevents.eqmt, hist_eqmtlist.unit
) A
order by A.unit
and when i need to keep the resutls for the tons of and the indicators given by the querys when the arent together, the resutls turns into this when the sum of "efectivo, demora reserva and accidente" shouldn't result more than 24 (24 hrs)
unit eqmt Efectivo Demora Reserva Accidente Toneladas
Camion CAM336 9678 1498 2156 109 2342022

Related

How to sum the returned results in the same query?

How can I sum the sum-ed results (goal + delivered) in a new column, in the same query?
SELECT Sum(CASE
WHEN the_status = 'goal' THEN 1
ELSE 0
END) AS goal,
Sum(CASE
WHEN the_status = 'delivered' THEN 1
ELSE 0
END) AS delivered
FROM the_data
where ....
You could just add a third column with both conditions:
SELECT Sum(CASE
WHEN the_status = 'goal' THEN 1
ELSE 0
END) AS goal,
Sum(CASE
WHEN the_status = 'delivered' THEN 1
ELSE 0
END) AS delivered,
Sum(CASE
WHEN the_status IN ('goal', 'delivered') THEN 1
ELSE 0
END) AS goal_and_delivered
FROM the_data

Dynamic Pivot MYSQL incomplete SQL

I have been having trouble with a project i've been working on for about 5 weeks now, i've made various stackoverflow posts along the way and i'm almost at the final hurdle.
I was having issues with duplicated data searching weekly sums but I seem to have figured that out but now my statement isn't completing.
REf:
Weekly Sum Dynamic Pivot MYSQL
Here is a fiddle with the data.
http://sqlfiddle.com/#!9/a3610
$period = 'YEARWEEK';
$sql = "
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(CASE WHEN (".$period."(date)) = ',
(".$period."(date)),
' THEN AMOUNT else 0 END) AS `',
(".$period."(date)),
'`'
)
ORDER BY date ASC ) AS `pivot_columns`
FROM record_offering
WHERE date BETWEEN ? AND ?
ORDER BY date ASC
";
$stmt = $pdo->prepare($sql);
$date_from = '2017-01-01';
$date_to = '2017-10-01';
$stmt->execute([$date_from, $date_to]);
$row = $stmt->fetch();
$stmt->closeCursor();
$pivot_columns = $row['pivot_columns'];
$sql = "
SELECT title AS `Service`, {$pivot_columns}
from record_offering t1
join setting_service ON t1.service_id = setting_service.id
WHERE t1.date BETWEEN ? AND ?
GROUP BY title asc WITH ROLLUP
";
$stmt = $pdo->prepare($sql);
$stmt->execute([$date_from, $date_to]);
$results = $stmt->fetchAll();
$stmt->closeCursor();
As you can see the last statement is incomplete:
SELECT title AS `Service`, SUM(CASE WHEN (YEARWEEK(date)) = 201635 THEN AMOUNT else 0 END) AS `201635`,
SUM(CASE WHEN (YEARWEEK(date)) = 201703 THEN AMOUNT else 0 END) AS `201703`,
SUM(CASE WHEN (YEARWEEK(date)) = 201709 THEN AMOUNT else 0 END) AS `201709`,
SUM(CASE WHEN (YEARWEEK(date)) = 201713 THEN AMOUNT else 0 END) AS `201713`,
SUM(CASE WHEN (YEARWEEK(date)) = 201715 THEN AMOUNT else 0 END) AS `201715`,
SUM(CASE WHEN (YEARWEEK(date)) = 201717 THEN AMOUNT else 0 END) AS `201717`,
SUM(CASE WHEN (YEARWEEK(date)) = 201718 THEN AMOUNT else 0 END) AS `201718`,
SUM(CASE WHEN (YEARWEEK(date)) = 201722 THEN AMOUNT else 0 END) AS `201722`,
SUM(CASE WHEN (YEARWEEK(date)) = 201723 THEN AMOUNT else 0 END) AS `201723`,
SUM(CASE WHEN (YEARWEEK(date)) = 201725 THEN AMOUNT else 0 END) AS `201725`,
SUM(CASE WHEN (YEARWEEK(date)) = 201726 THEN AMOUNT else 0 END) AS `201726`,
SUM(CASE WHEN (YEARWEEK(date)) = 201735 THEN AMOUNT else 0 END) AS `201735`,
SUM(CASE WHEN (YEARWEEK(date)) = 201736 THEN AMOUNT else 0 END) AS `201736`,
SUM(CASE WHEN (YEARWEEK(date)) = 201
from record_offering t1
join setting_service ON t1.service_id = setting_service.id
WHERE t1.`date` BETWEEN ? AND ?
GROUP BY title asc WITH ROLLUP
I have tried escaping the query in various ways but either the query completes and my data is duplicated or it doesn't compile at all.
GROUP_CONCAT has a limit off 1024 bytes.
Use
SET SESSION group_concat_max_len = ##max_allowed_packet
Before the GROUP_CONCAT query.
To my way of thinking, this (or something very like it) is ALL the sql you need for this problem. Everything else can, and should, be handled in the presentation layer.
SELECT YEARWEEK(x.date) yw
, x.title
, COALESCE(SUM(y.amount),0) total
FROM setting_service x
LEFT
JOIN record_offering y
ON y.service_id = x.id
GROUP
BY yw
, x.id;

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!

Closing percent MATH in MYSQL

I want to do math in a query, and was wondering if its better to do it in PHP or MYSQL.
Also, if I choose MYSQL can anyone help me with the query.
So far I have
SELECT COUNT(*) as total, booker, appdate,
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) book,
SUM(CASE WHEN status!='DNS' THEN 1 ELSE 0 END) tot
FROM appts WHERE WEEK(app_date)= WEEK(CURDATE()) GROUP BY booker
I want one more stat from this query.
I want to do book / (book+tot)
But obviously only if book!=0 or tot!=0, since obviously I don't want to divide anything by zero.
Is there a way to do this in a MYSQL query??
I want my output to be.....
book | 14
tot | 25
hold | 35%
Id also like to ORDER BY the hold percent from highest to lowest. Is this possible????
You can achieve what you ask for using a subquery, like this:
SELECT *, IF(book + tot, 100*book/(book + tot), NULL) AS hold
FROM (
SELECT COUNT(*) as total, booker, appdate,
SUM(status='DNS') book, SUM(status!='DNS') tot
FROM appts WHERE WEEK(app_date)= WEEK(CURDATE()) GROUP BY booker
) AS subquery
ORDER BY hold DESC
Note that in several places I'm using the fact that MySQL uses numbers for logical values. So you can sum up conditions without CASE, and you can write a formula for IF without <> 0 check.
Naive method:
SELECT
COUNT(*) as total,
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) book,
SUM(CASE WHEN status!='DNS' THEN 1 ELSE 0 END) tot,
IF( ( SUM(CASE WHEN status!='DNS' THEN 1 ELSE 0 END) +
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) ) = 0,
0,
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) /
( SUM(CASE WHEN status!='DNS' THEN 1 ELSE 0 END) +
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) )
) AS hold
FROM appts
WHERE WEEK(app_date) = WEEK(CURDATE()) GROUP BY booker
ORDER BY hold;
Or in order not to repeat your aliases, use a subquery:
SELECT *, IF (book + tot = 0, 0, book / (book + tot) * 100)
FROM (
SELECT
COUNT(*) as total,
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) book,
SUM(CASE WHEN status!='DNS' THEN 1 ELSE 0 END) tot,
FROM appts
WHERE WEEK(app_date) = WEEK(CURDATE()) GROUP BY booker
) AS subq
ORDER BY hold;
Or, more cleverly :) (book + tot = total)
SELECT *, IF (total = 0, 0, book / total * 100)
FROM (
SELECT
COUNT(*) as total,
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) book,
SUM(CASE WHEN status!='DNS' THEN 1 ELSE 0 END) tot,
FROM appts
WHERE WEEK(app_date) = WEEK(CURDATE()) GROUP BY booker
) AS subq
ORDER BY hold;
And, just for fun, the hackishly compactest form:
SELECT *, COALESCE(book / total * 100, 0) AS hold -- a division by 0 returns NULL
FROM (
SELECT
COUNT(*) total,
SUM(status='DNS') book, -- boolean "true" is internally integer "1"
SUM(status!='DNS') tot,
FROM appts
WHERE WEEK(app_date) = WEEK(CURDATE()) GROUP BY booker
) AS subq
ORDER BY hold;

Mysql addition and add them as new column

I want to fetch 2 coulmns count and do their total as a new column.
How can I do this?
i wrote this query, but this is returning wrong total.
SELECT count(case when `status`='1' then 1 else 0 end) AS HOT,
count(case when `status`='5' then 1 end)
AS Special_Case,count(case when 1=1 then 1 end) AS TOTAL
FROM `tbl_customer_conversation` group by
date(`dt_added`),user_id
COUNT will only give the times a record is matched, which in your query will always return 1. Because the values can either be 1 or 0. So count(1) is also 1 and count(0) is also 1.
AS, you want the total number of HOT cases and SPECIAL_CASE you have to use SUM.
SELECT
SUM(case when `status`='1' then 1 else 0 end) AS HOT,
SUM(case when `status`='5' then 1 end) AS Special_Case,
SUM(case when `status` = '1' or `status` = '5' then 1 end) AS TOTAL
FROM `tbl_customer_conversation`
group by date(`dt_added`),user_id

Categories