0

ive search for this problems solution for a while and although ive found some similar answers i can not seem to put them toguether to get the result i want. My problem is as follows: I have 3 tables in sql server 2008 (dont know if that matters much)

Order (order_id, sales_id, Product_id,combo_id)
product(product_id, name,price)
combo(combo_id,name price)

it doesnt really matter what sales have. Now i wanna get a sum of all the price for a sale id but the thing is for each row one of bouth product_id or combo_id will be null and i cant seem to get a result from using an inner join between the 3 tables, i think im realy lost in this one so i would apreciate a lot any help i can get Ive also try the following with a partial good result

SELECT        SUM(ISNULL(Combo.price, 0)) AS Expr1
FROM            Order INNER JOIN
                         Combo ON Order.combo_id = Combo.combo_id
WHERE        (Order.sales_id = @id) 

i wanna do something similar joinning the 3 tables but ive had no such luck. Thx again

1
  • What will be the third table does? it might affects your Result. Commented Dec 11, 2013 at 2:37

2 Answers 2

2

Try this

SELECT        SUM(ISNULL(Combo.price, 0)) AS Expr1, isnull(max(product.Price), 0.00) as ProdPRice
FROM            Order left outer join
                         Combo ON Order.combo_id = Combo.combo_id
                left outer join product on Order.Product_ID = product.Product_ID                      
WHERE        (Order.sales_id = @id) 
Sign up to request clarification or add additional context in comments.

Comments

2

If I undestand your need correctly, you want a sum from both product and combo. You can union the results from both

SELECT SUM(X.Expr1) AS Expr1
FROM
(
    SELECT        SUM(ISNULL(Combo.price, 0)) AS Expr1
    FROM            [Order] INNER JOIN
                         Combo ON [Order].combo_id = Combo.combo_id
    WHERE        ([Order].sales_id = @id) 

    UNION ALL

    SELECT        SUM(ISNULL(Product.price, 0)) AS Expr1
    FROM            [Order] INNER JOIN
                         Product ON [Order].Product_id = Product.Product_id
    WHERE        ([Order].sales_id = @id) 
) X

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.