Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:23:11+00:00 2026-06-10T11:23:11+00:00

I am using MS SQL. I have two tables: 1) OrderProducts – This table

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:23:12+00:00Added an answer on June 10, 2026 at 11:23 am

    Change the JOIN Condition to something like

    SELECT  SUM(Quantity) as Orders,  
            TransactionFeeProducts.ProductID, 
            FromDate, 
            ToDate  
    FROM    TransactionFeeProducts LEFT JOIN 
            OrderProducts   ON  TransactionFeeProducts.ProductID = OrderProducts.ProductID 
                            AND OrderDate >= TransactionFeeProducts.FromDate 
                            AND OrderDate <= TransactionFeeProducts.ToDate 
    WHERE   TransactionFeeProducts.BatchID = 3 
    GROUP BY    TransactionFeeProducts.ProductID, 
                FromDate, 
                ToDate  
    ORDER BY    SUM(Quantity) DESC
    

    The difference is that if you place the filtering conditions in the WHERE clause it will affect the query filtering the same as if you were to use an INNER JOIN, stating that you will only include entries from TransactionFeeProducts where OrderDate >= TransactionFeeProducts.FromDate and OrderDate <= TransactionFeeProducts.ToDate

    Have a look at the following example

    DECLARE @TABLE1 TABLE(
            ID INT,
            FromDate DATETIME,
            ToDate DATETIME
    )
    
    INSERT INTO @TABLE1 SELECT 1, '01 Jan 2012','31 Jan 2012'
    
    DECLARE @TABLE2 TABLE(
            ID INT,
            DateValue DATETIME
    )
    
    INSERT INTO @TABLE2 SELECT 1, '01 Feb 2012'
    
    SELECT  *
    FROM    @TABLE1 t1 LEFT JOIN
            @TABLE2 t2  ON  t1.ID = t2.ID
                        AND t2.DateValue BETWEEN t1.FromDate AND t1.ToDate
    
    SELECT  *
    FROM    @TABLE1 t1 LEFT JOIN
            @TABLE2 t2 ON   t1.ID = t2.ID
    WHERE   t2.DateValue BETWEEN t1.FromDate AND t1.ToDate
    

    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.ToDate

    Select 2 on the other hand says:

    Return all rows from T1 only those from T2 where t1.ID = t2.ID and then after that, only take rows where t2.DateValue BETWEEN t1.FromDate AND t1.ToDate

    SQL SERVER – Introduction to JOINs – Basic of JOINs is always handy to have around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL-Server 2005. I have two tables Users and Payments which has a
I'm using SQL Server 2008. I have two tables like this: OrderItems OrderItemID InventoryItemID
I'm using SQL Server 2008. Let's say I have two hypothetical tables like below:
I am using VB.NET with LINQ to MS SQL. I have two following tables.
I'm using SQL-SERVER 2005. I have two tables as you can see on diagram,
I am using SQL Server 2008, and I have two tables Table1 contains 3.5
using mysql and php, I have two tables, I'm french so the table names
I am using SQL Server and I have two tables and I would like
Using Microsoft SQL 2008. I have two tables that I want to select from
I'm new to SQL, so this isn't too complicated. I have two tables, with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.