I am having this table Sale_Order:
OrderId TotalAmount
1 200
2 560
52 4220
and I have the table Order_Line :
OrderLineId OrderId ProductId
12 1 5
5 2 56
I want to return only Sale_Order having matching Order_Line like below :
OrderId TotalAmount
1 200
2 560
A simple inner join would be BEST option in case as joins would run faster than sub query. Please check Link for details
SELECT
sd.OrderId,
sd.TotalAmount
FROM
Sales_Order sd
INNER JOIN
Order_Line ol
ON
(sd.OrderId = ol.OrderId)
Try EXISTS :
select * from sale_order as so where exists (select * from sale_order_line as sol where so.orderid=sol.order_id)
Related
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]
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
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
Hi I wonder if you can help with the following query , I am going around in circles trying to get the syntax correct.
I have two Tables Orders
OrderID | Product ID | LineTotal
1 ABC 2
2 CDE 3
2 DEF 1
and Products Table Containing the Weight and Cost
ProductID | Weight | Cost
ABC 1 1
CDE 2 2
DEF 1 0.5
So for each order ID I need to SUM the LineTotal the Weight and the Cost.
Thanks for some pointers on how to go about this as I am getting errors with joins and silly results
Thanks
It should be very simple if I got the task right:
SELECT o.OrderID, o.ProductID, sum = (o.LineTotal + p.Weight + p.Cost)
FROM ORDERS o
INNER JOIN PRODUCTS p on o.ProductID = p.ProductID
Try this.
Select t3.OrderID , SUM(t3.SUM1) As TotalSum
From (Select t1.*,t2.Weight,t2.Cost,t1.LineTotal+t2.Weight+t2.Cost AS Sum1
from Orders t1
INNER JOIN Products t2
ON t1.ProductID=t2.ProductID ) t3
Group BY t3.OrderID
SupplierId NetAmt
1 400
2 400
3 300
2 200
SupplierId RecdAmt
2 200
2 200
2 200
Expectect result:
1 400
3 300
First Table is Inward Table and Second Table is Payment Table when i do payment to supplier who's payment done fully should not show again
you can do like this
select SupplierId , NetAmt from table1
where SupplierId not in (select SupplierId from table2)
or
you can also try this
SELECT SupplierId , NetAmt
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.SupplierId = t2.SupplierId
WHERE t2.SupplierId IS NULL
Seems you need to get the balance per supplier
SELECT ISNULL(NetTable.SupID, RecTable.SupID) AS SupID, (ISNULL(NetTable.TotalNet,0) - ISNULL(RecTable.TotalRec,0)) AS BalAmt
FROM (SELECT SupID, SUM(NetAmt) AS TotalNet FROM Table_1 GROUP BY SupID) AS NetTable
LEFT OUTER JOIN
(SELECT SupID, SUM(RecAmt) AS TotalRec FROM Table_2 GROUP BY SupID) AS RecTable
ON NetTable.SupID = RecTable.SupID
WHERE NetTable.TotalNet <> RecTable.TotalRec
Need to GROUP BY supplier each table separately & JOIN the summaries.