I am using MS SQL. I have two tables:
1) “OrderProducts” – This table stores all the products an order has. There is another Orders main table, which we can leave out of this picture. Orders main table stores an order’s main data, while the OrderProducts table stores the details.
2) “TransactionFeeProducts” – This table stores all the products’ transaction fees that I want to charge for the orders. It has a BatchID, where multiple products can have the same BatchID.
Basically, all ordered products which OrderDate falls between TransactionFeeProducts.FromDate and TransactionFeeProducts.ToDate, will be charged TransactionFeeProducts.TransactionFee
OrderProducts
- ProductID
- Quantity
- OrderDate
TransactionFeeProducts
- BatchID
- ProductID
- FromDate
- ToDate
- TransactionFee
SQL:
SELECT SUM(Quantity) as Orders,
TransactionFeeProducts.ProductID, FromDate, ToDate
FROM TransactionFeeProducts
LEFT JOIN OrderProducts ON TransactionFeeProducts.ProductID = OrderProducts.ProductID
WHERE
OrderDate >= TransactionFeeProducts.FromDate AND
OrderDate <= TransactionFeeProducts.ToDate AND
TransactionFeeProducts.BatchID = 3
GROUP BY TransactionFeeProducts.ProductID, FromDate, ToDate
ORDER BY SUM(Quantity) DESC
I want the SQL to return ALL records in TransactionFeeProducts where the BatchID = 3.
SUM(Quantity) should only give me sum where the OrderDate are made between TransactionFeeProducts.FromDate and TransactionFeeProducts.ToDate
If SUM(Quantity) is 0, then the field should be NULL or 0.
My problem now is, the SQL doesn’t return any record if the SUM(Quantity) is 0.
Please help. Thank you very much.
Change the JOIN Condition to something like
The difference is that if you place the filtering conditions in the
WHEREclause it will affect the query filtering the same as if you were to use anINNER JOIN, stating that you will only include entries fromTransactionFeeProductswhereOrderDate >= TransactionFeeProducts.FromDate and OrderDate <= TransactionFeeProducts.ToDateHave a look at the following example
Select 1 says:
Return all rows from T1 and only those from T2 where
t1.ID = t2.ID AND t2.DateValue BETWEEN t1.FromDate AND t1.ToDateSelect 2 on the other hand says:
Return all rows from T1 only those from T2 where
t1.ID = t2.IDand then after that, only take rows wheret2.DateValue BETWEEN t1.FromDate AND t1.ToDateSQL SERVER – Introduction to JOINs – Basic of JOINs is always handy to have around.