Combine two SELECT queries with different function - mysql

I have one table for reports. My first select query is that combines all sales tables
SELECT *, ds_sales.id_sales
FROM ds_sales LEFT JOIN
ds_payment
ON ds_sales.id_sales=ds_payment.id_sales LEFT JOIN
customer_info
ON ds_payment.id_customer=customer_info.id_customer INNER JOIN
ds_salesdetails
ON ds_salesdetails.id_sales=ds_sales.id_sales
WHERE customer_info.id_customer = '".$_POST["id_customer"]."'
This is the result of the query
and my second query is this, to filter the same sales serial number, since the ds_salesdetails table have many sales serial number
select ds_salesdetails.id_sales, count(*),
group_concat(ds_salesdetails.id_product)
from ds_salesdetails
having count(*) >= 1
I need to merge them to create a report for customers sales.

Join with the grouped subquery instead of the whole table.
SELECT *, ds_sales.id_sales
FROM ds_sales LEFT JOIN
ds_payment
ON ds_sales.id_sales=ds_payment.id_sales LEFT JOIN
customer_info
ON ds_payment.id_customer=customer_info.id_customer LEFT JOIN
(SELECT id_sales, count(*) as product_count,
group_concat(ds_salesdetails.id_product) AS product_list
from ds_salesdetails
GROUP BY id_sales) AS grouped ON grouped.id_sales = ds_sales.id_sales
WHERE customer_info.id_customer = '".$_POST["id_customer"]."'

Related

How to get group by work with latest MYSQL version with left join also two aggregate functions

I have two tables like in above image and third one should be output.
I have tried like this but unable to get the result I can get only one of the columns with category not both at the same time.
This is what I have used:
select Budget.Category, sum(Budget.Estimated_Cost) as Estimated_Cost,
sum(Actuals.Actual_Cost) as Actual_Cost
from Budget
LEFT OUTER JOIN Actuals ON Budget.task = Actuals.Task
GROUP BY Budget.Category;
and
select b.Category, sum(b.Estimated_Cost) AS Estimated_Cost
FROM Budget b
LEFT JOIN (SELECT DISTINCT a.Task
FROM Actuals a) x
ON x.Task = b.task
GROUP BY b.Category**
First aggregate in Budget to get the sum of Estimated_Cost for each Category and then join to Actuals and aggregate again to get the sum of Actual_Cost that are linked to each Category:
SELECT b.Category,
b.Estimated_Cost,
SUM(a.Actual_Cost) AS Actual_Cost
FROM (
SELECT Category, SUM(Estimated_Cost) Estimated_Cost,
GROUP_CONCAT(Task) Tasks
FROM Budget
GROUP BY Category
) b LEFT JOIN Actuals a
ON FIND_IN_SET(a.Task, b.Tasks)
GROUP BY b.Category, b.Estimated_Cost

I want both distinct and nondistinct records in join mysql

SELECT distinct referrals.listing_id,submissions.listing_name
FROM submissions
INNER JOIN referrals ON referrals.listing_id=submissions.listing_id;
if i do
SELECT distinct referrals.listing_id,submissions.listing_name,referrals.timestamp
FROM submissions
INNER JOIN referrals ON referrals.listing_id=submissions.listing_id;
it gives me many others records without distinct listing_id
i want distinct listing_id with their timestamps(which are not distinct but are according to distinct listing_id)
Use aggregation, if you want one row per listing. For instance:
SELECT r.listing_id, s.listing_name, max(r.timestamp) as most_recent_timestamp
FROM submissions s INNER JOIN
referrals r
ON r.listing_id = s.listing_id
GROUP BY r.listing_id, s.listing_name;
SELECT DISTINCT applies to all the expressions in the SELECT.

How to avoid Left join table show duplicate row?

I have some problem with the query issue when trying to sum up the quantity.
Table
This cart item table stored id_cart and id product
This order table stored id_cart and other id may be included such as supplier. This table is used to track order record and send notification to supplier.
Wrong result. Expected output = 1, 1, 1
SELECT id, id_product, SUM(qty)
from cart_item
left join Orderp using(id_cart)
group by id_product
http://sqlfiddle.com/#!9/07bf57/1
The issue caused by duplicate id_cart in order table as well. How can i handle this? Any solution to make it works? Thanks.
There is something wrong in your data, or in your data model
INSERT INTO OrderP(`id_order`,`id_cart`)VALUES(1, 1);
INSERT INTO OrderP(`id_order`,`id_cart`)VALUES(2, 1);
There are 2 rows for id_cart = 1, so the "natural join" will double every row when joining cart_item to orderp.
Using an inner join to a different column in orderp works better because now there is only one row in orederp for each cart_item.
SELECT id_product, sum(qty)
from cart_item ci
left join Orderp o on ci.id_cart = o.id_order
GROUP BY id_product
http://sqlfiddle.com/#!9/07bf57/13
Try the following query
SELECT
i.id_product,
p.name productname,
b.id_branch,
b.branchname,
SUM(i.qty)
from cart_item i
left join (SELECT DISTINCT id_cart,id_branch FROM Orderp) o on o.id_cart=i.id_cart
left join product p on i.id_product=p.id_product
left join catalog c on c.id_product=p.id_product and c.id_branch=o.id_branch
left join branch b on b.id_branch=o.id_branch
group by
i.id_product,
p.name,
b.id_branch,
b.branchname
The main problem in Orderp table because it containts two different orders for one cart (DISTINCT id_cart,id_branch helps here). And you need to use the second condition by id_branch for catalog (and c.id_branch=o.id_branch).
SQL Fiddle - http://sqlfiddle.com/#!9/f32d5f/16
And I think you can use everywhere INNER JOIN instead LEFT JOIN
SELECT
i.id_product,
p.name productname,
b.id_branch,
b.branchname,
SUM(i.qty)
from cart_item i
join (SELECT DISTINCT id_cart,id_branch FROM Orderp) o on o.id_cart=i.id_cart
join product p on i.id_product=p.id_product
join catalog c on c.id_product=p.id_product and c.id_branch=o.id_branch
join branch b on b.id_branch=o.id_branch
group by
i.id_product,
p.name,
b.id_branch,
b.branchname

Aggregation MAX COUNT sub query, with joins

I have following query to solve:
"List the Member(s) who are born in and after 1990 and have organised the Hackathons that have received funding from the project(s) that have the highest number of labs co-working on them."
SELECT Member.email, Member.firstName, Member.lastName, Member.dateOfBirth,
Hubs.organiserMember, MAX(LabInProject.projectID)
FROM LabInProject
INNER JOIN Project ON LabInProject.projectID=Project.projectID
INNER JOIN Hackathon ON Project.projectID=Hackathon.fundingProject
INNER JOIN Hubs ON Hackathon.eventID=Hubs.eventID
INNER JOIN Member ON Member.email=Hubs.organiserMember
WHERE LabInProject.projectID = (SELECT MAX(LabInProject.projectID) FROM LabInProject)
GROUP BY Hubs.organiserMember
HAVING Member.dateOfBirth > '1990'
The SELECT MAX gives me the highest projectID (number) in the row, NOT the highest COUNT of projectID.
How do I get the "MAX COUNT" of projectID in table: LabInProject?
I have tried by making a subquery with a derived table: totalCount, but I don't know how to connect this with the joins, it's not working.
HAVING COUNT(*) =
(
SELECT COUNT(projectID) totalCount
FROM LabInProject
GROUP BY projectID
LIMIT 1
)
WHERE LabInProject.projectID = (SELECT MAX(LabInProject.projectID) FROM LabInProject)
You have a Syntax-Error here.
Try to post the closing bracket at the end of the statement.
Consider the below derived table in an inner join with its own derived tables to replace the earlier WHERE condition. This should return multiple projects that share same maximum counts:
...
INNER JOIN
-- OBTAIN PROJECT AND COUNTS CONDITIONED TO THE MAX
(SELECT sub.ProjectID, Count(*) As ProjectIDCount
FROM LabInProject sub
INNER JOIN Project ON LabInProject.projectID=Project.projectID
INNER JOIN Hackathon ON Project.projectID=Hackathon.fundingProject
INNER JOIN Hubs ON Hackathon.eventID=Hubs.eventID
INNER JOIN Member ON Member.email=Hubs.organiserMember
WHERE Member.dateOfBirth > '1990'
GROUP BY sub.ProjectID
HAVING Count(*) IN
-- OBTAIN SCALAR VALUE OF MAX PROJECT COUNT
(SELECT Max(dT.ProjectIDCount) As MaxOfProjectIDCount
FROM
-- OBTAIN PROJECT COUNTS
(SELECT subdT.ProjectID, Count(*) As ProjectIDCount
FROM LabInProject subdT
INNER JOIN Project ON LabInProject.projectID=Project.projectID
INNER JOIN Hackathon ON Project.projectID=Hackathon.fundingProject
INNER JOIN Hubs ON Hackathon.eventID=Hubs.eventID
INNER JOIN Member ON Member.email=Hubs.organiserMember
WHERE Member.dateOfBirth > '1990'
GROUP BY subdT.ProjectID) As dT)
) As temp
ON LabInProject.projectID = temp.projectID
...

when i use sum and count in single query i get incorrect result

for following query:
select sum(j.credits) as credits, count(id.redo) as redos
from operator o inner join jobdetails j on j.opid=o.id
inner join imagedetails id on id.opid=o.id
and im.redo=y group by o.id
You should separate SUM and COUNT from different table as soon if you have 3 records in J and 4 records in ID you get 3*4=12 records after JOIN in the result set so you get wrong sum and count. For example:
select o.id, j.sum_credits as credits, id.count_redo as redos
from operator o
inner join
(select opid, sum(credits) sum_credits from jobdetails group by opid) j
on j.opid=o.id
inner join
(select opid, count(redo) count_redo from imagedetails
where redo=y group by opid) id
on id.opid=o.id