Postgres, order by SUM of column in child table - sql

I have the following (simplified) tables
products - id name
order_products - quantity, product_id
I would like to order the rows from products table based on the sum of quantity column in order_products for every product
If order_products has these rows
quantity - product_id
2 - 555
2 - 555
6 - 666
3 - 777
I want the results to be ordered like that: 666, 555, 777
666 has 6 quantity, 555 has 4 and 777 has 3
I ended up using slightly modified version of the answer, as my requirement was to include products even if they don't have any relations in order_products table.
select p.*, coalesce(sum(op.quantity), 0) quantity_sold
from products p
left outer join order_products as op on op.product_id = p.id
group by p.id
order by quantity_sold desc

Just use ORDER BY and aggregate function
select p.product_id, p.name, sum(quantity) quantity_sold
from order_products op
join products p on op.product_id = p.product_id
group by p.product_id, p.name
order by sum(quantity) desc

Related

SQL filter unique and return total

I have two following tables:
products_table
id name
1 productA
2 productB
3 productC
inventory_table
id product_id amount
1 1 200
2 1 300
3 2 100
4 3 200
5 2 500
And the result I would like to get is
name total
productA 500
productB 600
productC 200
How could this be achieved using sql query?
Seems easy, first subSELECT query makes sums, parent one joins the names.
SELECT pt.name, n.total
FROM
(SELECT SUM(it.amount) as total, it.product_id
FROM inventory_table it
GROUP BY it.productID) n JOIN
products_table pt ON pt.id = n.product_id
I would try joining the two tables and use aggregation as
SELECT
p.name,
SUM(i.amount)
FROM product_table as p
LEFT JOIN inventory_table as i
ON p.id = i.product_id
GROUP by p.name
This is a simple inner join between the two tables, grouping by each Product and summing all its values.
select p.[name], Sum(i.amount) Amount
from product_table p join inventory_table i on i.product_id=p.id
group by p.[name]

GROUP BY in a FULL OUTER JOIN?

I am working on two tables
PRODUCT table
with
PRODUCTID, PRICE
and I am working with
ORDERLINE table
which has
PRODUCTID, QUANTITY, ORDERID
I want to join both tables so that I have
ORDERQUANTITY
which has
PRODUCT.PRODUCTID, ORDERLINE.QUANTITY
GROUPED BY
PRODUCTID
In other words I would like the two tables not to be like this:
PRODUCTID QUANTITY
- 10 4
- 10 2
- 20 1
- 20 6
- 30 4
- 30 6
- 30 2
- 30 2
- 40 2
- 40 2
- 40 5
But like this
PRODUCTID QUANTITY
- 10 6
- 20 7
- 30 14
- 40 9
My current code shown below only does the first table when I try to group by productid it won't work.
SELECT PRODUCT.PRODUCTID, ORDERLINE.QUANTITY
FROM ORDERLINE
FULL OUTER JOIN PRODUCT ON PRODUCT.PRODUCTID = ORDERLINE.PRODUCTID
ORDER BY PRODUCT.PRODUCTID;
You don't need the join if you need only id and sum of ordered quantity :
select PRODUCTID, sum(QUANTITY)
from ORDERLINE
group by PRODUCTID
order by PRODUCTID;
I think a simple SUM aggregate function and GROUP BY query will be enough:
SELECT PRODUCT.PRODUCTID, coalesce( SUM( ORDERLINE.QUANTITY), 0 )
FROM ORDERLINE
FULL OUTER JOIN PRODUCT ON PRODUCT.PRODUCTID = ORDERLINE.PRODUCTID
GROUP BY PRODUCT.PRODUCTID
ORDER BY PRODUCT.PRODUCTID;
I'm not sure about understanding your question, but I think that this sentece should give you the results you are asking for:
SELECT PRODUCT.PRODUCTID, SUM(ORDERLINE.QUANTITY) AS QUANTITY
FROM ORDERLINE
FULL OUTER JOIN PRODUCT ON PRODUCT.PRODUCTID = ORDERLINE.PRODUCTID
GROUP BY PRODUCT.PRODUCTID
ORDER BY PRODUCT.PRODUCTID;
SELECT PRODUCT.PRODUCTID, SUM(ORDERLINE.QUANTITY)
FROM ORDERLINE
FULL JOIN PRODUCT ON PRODUCT.PRODUCTID = ORDERLINE.PRODUCTID
GROUP BY PRODUCT.PRODUCTID
Here it is in action: SQL Fiddle

How to write Group by in Inner Join

Here I Have Two tables:
Orders
OrderId OrderName
1 Apple
2 Mango
Cust
Id Name OrderId Price
1 John 1 50
2 John 1 100
3 Mic 1 10
4 Mic 2 10
Sql Join Query:
SELECT Orders.CustName,Items.IteamName,Orders.Price
FROM Orders JOIN Items ON Items.Id = Orders.Id
Group By
SELECT Orders.CustName, SUM(Price) FROM Orders GROUP BY Orders.CustName
How can I Write Group by in Join?
You can do like this:
Select Orders.CustName, Items.IteamName, SUM(Orders.Price)
FROM
Orders JOIN Items ON Items.Id=Orders.Id
GROUP BY
Orders.CustName, Items.IteamName
The fields that you doesn't aggregate you put at the GROUP BYclause.

Sql SUM only when when value = Yes

I have 2 tables:
Order as master table with the columns OrderID, Amount, date, customer, Amount
OrderDetails as child table with the columns Id, OrderID, Item, status Boolean (yes or No), Price, quantity, SubTotal.
I want sum the subtotal only if the status is YES, then show the Sum in "amount" column in the master
SELECT
Order.OrderId, Order.Date, Order.Customer, Order.Amount,
SUM(OrderDetails.SubTotal ) AS TotalAmount,
FROM
Order
INNER JOIN
OrderDetails ON Order.OrderId = OrderDetails.OrderId
WHERE
(OrderId.status = Yes)
GROUP BY
Order.OrderId, Order.Date, Order.Customer, Order.Amount
This SQL statement only returns lines where the column Status is "Yes", but I want to return all of them but SUM only the lines with Status = Yes.
Example:
OrderDetails table
Order-Id item Price Quantity SubTotal Status
-------------------------------------------------------
M1 Shirt 10 10 100 yes
M1 item2 5 5 25 no
M2 Item3 7 5 35 yes
Order:
order-Id date Cus Amount
------------------------------------------------
M1 2016/8/20 Jean-Claude 100
M2 2016/8/20 Pierre 35
Thank you
You would seem to want something like this:
SELECT o.OrderId, o.Date, o.Customer, o.Amount,
SUM(CASE WHEN od.status = 'YES' THEN od.SubTotal ELSE 0 END)) AS TotalAmount
FROM Order o INNER JOIN
Orderdetails od
ON o.OrderId = od.OrderId
GROUP BY o.OrderId, o.Date, o.Customer, o.Amount;
The key is conditional aggregation -- having a CASE expression as the argument to the SUM().

SQL Construction Error

TABLE : ITEMS
---------------------------------------
Item_ID Description
---------------------------------------
1 Vivel Satin Soap
2 Flake
3 Maggie
4 Mango Juice
---------------------------------------
TABLE : SALES
------------------------------------------------
Sale_ID Sale_Date Item_ID Quantity
------------------------------------------------
1 15-Feb-14 1 2
2 16-Feb-14 1 1
3 16-Feb-14 2 1
4 17-Feb-14 3 1
5 18-Feb-14 1 1
6 18-Feb-14 2 2
------------------------------------------------
I'm having trouble in constructing SQL query as the way i wanted.... Here, i have two
tables in the database as shown above. The "ITEMS" table is for Items' Description Look-up
and "SALES" table for Items' Sale Record Look-up. Now, my requirement is, i want to select a records
from both the tables to generate report (as shown in the following). Report should contain
Items Description and its corresponding Sum of Quantity.
REPORT
------------------------------------------------------
Item_ID Description Total_Quantity
------------------------------------------------------
1 Vivel Satin Soap 4
2 Flake 3
3 Maggie 1
4 Mango Juice (SHOULD BE NULL HERE)
------------------------------------------------------
I tried following SQL query and some more to generate the report but had a logical error....
so, help me to construct better!
1) SELECT I.Item_ID, I.Description, Sum(S.Quantity)
FROM ITEMS I
INNER JOIN SALES S ON I.Item_ID = S.Item_ID
ORDER BY I.Item_ID;
2) Select I.Item_ID, I.Description, Sum(S.Quantity)
From ITEMS I, SALES S
Where S.Item_ID IN (Select Item_ID from ITEMS)
Order by I.Item_ID;
3) etc..........
try this,
SELECT I.Description, Sum(S.Quantity)
FROM ITEMS I
LEFT JOIN SALES S ON I.Item_ID = S.Item_ID
GROUP BY I.Description
Instead of Inner just try with Left Join...
And you need to use Group by Clause
SELECT I.Item_ID, I.Description, Sum(S.Quantity) -- It will return Null for Mango Juice
FROM ITEMS I
LEFT JOIN SALES S ON I.Item_ID = S.Item_ID
Group By I.Item_ID, I.Description
ORDER BY I.Item_ID;
OR:
SELECT I.Item_ID, I.Description, CASE WHEN Sum(S.Quantity) IS NULL THEN 0 ELSE Sum(S.Quantity) END Quantity -- It will return 0 for Mango Juice
FROM ITEMS I
LEFT JOIN SALES S ON I.Item_ID = S.Item_ID
Group By I.Item_ID, I.Description
ORDER BY I.Item_ID;