How can I select the second minimal value within a inner join? - mysql

I have this query
select c.id, c.name, c.email, c.totalpets, min(p.date_created) as first_order,
min(p.weight) as min_weight_bought,
max(p.weight) as max_weight_bought,
count(p.ordernumber) as total_orders
from orders p
inner join customers c
on p.customer_id = c.id
where p.approved = 1
and c.totalpets >= 1
group by c.id
having total_orders > 1
Note that first_order gives me the first result of the row, right? I am trying to get customer first order and customer second order. How can i do that within this inner join?
Thanks

SELECT c1.id,
c1.name,
c1.email,
c1.totalpets,
p1.date_created
FROM orders p1
INNER JOIN customers c1
ON p1.customer_id = c1.id
WHERE
(
SELECT COUNT(*)
FROM orders p2
INNER JOIN customers c2
ON p2.customer_id = c2.id
WHERE c2.id = c1.id AND p2.date_created <= p1.date_created
) <= 2
ORDER BY c1.id;
Here is a running demo which shows a simplified version of the above query (and simplified data set) in action:
SQLFiddle

Related

MY SQL : please confirm if my query is right else provide right query

i using this query but test case not passed
select ct.name,cm.name,sl.quantity
from sales as sl
inner join country as ct on sl.country_id=ct.id
inner join car_model as cm on cm.id=sl.model_id
where sl.sales_date BETWEEN '01-01-2020' AND '12-31-2020'
order by ct.quantity asc;
select
ct.name,
cm.name,
sl.quantity
from
sales as sl
inner join country as ct
on sl.country_id = ct.id
inner join car_model as cm
on sl.model_id = cm.id
where
sl.sales_date BETWEEN '2020-01-01' AND '2020-12-31'
order by
sl.quantity asc;

How do i sub query two joins?

I have created two queries that both return the required results independent of each other. I am trying to join them to have the returned values be customerName, Amount Ordered, and Amount Paid.
Currently, this query works but only returns the customerName. How can it get the query to return the other two columns?
SELECT c1.customerName
FROM
(SELECT cc.customerName, ROUND(SUM(od.priceEach * od.quantityOrdered), 2) as '$ Amount Ordered'
FROM customers cc
INNER JOIN orders o ON o.customerNumber = cc.customerNumber
INNER JOIN orderdetails od ON od.orderNumber = o.orderNumber
GROUP BY cc.customerName
) c1
INNER JOIN
(SELECT c.customerName, ROUND(SUM(p.amount), 2) as 'Total $ Amount Paid'
FROM customers c
INNER JOIN payments p ON p.customerNumber = c.customerNumber
GROUP BY c.customerName
) c2
WHERE c1.customerName = c2.customerName
GROUP BY c1.customerName
ORDER BY c1.customerName;
this should select the others column
SELECT c1.customerName, c1.Amount_Ordered as '$ Amount Ordered', c2.Total_Amount_Paid as 'Total $ Amount Paid'
FROM
(SELECT cc.customerName, ROUND(SUM(od.priceEach * od.quantityOrdered), 2) as Amount_Ordered
FROM customers cc
INNER JOIN orders o ON o.customerNumber = cc.customerNumber
INNER JOIN orderdetails od ON od.orderNumber = o.orderNumber
GROUP BY cc.customerName
) c1
INNER JOIN
(SELECT c.customerName, ROUND(SUM(p.amount), 2) as Total_Amount_Paid
FROM customers c
INNER JOIN payments p ON p.customerNumber = c.customerNumber
GROUP BY c.customerName
) c2
WHERE c1.customerName = c2.customerName
GROUP BY c1.customerName
ORDER BY c1.customerName;
simply add them to the select section:
SELECT c1.customerName, C1.amountOrdered, C2.amountPaid FROM ...
And one more word of advice - DONT use whitespace or special signs like $ in your column names, it is bad practice. I think it's a mistake that mySql even allows it

MySQL Query extremely slow

The following query takes too much time (0.8169sec at the moment) to show 20 entries and i can't find the reason why...
SELECT
`Order`.ID,
Order_Type.Description as OrderTypeDescription,
`Order`.OrderType as OrderType,
DATE_FORMAT(`Order`.InsertDate, '%d.%m.%Y') as OrderInsertDate,
round(sum(Item_Price.Amount),2) as OrderAmount,
c1.ID as buyerCustomerId,
c2.ID as sellerCustomerId,
c1.CompanyName as BuyerCompany,
c2.CompanyName as SellerCompany,
c1.ID as BuyerCustomer,
c2.ID as SellerCustomer
FROM `Order`
INNER JOIN Order_Type ON Order_Type.ID=`Order`.OrderType
INNER JOIN Customer as c1 ON c1.ID=`Order`.BuyerCustomer
INNER JOIN Customer as c2 ON c2.ID=`Order`.SellerCustomer
INNER JOIN Item ON Item.`Order`=`Order`.ID
INNER JOIN Item_Price ON Item_Price.Item=Item.ID
GROUP BY `Order`.ID,OrderType,OrderTypeDescription,buyerCustomerId,sellerCustomerId,BuyerCustomer,SellerCustomer
ORDER BY `Order`.ID DESC
LIMIT 20
EXPLAIN shows the following output: http://pastebin.com/5f9QYizq
I am not really good in optimizing queries, but i think the reason for the bad performance could be the join (and the sum) on the item and the item_price table, because there are a a lot of rows in both tables (item: 16974, item_price: 23981) as each item has one or more item_prices which sum up to order amount.
Any ideas how to make this query faster?
You might try using correlated subqueries instead of group by:
SELECT o.ID, ot.Description as OrderTypeDescription, ot.OrderType as OrderType,
DATE_FORMAT(o.InsertDate, '%d.%m.%Y') as OrderInsertDate,
(SELECT round(sum(ip.Amount), 2)
FROM Item i INNER JOIN
Item_Price ip
ON ip.Item = i.ID
WHERE i.`Order` = o.ID
) as OrderAmount,
c1.ID as buyerCustomerId,
c2.ID as sellerCustomerId,
c1.CompanyName as BuyerCompany,
c2.CompanyName as SellerCompany,
c1.ID as BuyerCustomer,
c2.ID as SellerCustomer
FROM `Order` o INNER JOIN
Order_Type ot
ON ot.ID = o.OrderType INNER JOIN
Customer c1
ON c1.ID = o.BuyerCustomer INNER JOIN
Customer c2
ON c2.ID = `Order`.SellerCustomer
ORDER BY o.ID DESC
LIMIT 20;
This should save on the GROUP BY overhead.
You could even move the LIMIT to a subquery, assuming that the joins are not filtering any rows:
SELECT o.ID, ot.Description as OrderTypeDescription, ot.OrderType as OrderType,
DATE_FORMAT(o.InsertDate, '%d.%m.%Y') as OrderInsertDate,
(SELECT round(sum(ip.Amount), 2)
FROM Item i INNER JOIN
Item_Price ip
ON ip.Item = i.ID
WHERE i.`Order` = o.ID
) as OrderAmount,
c1.ID as buyerCustomerId,
c2.ID as sellerCustomerId,
c1.CompanyName as BuyerCompany,
c2.CompanyName as SellerCompany,
c1.ID as BuyerCustomer,
c2.ID as SellerCustomer
FROM (SELECT o.*
FROM `Order` o
ORDER BY o.id DESC
) o INNER JOIN
Order_Type ot
ON ot.ID = o.OrderType INNER JOIN
Customer c1
ON c1.ID = o.BuyerCustomer INNER JOIN
Customer c2
ON c2.ID = `Order`.SellerCustomer
ORDER BY o.ID DESC
LIMIT 20;

inner join 4 tables with group, order by, having clause

I have 4 table and i want to extract: id, nume, localitate, masina_id, nr_inmatriculare, an_fabricatie, rafinarie, marca, and sum (quantity+deliver_quantity) as total_quantity group by an_fabricatie , Order by marca, and put some having clouse.
I don’t know how to make this.
My query is as bellow , but I think isn't correct.
select c.id, c.nume,c.localitate,l.masina_id, i.nr_inmatriculare, i.an_fabricatie,
i.rafinarie, m.marca from clienti c inner join livrari l on c.id = l.id inner join incarcari I on l.incarcare_id = l.livrari_id inner join masina m on i.id_marca = m.id, sum(select quantity, deliver_quantity) as total_quantity group by an_fabricatie having quantity >1000 order by marca;
Incarcari table
Id|livrari_id|id_marca|nr_inmatriculare|an_fabricatie|rafinarie|aviz_incarcare|quantity|
Livrari table
Id|masina_id|client_id|incarcare_id|deliver_quantity|aviz_livrare
Masini table
Id|numar_inmatriculare|marca|an_fabricatie|
Clienti table
Id|nume|localitate|date_add|date_upd|
SELECT c.id, c.nume, c.localitate, l.masina_id, i.nr_inmatriculare, i.an_fabricatie, i.rafinarie, m.marca, (SUM(i.quantity) + SUM(l.deliver_quantity)) AS total_quantity
FROM clienti c
INNER JOIN livrari l ON c.id = l.id
INNER JOIN incarcari i ON l.incarcare_id = i.livrari_id
INNER JOIN masini m ON i.id_marca = m.id
GROUP BY i.an_fabricatie, c.id, c.nume,c.localitate,l.masina_id, i.nr_inmatriculare, i.rafinarie, m.marca
HAVING i.quantity > 1000
ORDER BY m.marca DESC;

Issue to get total amount while using inner join in MYSQL

I get an wrong TotalPrice from the ItemDetails table while join the taxdetails table.
Code 1:
select Distinct B.ID as BillId,
I.ID as ItemDetailId,
I.TotalPrice as ItemPrice,
T.Amount as TaxAmount from Bill B
inner join ItemDetails I on I.BillNoID = B.ID
inner join TaxDetails T on T.ItemDetailId = I.Id;
Refer below image,
While sum the TotalPrice and TotalTaxAmount on the same query i got an wrong TotalPrice result,
Code 2:
select Distinct B.ID as BillId,
SUM(I.TotalPrice) as TotalPrice,
SUM(T.Amount) as TotalTaxAmount from Bill B
inner join ItemDetails I on I.BillNoID = B.ID
inner join TaxDetails T on T.ItemDetailId = I.Id
group by B.ID;
Please refer below image,
Actual issue is as per taxdetails per itemdetail each row the rows become multiple so the totalprice get differs.
Expected Output for code 2 (i have listed the first row expected result) is,
Billid TotalPrice TotalTaxAmount
1 70 30
Try this,you have multiple items/bill, that is why you can't make inner join directly.Remove group by also.
select B.ID as BillId,
I.TotalPrice as TotalPrice,
T.Amount as TotalTaxAmount from Bill B
inner join
(
select BillNoID,SUM(TotalPrice) as TotalPrice from ItemDetails
group by BillNoID
)as I on I.BillNoID = B.ID
inner join
(
Select ItemDetailId,SUM(Amount) as Amount from TaxDetails
group by ItemDetailId
)as T on T.ItemDetailId = I.Id