Sql 2 inner joins other data outcome - mysql

Can someone explain the following.
SELECT results.`referentie`,products.`categorie`,products.`url`,products.`inkoopPrijs`,SUM(results.`stockverschil`) AS 'stockverschil'
FROM `results` results
INNER JOIN products
ON products.`referentie` = results.`referentie`
WHERE results.`datum` BETWEEN '2017-01-11' AND '2017-01-12' AND results.`referentie`= '1'
The result is a stockverschil outcome of 200. (the right amount)
SELECT results.`referentie`,products.`categorie`,products.`url`,products.`inkoopPrijs`,sold.`winstPerProductPerDag`,SUM(results.`stockverschil`) AS 'stockverschil'
FROM `results` results
INNER JOIN products
ON products.`referentie` = results.`referentie`
INNER JOIN sold
ON sold.`referentie` = results.`referentie`
WHERE results.`datum` BETWEEN '2017-01-11' AND '2017-01-12' AND results.`referentie`= '1'
The result is a stockverschil outcome of 4800.
Am i doing something wrong with the INNER JOIN?

Seems like an issue on sold, being a one[product] to many[sold] relationship
Try this, with the appropriate aggregate function forSUM(sold.winstPerProductPerDag)
SELECT results.`referentie`,products.`categorie`,products.`url`,products.`inkoopPrijs`,s.`winstPerProductPerDag`,SUM(results.`stockverschil`) AS 'stockverschil'
FROM `results` results
INNER JOIN products
ON products.`referentie` = results.`referentie`
INNER JOIN ( select SUM(sold.`winstPerProductPerDag`) as `winstPerProductPerDag`, sold.`referentie` from sold group by sold.`referentie`) s on s.`referentie` = results.`referentie`
WHERE results.`datum` BETWEEN '2017-01-11' AND '2017-01-12' AND results.`referentie`= '1'

Related

SQL Join gives wrong results (creates duplicates)

I have a problem with my SQL join query. I have looked up other suggested answers and tried to apply it to my query, but it doesn't seem to be working.
I have this query:
SELECT SUM(p.quantity)
FROM stocktake_scans p
LEFT JOIN (
SELECT stocktake_area_id
FROM stocktake_areas
WHERE stocktake_id =8592 AND area_checked = 1
)d ON d.stocktake_area_id = p.stocktake_area_id
LEFT JOIN (
SELECT user_id
FROM stocktake_scan_edit
WHERE user_id =46521
)e ON e.user_id = p.stocktake_staff_id
WHERE p.stocktake_staff_id = 46521
And it gives me a result of 42, while I should get only 6. What is missing from the query?
I think you may have extra records with the same ID in your joined table that is where you are getting multiple rows returned from which is then calculating wrong in your sum, please try the below.
SELECT SUM(p.quantity) FROM stocktake_scans p LEFT JOIN ( SELECT distinct stocktake_area_id FROM stocktake_areas WHERE stocktake_id =8592 AND area_checked = 1 )d ON d.stocktake_area_id = p.stocktake_area_id LEFT JOIN ( SELECT distinct user_id FROM stocktake_scan_edit WHERE user_id =46521 )e ON e.user_id = p.stocktake_staff_id WHERE p.stocktake_staff_id = 46521

mysql query doesnot work

This is my mysql query!
SELECT projects.projects_id,
projects.projects_title,
projects.projects_cost
FROM projects
LEFT JOIN invoice
ON invoice.projects_id = projects.projects_id
LEFT JOIN project_assign
ON project_assign.projects_id=projects.projects_id
WHERE project_assign.assigned_user_id=3
AND (SUM( invoice.invoice_amount) < projects.projects_cost
OR invoice.projects_id is null )
AND project_assign.project_completed_date IS NOT NULL
In this query i want select all row that:
Is not present in other table e.g. (in my case other table is
"invoice")
Or if persent then this condition must hold sum(invoice.invoice_amount) < projects.projects_cost
Thanks.
Divide your problem. Use a UNION. First use one query to select all records that are not present in the first table (Use a LEFT JOIN), union that with the result of your second query which would give you all records meeting your second condition (use an outer join)
//Select all records present in left table
//union
//select all records present in both tables matching your condition.
SELECT projects.projects_id,
projects.projects_title,
projects.projects_cost
FROM projects
LEFT JOIN invoice
ON invoice.projects_id = projects.projects_id
LEFT JOIN project_assign
ON project_assign.projects_id=projects.projects_id
WHERE project_assign.assigned_user_id=3
AND project_assign.project_completed_date IS NOT NULL
UNION
SELECT projects.projects_id,
projects.projects_title,
projects.projects_cost
FROM projects
INNER JOIN invoice
ON invoice.projects_id = projects.projects_id
INNER JOIN project_assign
ON project_assign.projects_id=projects.projects_id
WHERE project_assign.assigned_user_id=3
AND (SUM( invoice.invoice_amount) < projects.projects_cost
AND project_assign.project_completed_date IS NOT NULL
select projects.projects_id, projects.projects_title,projects.projects_cost
from projects
left join invoice
on invoice.projects_id = projects.projects_id
left join project_assign
on project_assign.projects_id=projects.projects_id
where project_assign.assigned_user_id=3 and
((select sum(invoice.invoice_amount) from invoice) < projects.projects_cost or invoice.projects_id is null )
and project_assign.project_completed_date is not null
You cannot put aggregation functions in the where clause. In this case, you can do the aggregation using a subquery and then do the comparison:
SELECT p.projects_id, p.projects_title, p.projects_cost
FROM projects p LEFT JOIN
(select i.projects_id, sum(invoice_amount) as invoice_amount
from invoice i
) i
ON i.projects_id = p.projects_id LEFT JOIN
project_assign pa
ON pa.projects_id = p.projects_id
WHERE pa.assigned_user_id = 3 AND
(i.invoice_amount < p.projects_cost OR i.projects_id is null ) AND
pa.project_completed_date IS NOT NULL;

use Aggregate function with multiple table in Access

I have Four tables Items,Customer,Invoice_summary,Invoice_details. Here I want to join these four tables and to get Sum(Invoice_details.Item_quntity) and Sum(Invoice_details.Price) for a specific item_code and for a specific date range . Main Columns are as follows:
Invoice_summary :Inv_num,Inv_date,Cus_id,Total
Items :Item_code,Item_name,Unit_price
Invoice_details :Inv_num,Item_code,Item_qty,Price
Customers :Cus_id,Cus_name,Route
Here is what I currently have.this return more than a row(whole itemsnames) i need only for a specific item code.Could someone explain where I am going wrong.
SELECT Invoice_Table.Item_Code, Items.Item_Name,
(Select sum(Invoice_Table.Item_Quntity) from (Invoice_Table INNER JOIN Invoice ON Invoice_Table.Inv_Num = Invoice.Inv_Num) where ((Invoice_Table.Item_Code=[?]) And Invoice.inv_date Between #3/4/2013# And #6/4/2013#) group BY Invoice_Table.Item_Code) AS Quntity,
(Select sum(Invoice_Table.Price) from (Invoice_Table INNER JOIN Invoice ON Invoice_Table.Inv_Num = Invoice.Inv_Num) where ((Invoice_Table.Item_Code=[?]) And Invoice.inv_date Between #3/4/2013# And #6/4/2013#) group BY Invoice_Table.Item_Code) AS Price
FROM Invoice_Table
INNER JOIN Items ON Invoice_Table.Item_Code = Items.Item_Code
GROUP BY Invoice_Table.Item_Code, Items.Item_Name;
The sums you're interested in should be given by this query.
select Inv_num, Item_code,
sum(Item_qty) item_code_qty,
sum(Price) item_code_price
from invoice_details
group by Inv_num, Item_code
It looks like the date range should be found in the invoice summary table. It looks like Invoice_summary and Invoice_details should be joinable on Inv_num.
select inv_s.Inv_num, inv_s.Inv_date, inv_s.Cus_id, inv_s.Total,
inv_t.item_code_qty, inv_t.item_code_price
from Invoice_summary inv_s
inner join (select Inv_num, Item_code,
sum(Item_qty) item_code_qty,
sum(Price) item_code_price
from invoice_details
group by Inv_num, Item_code
) inv_t
on inv_s.Inv_num = inv_t.Inv_num
where inv_s.Inv_date between ? and ?;
And to round that out, join the other two tables on their keys, and add some of their columns to the SELECT clause.
select inv_s.Inv_num, inv_s.Inv_date, inv_s.Cus_id, cus.Cus_name, inv_s.Total,
inv_t.item_code_qty, inv_t.item_code_price,
items.name
from Invoice_summary inv_s
inner join (select Inv_num, Item_code,
sum(Item_qty) item_code_qty,
sum(Price) item_code_price
from invoice_details
group by Inv_num, Item_code
) inv_t
on inv_s.Inv_num = inv_t.Inv_num
inner join items on items.Item_code = inv_t.Item_code
inner join Customers cus on cus.Cus_id = inv_s.Cus_id
where inv_s.Inv_date between ? and ?;
Try this..
Select ID.Item_qty,ID.Price,I.Item_code from Invoice_summary IS
INNER JOIN Invoice_details ID ON ID.Inv_num=IS.Inv_num
INNER JOIN Customers C ON C.Cus_id=IS.Inv_num
INNER JOIN Items I ON I.Item_code=ID.Item_code

Hanging mysql query

I'm having an issue with the following query
select
ord.order_id,
ordProduct.product_id,
coupProd.product_id,
coup.date_end as DateEnd, coup.coupon_id
from `order` ord
inner join order_product ordProduct on ord.order_id = ordProduct.order_id
inner join coupon_product coupProd on ordProduct.product_id = coupProd.product_id
inner join coupon coup on coupProd.coupon_id = coup.coupon_id
where (coup.date_end > curdate());
If I remvove the where clause, the query executes fine, otherwise it just hangs. Any ideas?
It's not a solution per se, but as a workaround, you could maybe get it done as a nested query. i.e. ,
SELECT * FROM (
SELECT
ord.order_id,
ordProduct.product_id,
coupProd.product_id,
coup.date_end AS DateEnd, coup.coupon_id
FROM `order` ord
INNER JOIN order_product ordProduct ON ord.order_id = ordProduct.order_id
INNER JOIN coupon_product coupProd ON ordProduct.product_id = coupProd.product_id
INNER JOIN coupon coup ON coupProd.coupon_id = coup.coupon_id)
WHERE (DateEnd > CURDATE());

SQL query wrong result

i have this query:
SELECT `completed`.`ID` AS `ID`,`completed`.`level` AS `level`,`completed`.`completed_in` AS `completed_in`, COUNT(1) AS `right_answers_num`
FROM `completed`
INNER JOIN `history` ON `history`.`ID` = `completed`.`ID`
INNER JOIN `questions` ON `questions`.`ID` = `history`.`question`
WHERE `completed`.`student_id` = '1' AND `questions`.`answer` = `history`.`answer`
GROUP BY `completed`.`ID`
ORDER BY `completed`.`completed_in` DESC
what i need is to get info of each test in completed table (id,level,completed_in,right_answer_num)
the problem with that query is that if there is no one right answer(history.answer = questions.answer) then it doesn't return the row, while it should return the row(id,level,completed_in) and the right_answer_num(counter) should be zero..
please help me,, thanks ahead.
SELECT
completed.ID AS ID,
completed.level AS level,
completed.completed_in AS completed_in,
COUNT(questions.answer) AS right_answers_num
FROM completed
INNER JOIN history ON history.ID = completed.ID
LEFT JOIN questions ON questions.ID = history.question AND questions.answer = history.answer
WHERE
completed.student_id = '1'
GROUP BY
completed.ID
ORDER BY completed.completed_in DESC
use a LEFT OUTER JOIN intead of an INNER JOIN.
The second inner join is what's causing rows with no record in the questions table to be omitted. An inner join will only return rows that have data in all corresponding tables. Change the second inner join to a left join like so:
SELECT
completed.ID AS ID,
completed.level AS level,
completed.completed_in AS completed_in,
COUNT(questions.answer) AS right_answers_num
FROM completed
INNER JOIN history ON history.ID = completed.ID
LEFT JOIN questions ON questions.ID = history.question
WHERE completed.student_id = 1
GROUP BY completed.ID
ORDER BY completed.completed_in DESC