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 7603337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:37:56+00:00 2026-05-30T23:37:56+00:00

I have a query that involves searching database over a range of 30 days.

  • 0

I have a query that involves searching database over a range of 30 days. Queries, both with correct output and wrong output are below:

CORRECT RESULTS:
SELECT
    affiliates.member_id,
    IFNULL( COUNT(orders.deal_id) , 0 ) AS deals_count,
    IFNULL( SUM(orders.quantity) , 0 ) AS deals_quanity
FROM affiliates
LEFT JOIN deals ON affiliates.member_id = deals.member_id
LEFT JOIN orders ON deals.deal_id = orders.deal_id
LEFT JOIN customers_orders_link ON orders.order_id = customers_orders_link.order_id
    AND DATE(customers_orders_link.datetime) BETWEEN '2011-06-01' AND '2011-07-01'
    AND customers_orders_link.order_status = 'Delivered'
GROUP BY affiliates.member_id;

EXPECTED & RECEIVED: (Correct)

MemberID    COUNT   SUM
1           11      16
2           0       0

WRONG RESULTS:

//Notice the change in the date range

SELECT
    affiliates.member_id,
    IFNULL( COUNT(orders.deal_id) , 0 ) AS deals_count,
    IFNULL( SUM(orders.quantity) , 0 ) AS deals_quanity
FROM affiliates
LEFT JOIN deals ON affiliates.member_id = deals.member_id
LEFT JOIN orders ON deals.deal_id = orders.deal_id
LEFT JOIN customers_orders_link ON orders.order_id = customers_orders_link.order_id
    AND DATE(customers_orders_link.datetime) BETWEEN '2011-10-01' AND '2011-10-31'
    AND customers_orders_link.order_status = 'Delivered'
GROUP BY affiliates.member_id

EXPECTED:

MemberID    COUNT   SUM
1           0       0
2           0       0

BUT I RECEIVE: (INCORRECT OUTPUT)

MemberID    COUNT   SUM
1           11      16
2           0       0

The first query is producing correct results whereas the second query is producing incorrect results. Even if I use a date in the past as the range, I still receive the same Incorrect Output. Its as if the query is completely ignoring the date range specification. So this case of ignoring the date range specification seems to be the problem.

How can I make the query “see” and “obey” the date range specification and actually receive the Expected Output for the 2nd query listed above?

EDIT 1:

//Table: Orders
order_id        deal_id quantity        price
1               1       2               40.00
1               2       1               15.00
2               1       1               20.00
3               9       1               5.00
4               1       2               40.00
4               9       2               10.00
5               1       1               20.00
5               9       1               5.00
6               1       2               40.00
6               9       2               10.00
7               1       1               20.00
8               11      1               1.00


//Table: customers_orders_link
order_id        customer_id     order_status    datetime
1               4               Cancelled       2011-06-05 20:26:45
2               4               Delivered       2011-06-05 20:38:28
3               4               Pending Payment 2011-06-05 20:56:50
4               4               Pending Payment 2011-06-09 17:03:08
5               4               Pending Payment 2011-06-09 17:12:23
6               4               Pending Payment 2011-06-09 17:19:57
7               4               Pending Payment 2011-06-09 17:40:59
8               4               Pending Payment 2011-06-10 03:55:17
  • 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-05-30T23:37:57+00:00Added an answer on May 30, 2026 at 11:37 pm

    I solved it myself using a totally different method.

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

Sidebar

Related Questions

In SQL Server 2005, I have a query that involves a bunch of large-ish
I've tested a weird query that involves me searching for non-existent records. In a
I have a Stored Proc query below which involves returning partial delimited search string.
I have written a query which involves joins and finally returns the below result,
I have a query that involves finding response times several times in a row,
I am using in C# MYsql .I have query that works if I run
I have a query that is dynamically built after looking up a field list
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a query that looks like this: public IList<Post> FetchLatestOrders(int pageIndex, int recordCount)
I have a query that I'm executing from a .NET application to a SQL

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.