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
Related
I tried to join some Products based on if they're in a table or not. I'm using MSSQL and I'm trying to do this to see if the category has some products.
simplified Query:
SELECT c.CategoryID, c.Name, p.ProductID
FROM Category AS c
JOIN Product AS p ON p.ProductID IN (
SELECT PrductID FROM exampleTable
)
ProductTable:
ProductID
CategoryID
1
1
2
1
3
2
4
4
The output I receive:
CategoryID
Name
ProductID
1
Cat1
1
1
Cat1
2
2
Cat2
3
4
Cat4
4
The expected output:
CategoryID
Name
ProductID
1
Cat1
1
2
Cat2
3
4
Cat4
4
I'm trying to only join a product if it's in the select statement and not join all products which have the same category id.
In pseudo code I'm trying to achive this:
JOIN Product AS p IF p.ProductID IN (Subquery)
Is this somehow possible?
Ed banga's answer is IMHO more elegant and perfoment but to be closer to what you proposed in your question, you can simply use a where clause.
SELECT c.CategoryID, c.Name, p.ProductID
FROM Category AS c
JOIN Product AS p ON p.CategoryID = c.CategoryID
WHERE p.ProductID IN (
SELECT PrductID FROM exampleTable
)
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)
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
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
I have two tables: tblProduct which has list of Products, and tblConsumer which has consumer name with consumed product ID.
Now I need to find the name of consumers who have consumed all products from the product table.
I tried to solve this with using INTERSECT, but the problem is I have provide each productid in WHERE clause. This syntax gives the result that I wanted, but how do I write this query where I don’t need to specify each productID.
SELECT ConsumerName FROM tblConsumer WHERE ProductID= 1
INTERSECT
SELECT ConsumerName FROM tblConsumer WHERE ProductID =2
INTERSECT
SELECT ConsumerName FROM tblConsumer WHERE ProductID =3
tblProduct
---------------------------------
ProductID | Product Name
---------------------------------
1 | Mango
2 | Orange
3 | Banana
tblConsumer
---------------------------------
ConsumerName | ProductID
---------------------------------
David | 1
David | 3
David | 2
Henry | 3
Henry | 2
If you're actually wanting to list all the Products in tblProducts, then you can use NOT EXISTS...
Otherwise, if you have a list of the Products you want to check, you can do something like:
SELECT c.ConsumerName
FROM tblConsumer AS c
WHERE c.ProductID IN (1,2,3)
GROUP BY c.ConsumerName
HAVING COUNT(DISTINCT c.ProductID) = (SELECT COUNT(DISTINCT p.ProductID) FROM tblProduct WHERE p.ProductID IN (1,2,3))
;
But I think maybe you just want to use NOT EXISTS to eliminate the Consumers for whom there's a record they haven't bought.
Like this:
SELECT *
FROM tblPerson AS pn CROSS JOIN tblProduct AS pt /* Every possible combo */
WHERE NOT EXISTS (SELECT * FROM tblConsumer c
WHERE c.ConsumerName = pn.ConsumerName
AND c.ProductID = pt.ProductID)
;
I have an other small solution:
SELECT * FROM tblConsumer
WHERE NOT EXISTS (SELECT * FROM tblProduct
LEFT JOIN tblConsumer C ON tblProduct.ProductID = C.ProductID AND tblConsumer .ConsumerName = C.ConsumerName
WHERE C.ConsumerName IS NULL)
It will work if you add a new entry too. It just checks, that is there any record, where you cant make a connection between the given Consumer and a Product.