Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
elic05
Yak Posting Veteran
62 Posts |
Posted - 2008-11-02 : 18:24:08
|
I have 3 tables1. Orders orderID (PK),clientID,purchesDate,totalPrice2. OrdersItems orderID (FK),productID (FK),units,price3. Products productID (PK),mainCategory,subCategoryI have to get the total sum of all products that where sold between two dates I understand that...first i have to select the orderIDs that where ordered between the 2 given dates from the Orders table (cause only in that tabe the dates are written).SELECT orderId FROM Orders WHERE purchesDate BETWEEN @d1 AND @d2then I have to get all the productIdDs that the user choosed they are filtered by the user choosing (specific main-category or sub-category. threfore they have to be selected from the Products table. SELECT productID FROM Products WHERE category = @categorythen i have to get from the OrderItems table a list of all productIDs that their orderIDs inner join with orderIDs that I got in the first stage and also are inner joined with the productIDs the user choosed and to SUN the price column for those specific ProductIDsis it possible to write such a query in one select? |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-02 : 23:32:34
|
it is possibleSELECT *FROM Orders oINNER JOIN OrderItems oiON oi.orderID =o.orderID INNER JOIN Products pON p.productID =oi.productID WHERE o.purchesDate BETWEEN @d1 AND @d2AND (p.maincategory =@categoryOR p.subCategory=@category) |
|
|
elic05
Yak Posting Veteran
62 Posts |
Posted - 2008-11-03 : 07:46:35
|
thanks visakh16butI need to SUM(oi.price) after all the JOINs and the WHEREswhere do i put it?i want to sum the price column in the OrderItems tableafter filtering prices of products that where not purchased between to given dates and filtering prices of products that are not of the given main category and given subcategoryin other words I want to know how much money i got by selling products of sepecific category between two datesand why do i have to selecet the clientID and the totalPrice columns of the Orders table?I think it should startSELECT SUM(price) FROM OrderItems.... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-03 : 09:12:00
|
is this what you want?SELECT SUM(oi.price) AS TotalPriceFROM Orders oINNER JOIN OrderItems oiON oi.orderID =o.orderID INNER JOIN Products pON p.productID =oi.productID WHERE o.purchesDate BETWEEN @d1 AND @d2AND (p.maincategory =@categoryOR p.subCategory=@category) |
|
|
elic05
Yak Posting Veteran
62 Posts |
Posted - 2008-11-03 : 12:01:46
|
wonderfull!!thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-03 : 12:04:46
|
cheers |
|
|
|
|
|