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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:35:09+00:00 2026-05-31T22:35:09+00:00

Ok, I have an example table with the following information and query. First up

  • 0

Ok, I have an example table with the following information and query.
First up is the data, with the question following at the end.

Here’s the SQL Dump:
http://pastie.org/private/o7zzajdpm6lzcbqrjolgg

Or you can use the included a visual below:

Purchases Table

|  id   |   brand   |    date     |

    1        b1       2000-01-01 
    2        b1       2000-01-03 
    3        b2       2000-01-04
    4        b3       2000-01-08
    5        b4       2000-01-14

Owners Table

id | firstname | lastname | purchaseid | itemCoupon | itemReturned | Accessories
 1     Jane        Doe          1           yes           no               4
 2     Jane        Doe          2           yes           no               2 
 3     Jane        Doe          3           no            no               1
 4     Jane        Doe          4           no            no               3
 5     Jane        Doe          5           no            yes              6

The Query

SELECT brand, COALESCE( SUM( inTime.Accessories ) , 0 ) AS acessory_sum
FROM purchases
INNER JOIN owners AS person ON person.purchaseid = purchases.id
AND person.firstname =  'Jane'
AND person.lastname =  'Doe'
LEFT JOIN owners AS inTime ON person.id = inTime.id
AND purchases.date
BETWEEN DATE(  '2000-01-01' ) 
AND DATE(  '2000-01-05' ) 
GROUP BY purchases.brand

This gives the following expected result:

| brand | accessory_sum
   b1          6
   b2          1
   b3          0
   b4          0

The question

Now, I would like to add to the query:

WHERE itemCoupon = 'yes' OR itemReturned = 'yes'

But this overrides the last join and when I do the same search above I get:

| brand | accessory_sum
   b1          6
   b2          1

Similarly I still want it to return No results found for 2000-01-04, 2000-01-08 using WHERE itemCoupon = 'yes' OR itemReturned = 'yes'. Removing the WHERE gives me zeros for all brands if I try to do it another way.

Basically I want to keep the way the WHERE behaves but also keep the format that I described in the first example of the expected output.

As it is now, using WHERE destroys the way the last LEFT JOIN works with COALESCE which fills the remaining brand rows with zeros.

  • 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-31T22:35:10+00:00Added an answer on May 31, 2026 at 10:35 pm

    Your WHERE turns the outer join into an inner join.

    You need to move your additionally condition into the LEFT JOIN condition:

    LEFT JOIN owners as inTime 
           ON person.id = inTime.id 
          AND purchases.date between purchases.date DATE ('2000-01-01') and DATE ('2000-01-05')
          AND (inTime.itemCoupon = 'yes' or inTime.itemReturned = 'yes')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following example in a SQL table Cust Group Sales A 1
I have the following query in VBA: DoCmd.RunSQL INSERT INTO table (value) VALUES ('example')
I have a SQL table like so: Update: I'm changing the example table as
I have the following database table with information about people, diseases, and drugs: PERSON_T
I'm following a simple ajax>php>mysql example posted here http://openenergymonitor.org/emon/node/107 I can only display information
I have a MS SQL query that joins multiple tables and an example of
I have a MySQL table which contains the following type of information: Date product
First of all Sorry if my question title sounds stupid.... I have the following
Say for example, I have the following two tables: TableA { _id } TableB
Does anyone have an example using the table object in YUI library. More specifically,

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.