GROUP BY in a FULL OUTER JOIN? - sql

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

Related

Postgres, order by SUM of column in child table

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

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]

SQL get a specified column from another table

there are 2 tables
-OrderDetails-
OrderDetailID OrderID ProductID Quantity
------------------------------------------------------------------
1 10248 11 12
2 10248 42 10
-Products-
ProductID ProductName Price
-----------------------------------------
11 Queso 20
42 Fried Mee 14
What i really wanna do is to extract Price from 'Product' Table and place it in 'OrderDetail' Table and match it with corresponding 'ProductID'. I felt like i have to first create a new column using 'alter table', so i did. but after that, i tried my best but i couldn't figure that out.
this means...
-OrderDetails-
OrderDetailID OrderID ProductID Quantity Price
-------------------------------------------------------------------------
1 10248 11 12 20
2 10248 42 10 14
this is what i want!
CREATE VIEW dbo.vOrderDetailsPrice
AS
SELECT
a.OrderDetailID,
a.OrderID,
a.ProductID,
a.Quantity,
b.Price
FROM OrderDetails a
INNER JOIN Products b
ON a.ProductID = b.ProductID
You can create a new table using this statement:
SELECT
OrderDetails.OderDetailID,
OrderDetails.OderID,
OrderDetails.ProductID,
OrderDetails.Quantity,
Product.Price
INTO
NewTable
FROM
OrderDetails, Product
WHERE
OrderDetails.ProductID = Product.ProductID;
Make sure that NewTable looks the way you want it to and then delete OrderDetails table. Then you can rename NewTable as OrderDetails table.
you could use inner join to get your result
SELECT OD.OrderDetailID,OD.OrderID,OD.ProductID,OD.Quantity,PD.Price
FROM ORDERDETAILS OD INNER JOIN PRODUCTS PD ON OD.PRODUCTID=PD.PRODUCTID

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().