How to subtract the one row from another MySQL - mysql

Given that a table as enclosed
How do I find the difference between Mac and Windows sold each day.
Can someone pls explain the logic

An INNER JOIN would do the job.
SELECT
WindowsTable.Date,
ABS(WindowsTable.Sold - MacTable.Sold) absoluteDifference
FROM
(SELECT
*
FROM producttable
WHERE Products = 'Windows') WindowsTable
INNER JOIN
(
SELECT
*
FROM producttable
WHERE Products = 'Mac' ) MacTable
ON WindowsTable.Date = MacTable.Date;
DEMO HERE

Try reshaping your query using an INNER JOIN on date:
SELECT macs_sales.Date, (MacsSold - WindowsSold) AS sales_difference
FROM
(
SELECT Date, Sold as MacsSold
FROM computer_sales
WHERE Products="Mac"
) macs_sales
INNER JOIN
(
SELECT Date, Sold as WindowsSold
FROM computer_sales
WHERE Products="Windows"
) windows_sales
ON macs_sales.Date = windows_sales.Date

Related

mysq: Using the same subquery in FROM and WHERE clause without repeating it?

I need to select data with all the cars that have been the longest in a workshop. So if the max of days is 16 and there are multiple cars with 16 days I need all of them.
My models are like this
Car: id_car, mat_car, mod_car, color, type
Period: id_per, date_ini, date_fin
Relacion4: id_car, id_per
This method works but is there a way to do it with without repeating the same subquery in FOR and in WHERE clause as well?
select * from (
select Car.mat_car, mod_car, color, type, date_ini, date_fin, datediff(date_fin, date_ini) daysInWorkshop from Car
inner join Relacion4 on Car.mat_car = Relacion4.mat_car
inner join Period on Relacion4.id_per = Periodo.id_per
) as CarWithDurr
where daysInWorkshop = (
select max(daysInWorkshop) from (
select datediff(date_fin, date_ini) daysInWorkshop from Car
inner join Relacion4 on Car.mat_car = Relacion4.mat_car
inner join Period on Relacion4.id_per = Period.id_per
) as CarWithDurr
);
I think you can slim down a bit.
select Car.mat_car, mod_car, color, type,
date_ini, date_fin, datediff(date_fin, date_ini) daysInWorkshop
from Car
inner join Relacion4 on Car.mat_car = Relacion4.mat_car
inner join Period on Relacion4.id_per = Periodo.id_per
where datediff(date_fin, date_ini) =
(
select max(datediff(date_fin, date_ini))
from Period
)
;
If this doesn't work as you wish please add sample data and expected output as text to the question.
btw WHERE DO YOU GET Relacion4.mat_car it isn't in your published table!! AND Periodo is misspelled.

SQL - Divide One Query by Another

I am trying to create a query which returns the workout percentage completion...
Workout Percentage Completion =
((Sum of LogEntries WHERE date = IN list of dates and WorkoutID =1 ) /
(Sum of all set numbers WHERE WorkoutID = 1))
x 100
Below is what I currently have, at the moment it is only returning the result of the first query.
What should I change for the query to run correctly?
SELECT
(
(
SELECT COUNT(LogEntriesID)
FROM LogEntriesTable
LEFT JOIN ExerciseWorkoutJunctionTable
ON ExerciseWorkoutJunctionTable.ExerciseWorkoutJunctionID =
LogEntriesTable.JunctionID
WHERE LogEntriesTable.date IN (
"14-05-2020", "15-05-2020", "16-05-2020", "17-05-2020",
"18-05-2020", "19-05-2020", "20-05-2020"
)
AND ExerciseWorkoutJunctionTable.WorkoutID = 1
) / (
SELECT sum(SetNumber)
FROM ExerciseWorkoutGoalsTable
LEFT JOIN ExerciseWorkoutJunctionTable
ON ExerciseWorkoutJunctionTable.ExerciseWorkoutJunctionID =
ExerciseWorkoutGoalsTable.JunctionID
WHERE ExerciseWorkoutJunctionTable.WorkoutID = 1
)
)
Your first SELECT statement is doing an OUTER JOIN but then you have a WHERE clause that is selecting non-NULL values from the ExerciseWorkoutJunctionTable table, so I suspect you might as well be doing an INNER JOIN.
When you have two queries, try:
SET #sum = (SELECT SUM(SetNumber) etc ....);
SELECT (COUNT(LogEntriesID) * 100 / #sum) AS percentage
FROM etc.
If you are using MySQL >= 8.0 you should be able to use window functions like this which breakdown your query into more readable sections.
with entries as (
SELECT COUNT(LogEntriesID) as log_entry_count
FROM LogEntriesTable as l
LEFT JOIN ExerciseWorkoutJunctionTable as e ON
e.ExerciseWorkoutJunctionID = l.JunctionID
WHERE l.date IN ("14-05-2020","15-05-2020","16-05-2020","17-05-2020","18-05-2020","19-05-2020","20-05-2020")
AND e.WorkoutID = 1
),
sets as (
SELECT sum(SetNumber) as set_sum
FROM ExerciseWorkoutGoalsTable as eg
LEFT JOIN ExerciseWorkoutJunctionTable ej
ON ej.ExerciseWorkoutJunctionID = eg.JunctionID
WHERE ej.WorkoutID = 1
)
select ((select log_entry_count from entries) / (select set_sum from sets)) * 100 as workout_completion_pct

How to join the result of a request to another table (to add column)

Edit
I have two tables : etape and etapex.
I need to find the maximum idProjet for each idEtape (in table etapex), and there link the idEtape and idProjet to 'nomEtapeandlivrable` from the table etape.
So I tried two different things :
SELECT etapexprojet.idEtape, idProjet, nometape, livrable
FROM etape, etapexprojet
WHERE etapexprojet.idetape = (SELECT MAX(etape.idetape) FROM etapexprojet )
Unfortunately, this is what I get :
Here's my other "solution":
The first step is to find the max value (I don't want to use group by) :
SELECT EX.idEtape
FROM etapexprojet EX
LEFT JOIN etapexprojet EX2
ON EX.idprojet = EX2.idprojet
AND EX.idetape < EX2.idetape
WHERE EX2.idetape IS NULL
But now I'm stuck, and I don't get how to join what I get from that first request to the table etape.
At the end, it should give me something like :
But with the columns nomEtape and livrable as well...
Thanks in advance for your help !
You can just JOIN the etape table to the results of your first query:
SELECT EX.idProjet, E.*
FROM etape E
JOIN etapexprojet EX ON EX.idEtape = E.idEtape
LEFT JOIN etapexprojet EX2
ON EX.idprojet = EX2.idprojet
AND EX.idetape < EX2.idetape
WHERE EX2.idetape IS NULL
ORDER BY idProjet
Demo on SQLFiddle
There are various solution to this greatest-n-per-group problem.
One simple method is to start from this aggregate query that gives you the maximum idEtape per IdProjet:
select idProjet, max(idEtape) idEtape from etapex group by idProjet
Then you can simply join this with the original table:
select e.*, p.idProjet
from etape e
inner join (
select idProjet, max(idEtape) idEtape from etapex group by idProjet
) p on p.idEtape = e.idEtape
Try this:
SELECT ET.idEtape, ETX.idProjet, ET.nomEtape, ET.livrable
FROM etape ET
LEFT JOIN etapexprojet ETX ON ET.idEtape = ETX.idEtape
WHERE ETX.idEtape = (SELECT MAX(idEtape) FROM etapexprojet WHERE idProjet = ETX.idProjet);

How to get data by using join in mysql

I have a table:
and this table:
I would like to create report like this
I've tried this SQL:
select
master_problem.problem,
master_problem.sop_reference,
master_problem.adidas_spec,
count(log_roving_qc.id_problem) as jumlahfrom
master_problem
inner join log_roving_qc
on master_problem.id_problem = log_roving_qc.id_problem
group by master_problem.id_problem
but the empty data does not show. I want to display blank data with a description of 0
Do a left join of the master_problem table to a subquery which does the count aggregation:
SELECT
mp.problem,
mp.sop_reference,
mp.adidas_spec,
COALESCE(t.cnt, 0) AS jumlahfrom
FROM master_problem mp
LEFT JOIN
(
SELECT id_problem, COUNT(*) as cnt
FROM log_roving_qc
GROUP BY id_problem
) t
ON mp.id_problem = t.id_problem;

mysql several count and join returns strange value

I'm trying to make a count within several table with JOIN, but when I made several JOINs the COUNTs got wrongly counted.
Basically I've got 4 tables, named:
predective_search
predective_to_product
predective_to_category
predective_to_manufacturer
I want to count the total number of products, categories and manufacturer which has same id in table predective_search.
Here's my code:
SELECT * ,
COUNT(pp.predictive_id) AS total_products,
COUNT(pc.predictive_id) AS total_categories,
COUNT(pm.predictive_id) AS total_manufacturers
FROM predictive_search ps
LEFT JOIN predictive_to_product pp ON (ps.predictive_id = pp.predictive_id)
LEFT JOIN predictive_to_category pu ON (ps.predictive_id = pc.predictive_id)
LEFT JOIN oc_predictive_to_manufacturer pm ON (ps.predictive_id = pm.predictive_id)
GROUP BY ps.predictive_id
Also the GROUP BY is needed I think. I'm stuck at this as I'm not getting any way to do this
SELECT
ps.*,
agg_pp.total_products,
agg_pc.total_categories,
agg_pm.total_manufacturers
FROM predictive_search ps
LEFT JOIN (
SELECT pp.predictive_id, COUNT(*) AS total_products
FROM predictive_to_product pp
GROUP BY pp.predictive_id
) agg_pp ON ps.predictive_id = agg_pp.predictive_id
LEFT JOIN (
SELECT pc.predictive_id, COUNT(*) AS total_categories
FROM predictive_to_category pc
GROUP BY pc.predictive_id
) agg_pc ON ps.predictive_id = agg_pc.predictive_id
LEFT JOIN (
SELECT pm.predictive_id, COUNT(*) AS total_manufacturers
FROM predictive_to_category pm
GROUP BY pm.predictive_id
) agg_pm ON ps.predictive_id = agg_pm.predictive_id