getting multiple sum on multiple rows group by supplier - php

I have a query that fetch the result from database as shown in this screenshot but I want the result like the second screenshot.
The query is:
SELECT ED.modelno,
ED.grade,
ED.type,
ED.class,
ED.thickness,
ED.over_size_type ,
ED.description,
ED.thread_pitch,
ED.thread_series,
ED.head_mark,
ED.qty_per_box,
ED.no_boxes,
ED.length,
ED.qty,
SQ.s_id,
SQ.supplier_code,
SQ.currancy,
SQ.moq,
SQ.weight_per1000,
SQ.us_dollor_per1000,
SQ.final_us_doller,
SQ.discount_ontotal,
SQ.us_dollor_kg,
SQ.ttl_us_dollor,
SQ.ttl_kg,
SQ.evaluation,
SQ.totadiscount_value,
SQ.pallet,
R.name AS nam,
R.companyname
FROM enquiry_details AS ED
LEFT OUTER JOIN supplier_quotation AS SQ ON ED.enq_detail_id=SQ.enq_detail_id
LEFT OUTER JOIN registration AS R ON SQ.s_id=R.id
WHERE ED.enq_id=15
GROUP BY R.name

Can you try this. change your group by to
Group by R.name, ED.modelno
With Rollup.
Thanks.
Here is my test result.
Hi here is the query.
I just assume that you need to total the totadiscount_value.
SELECT coalesce(ED.modelno,'Sub Total') modelno,
if (coalesce(ED.modelno,'Total')='Total',null,ED.grade) grade,
if (coalesce(ED.modelno,'Total')='Total',null,ED.type) type,
if (coalesce(ED.modelno,'Total')='Total',null,ED.class) class,
if (coalesce(ED.modelno,'Total')='Total',null,ED.thickness) thickness,
if (coalesce(ED.modelno,'Total')='Total',null,ED.over_size_type) over_size_type,
if (coalesce(ED.modelno,'Total')='Total',null,ED.description) description,
if (coalesce(ED.modelno,'Total')='Total',null,ED.thread_pitch) thread_pitch,
if (coalesce(ED.modelno,'Total')='Total',null,ED.thread_series) thread_series,
if (coalesce(ED.modelno,'Total')='Total',null,ED.head_mark) head_mark,
if (coalesce(ED.modelno,'Total')='Total',null,ED.qty_per_box) qty_per_box,
if (coalesce(ED.modelno,'Total')='Total',null,ED.no_boxes) no_boxes,
if (coalesce(ED.modelno,'Total')='Total',null,ED.length) length,
if (coalesce(ED.modelno,'Total')='Total',null,ED.qty) qty,
if (coalesce(ED.modelno,'Total')='Total',null,SQ.s_id) s_id,
if (coalesce(ED.modelno,'Total')='Total',null,SQ.supplier_code) supplier_code,
if (coalesce(ED.modelno,'Total')='Total',null,SQ.currancy) currancy,
if (coalesce(ED.modelno,'Total')='Total',null,SQ.moq) moq,
if (coalesce(ED.modelno,'Total')='Total',null,SQ.weight_per1000) weight_per1000,
if (coalesce(ED.modelno,'Total')='Total',null,SQ.us_dollor_per1000) us_dollor_per1000,
if (coalesce(ED.modelno,'Total')='Total',null,SQ.final_us_doller) final_us_doller,
if (coalesce(ED.modelno,'Total')='Total',null,SQ.discount_ontotal) discount_ontotal,
if (coalesce(ED.modelno,'Total')='Total',null,SQ.us_dollor_kg) us_dollor_kg,
if (coalesce(ED.modelno,'Total')='Total',null,SQ.ttl_us_dollor) ttl_us_dollor,
if (coalesce(ED.modelno,'Total')='Total',null,SQ.ttl_kg) ttl_kg,
if (coalesce(ED.modelno,'Total')='Total',null,SQ.evaluation) evaluation,
sum(SQ.totadiscount_value) totadiscount_value,
if (coalesce(ED.modelno,'Total')='Total',null,SQ.pallet) pallet,
coalesce(R.name,'Total') as nam,
if (coalesce(ED.modelno,'Total')='Total',null,R.companyname) companyname
FROM enquiry_details AS ED
LEFT OUTER JOIN supplier_quotation AS SQ ON ED.enq_detail_id=SQ.enq_detail_id
LEFT OUTER JOIN registration AS R ON SQ.s_id=R.id
WHERE ED.enq_id=15
group by R.name,ED.modelno
with rollup
Thanks.

Related

Getting data from a table even if there's no data in table 2 that links to table 1

I'm creating a planner but the SQL statement I have now only shows employees that have something in table 2 (plannerEntries) and doesn't show the rest of the employees from table 1 (Employee_List).
I need all of the employees to be outputted into the table regardless of whether they have any jobs assigned for them for the week, so that they can have new jobs assigned easily.
This is my current SQL code
SELECT [EL].[Employee_Numer],
[PP].[workDate],
[PP].[jobNo],
[PP].[workDescription],
[PP].[timeOfDay],
[JF].[Description],
[EL].[Forename],
[EL].[Surname]
FROM plannerEntries AS PP
RIGHT JOIN [Employee_List] AS EL
ON [PP].[employeeNumber] = [EL].[Employee_Numer]
INNER JOIN [Job File] AS JF
ON [PP].[jobNo] = [JF].[Job No]
WHERE [PP].[workDate] >= '$monday'
AND [PP].[workDate] <= '$sunday'
ORDER BY [PP].[employeeNumber] ASC;
I expected all employees to be printed regardless of records in table 2, however only the employees with records in table 2 were printed. The below image is the actual output.
Please check the difference between inner join, left join, right join.
Something like this should do what you need:
SELECT
[EL].[Employee_Numer],
[PP].[workDate],
[PP].[jobNo],
[PP].[workDescription],
[PP].[timeOfDay],
[JF].[Description],
[EL].[Forename],
[EL].[Surname]
FROM
[Employee_List] AS EL
left join plannerEntries AS PP on [PP].[employeeNumber] = [EL].[Employee_Numer]
and [PP].[workDate] >= '$monday'
and [PP].[workDate] <= '$sunday'
left join [Job File] AS JF on [JF].[Job No] = [PP].[jobNo]
ORDER BY
[PP].[employeeNumber] ASC;

Sum mysql only works for the first row or just get wrong

I got this:
SELECT `cuenta`.`reservaId`, sum(`cuenta`.`tarifaTotal`)
FROM `check`,`cuenta`,`comanda`,`reserva`
WHERE `comanda`.`id`=`cuenta`.`idComanda` AND `comanda`.`tipo`=0
GROUP BY `cuenta`.`reservaId` ORDER BY `check`.`id`
the problem is that I get something like 10000000 as result of the first "reservaID" over the 119 I should get.
When I use this code, it works, but Only for the first "reservaID" space:
SELECT `cuenta`.`reservaId`, sum(`cuenta`.`tarifaTotal`)
FROM `check`,`reserva`,`cuenta`,`comanda`
WHERE `cuenta`.`reservaId`=`reserva`.`id` AND `cuenta`.`idComanda`=`comanda`.`id` AND `check`.`idReserva`=`reserva`.`id` AND `comanda`.`tipo`=0
GROUP BY `cuenta`.`reservaId`
ORDER BY `check`.`id`
tables:
check: id, reservaID
reserva: id, idPersona, idEmpresa, etc.
cuenta: reservaID, idComanda, Tarifatotal(this field is like mini bills, so "tarifatotal" is the price for each mini-bill)
comanda: id, name, tipo(or type, who can be 0,1,2 or 3) to describe the service type, like drinks, restaurant, services, others
Please help, I want to show all "sum"s for every single "reservaID".
Try this:
SELECT cuenta.reservaId, sum(cuenta.tarifaTotal) FROM check
JOIN reserva
ON reserva.id = check.idReserva
JOIN cuenta
ON cuenta.reservaId = reserva.id
JOIN comanda
ON comanda.id = cuenta.idComanda
WHERE comanda.tipo = 0
GROUP BY cuenta.reservaId
ORDER BY check.id;
Possibly a left join would solve your problems:
SELECT r.id, sum(c.tarifaTotal)
FROM reserva r LEFT JOIN
cuenta c
ON c.reservaId = r.id LEFT JOIN
commanda co
ON co.id = c.idCommanda
WHERE co.tipo = 0
GROUP BY r.id;
You don't seem to need the check table. Your ORDER BY doesn't make sense, because check.id is not in the SELECT list or GROUP BY list.

Filtering and restricting in query

I would like to seek some help in my query...i want to do is if specific atic and oaic is empty in the table...the interview_sum or other_sum to that specific atic oaic should be empty too....can anyone know how to do that?
picture of current output:
current query: my query still gives numbers to other_sum or interview_sum even its empty.
SELECT DISTINCT
IF(t.inttotal=NULL,0,(SELECT SUM(t2.inttotal)
FROM app_interview2 AS t2
WHERE t2.atic = t.atic AND t2.inttotal>0)/7)
AS interview_sum,
IF(o.ototal=NULL,0,(SELECT SUM(o2.ototal)
FROM other_app2 AS o2
WHERE o2.oaic = o.oaic AND o2.ototal>0)/7)
AS other_sum,
atid,
atic,
atname,
region,
town,
uniq_id,
position,
salary_grade,
salary
FROM app_interview2 AS t, other_app2 AS o
GROUP BY t.atname HAVING COUNT(DISTINCT t.atic)
I made a few assumptions:
You probably have a table that app_interview2.atic and other_app2.oaic are the foreign keys of, but since you did not share it, I derived a table in the FROM clause.
This assumes atname is always the same for atid.
You are also dividing by 7 - which I assume is to get the average, so I used the AVG function.
Solution---
SELECT t1.id AS atid
,interview.atname AS atname
,COALESCE(interview.interviewsum, 0) AS interviewsum
,COALESCE(interview.interviewavg,0) AS interviewavg
,COALESCE(other.othersum, 0) AS othersum
,COALESCE(other.otheravg) AS otheravg
FROM (SELECT DISTINCT atid AS id
FROM app_interview2
UNION
SELECT DISTINCT oaic
FROM other_app2) AS t1
LEFT JOIN (SELECT atid, atname, SUM(inttotal) AS interviewsum, AVG(inttotal) AS interviewavg
FROM app_interview2
GROUP BY atid, atname) as interview
ON interview.atid = t1.id
LEFT JOIN (SELECT oaic, SUM(ototal) AS othersum, AVG(ototal) AS otheravg
FROM other_app2
GROUP BY oaic) AS other
ON other.oaic = t1.id;
--
If this gives the results your were hoping for, I would replace the t1 derived table in the FROM clause with the table whose primary key I described above AND probably has those columns (e.g., region, town, etc) that I did not include

MySQL Join and create new column value

I have an instrument list and teachers instrument list.
I would like to get a full instrument list with id and name.
Then check the teachers_instrument table for their instruments and if a specific teacher has the instrument add NULL or 1 value in a new column.
I can then take this to loop over some instrument checkboxes in Codeigniter, it just seems to make more sense to pull the data as I need it from the DB but am struggling to write the query.
teaching_instrument_list
- id
- instrument_name
teachers_instruments
- id
- teacher_id
- teacher_instrument_id
SELECT
a.instrument,
a.id
FROM
teaching_instrument_list a
LEFT JOIN
(
SELECT teachers_instruments.teacher_instrument_id
FROM teachers_instruments
WHERE teacher_id = 170
) b ON a.id = b.teacher_instrument_id
my query would look like this:
instrument name id value
--------------- -- -----
woodwinds 1 if the teacher has this instrument, set 1
brass 2 0
strings 3 1
One possible approach:
SELECT i.instrument_name, COUNT(ti.teacher_id) AS used_by
FROM teaching_instrument_list AS i
LEFT JOIN teachers_instruments AS ti
ON ti.teacher_instrument_id = i.id
GROUP BY ti.teacher_instrument_id
ORDER BY i.id;
Here's SQL Fiddle (tables' naming is a bit different).
Explanation: with LEFT JOIN on instrument_id we'll get as many teacher_id values for each instrument as teachers using it are - or just a single NULL value, if none uses it. The next step is to use GROUP BY and COUNT() to, well, group the result set by instruments and count their users (excluding NULL-valued rows).
If what you want is to show all the instruments and some flag showing whether or now a teacher uses it, you need another LEFT JOIN:
SELECT i.instrument_name, NOT ISNULL(teacher_id) AS in_use
FROM teaching_instrument_list AS i
LEFT JOIN teachers_instruments AS ti
ON ti.teacher_instrument_id = i.id
AND ti.teacher_id = :teacher_id;
Demo.
Well this can be achieved like this
SELECT
id,
instrument_name,
if(ti.teacher_instrument_id IS NULL,0,1) as `Value`
from teaching_instrument_list as til
LEFT JOIN teachers_instruments as ti
on ti.teacher_instrument_id = til.id
Add a column and check for teacher_instrument_id. If found set Value to 1 else 0.

MySQL returns some rows 3 times

I have this query:
SELECT w.windate, w.wintime, w.field, w.htname, w.vtname, w.plateumpire,
w.fieldumpire1, w.fieldumpire2, w.fieldumpire3, p.pteamname, p.teamcoach,
p.pteamdivision, d.originalid
FROM sportsdb_wins AS w, playerteams AS p, sportsdb_divs AS d
WHERE p.teamcoach='$coachid'
AND p.pteamname IN (w.htname, w.vtname)
AND p.forteam='$teamid'
and at least one of the rows is returned 3 times! Why? Please help I am a SQL novice.
Using this in PHP...
Use JOIN's when you have to join the tables. Your current problem is a so called cartesian product.
You have no join condition defined for the sportsdb_divs table, so you're getting a cross product of those results. This would be more obvious if you used explicit joins (which you should be doing in all of your SQL code).
SELECT w.windate, w.wintime, w.field, w.htname, w.vtname, w.plateumpire,
w.fieldumpire1, w.fieldumpire2, w.fieldumpire3, p.pteamname, p.teamcoach,
p.pteamdivision, d.originalid
FROM playerteams AS p
INNER JOIN sportsdb_wins AS w
ON p.teamname in (w.htname, w.vtname)
INNER JOIN sportsdb_divs AS d
ON ?????
WHERE p.teamcoach='$coachid'
AND p.forteam='$teamid'

Categories