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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:22:39+00:00 2026-05-27T20:22:39+00:00

Say we have a table like this: **tablename** ID Name Size Date AtStore1 AtStore2

  • 0

Say we have a table like this:

**tablename**
ID     Name      Size      Date   AtStore1  AtStore2
 1    Apple    Medium  20120101                  Yes
 2     Pear    Medium  20111231        Yes       Yes
 3    Lemon     Small  20111231        Yes       Yes
 4   Orange     Small  20111231        Yes          
 5   Carrot    Medium  20111231        Yes          
 6   Potato     Small  20111231        Yes          
 7   Celery     Large  20111231                  Yes
 8    Onion    Medium  20111231                     
 9   Tomato    Medium  20111231                     
 10   Apple    Medium  20111231                     
 12    Pear    Medium  20111230        Yes       Yes
 13   Lemon     Small  20111230        Yes       
 14  Orange     Small  20111230                  Yes
 15  Carrot    Medium  20111230                  Yes
 16  Potato     Small  20111230           
 17  Celery     Large  20111229                     
 18   Onion    Medium  20111229                  Yes
 19  Tomato    Medium  20111229                     

Can we construct an efficient query that retrieves ALL data per row where:

  • “Yes” AtStore1 rows where there are at least 4 “Yes”es in AtStore1 on that day single day
    OR (inclusive)
  • There is a “Yes” in AtStore2

Also acceptable is if only the first parameter is satisfied, i.e. the forward lookup of rows. I can probably script the AtStore2 part with PHP if necessary. All of my attempts have failed miserably; my attempts on how to write the question effectively in Google weren’t fruitful either.

(Answers breaking things out into different parts of code in PHP are fine too, I just want something reasonably efficient.)

For this example table, these would be the expected rows:

ID     Name      Size      Date   AtStore1  AtStore2
 1    Apple    Medium  20120101                  Yes
 2     Pear    Medium  20111231        Yes       Yes
 3    Lemon     Small  20111231        Yes       Yes
 4   Orange     Small  20111231        Yes          
 5   Carrot    Medium  20111231        Yes          
 6   Potato     Small  20111231        Yes          
 7   Celery     Large  20111231                  Yes
12     Pear    Medium  20111230        Yes       Yes
14   Orange     Small  20111230                  Yes
15   Carrot    Medium  20111230                  Yes
18    Onion    Medium  20111229                  Yes

As you can see

  • Jan 1 2012
    -No Yeses in AtStore1
    -AtStore2 has a Yes so the row is returned
  • Dec 31
    -5 Yeses in AtStore1, so those are returned (AtStore2 Yeses in ID 2 and 3 would have sufficed as well)
    -Also has ID 7 since there is a Yes in AtStore2
  • The rest of the rows returned were only because of Yeses in AtStore2
  • 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-27T20:22:39+00:00Added an answer on May 27, 2026 at 8:22 pm

    Try this:

    select t.* 
    from tablename t
    join (
        select date
        from tablename
        where AtStore1='yes'
        group by date
        having count(*) >= 4
    ) ta on t.date = ta.date and t.AtStore1 = 'yes'
    union
    select *
    from tablename
    where AtStore2 = 'yes'
    

    Result:

    ID  Name    Size    Date    AtStore1    AtStore2
    1   Apple   Medium  2012-01-01 00:00:00.000 NULL    Yes
    2   Pear    Medium  2011-12-31 00:00:00.000 Yes Yes
    3   Lemon   Small   2011-12-31 00:00:00.000 Yes Yes
    4   Orange  Small   2011-12-31 00:00:00.000 Yes NULL
    5   Carrot  Medium  2011-12-31 00:00:00.000 Yes NULL
    6   Potato  Small   2011-12-31 00:00:00.000 Yes NULL
    7   Celery  Large   2011-12-31 00:00:00.000 NULL    Yes
    12  Pear    Medium  2011-12-30 00:00:00.000 Yes Yes
    14  Orange  Small   2011-12-30 00:00:00.000 NULL    Yes
    15  Carrot  Medium  2011-12-30 00:00:00.000 NULL    Yes
    18  Onion   Medium  2011-12-29 00:00:00.000 NULL    Yes
    

    In this case null values are intentionally eliminated by an aggregate.

    I’m not sure about and t.AtStore1 = 'yes' in ta on t.date = ta.date and t.AtStore1 = 'yes' remove it if you want all rows from specific day without caring whether it has yes value in AtStore1 column.

    Added:

    Answer to question from comments, but this is quick and probably dirt way to do it (lack of time):

    select t3.*, t4.qty from tablename t3
    join (
        select id, max(qty) as qty from (
            select t.id, ta.qty
            from tablename t
            join (
                select date, count(*) as qty
                from tablename
                where AtStore1='yes'
                group by date
                having count(*) >= 4
            ) ta on t.date = ta.date and t.AtStore1 = 'yes'
            union
            select id, 0 as src
            from tablename
            where AtStore2 = 'yes'
        ) t2
        group by t2.id
    ) t4 on t3.id = t4.id
    

    Result:

    ID  Name    Size    Date    AtStore1    AtStore2    qty
    1   Apple   Medium  2012-01-01 00:00:00.000 NULL    Yes 0
    2   Pear    Medium  2011-12-31 00:00:00.000 Yes Yes 5
    3   Lemon   Small   2011-12-31 00:00:00.000 Yes Yes 5
    4   Orange  Small   2011-12-31 00:00:00.000 Yes NULL    5
    5   Carrot  Medium  2011-12-31 00:00:00.000 Yes NULL    5
    6   Potato  Small   2011-12-31 00:00:00.000 Yes NULL    5
    7   Celery  Large   2011-12-31 00:00:00.000 NULL    Yes 0
    12  Pear    Medium  2011-12-30 00:00:00.000 Yes Yes 0
    14  Orange  Small   2011-12-30 00:00:00.000 NULL    Yes 0
    15  Carrot  Medium  2011-12-30 00:00:00.000 NULL    Yes 0
    18  Onion   Medium  2011-12-29 00:00:00.000 NULL    Yes 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a table like this: name | score_a | score_b -----+---------+--------
Let's say I have a table like this: name |order_id ======================= first_record | 0
Say I have a table like this: Table name: Test col1 | col2 |
Let's say I have a table like this a | b | c |
In MS Transact SQL, let's say I have a table (Orders) like this: Order
Let's say I have a MySQL table that is something like this: software table:
Lets say I have a MySQL table, Users, like this - ----------------------------------------- ID |
Well, I'll say it like this. I have a table with 2 columns. The
I have a domain object annotated like this for hibernate support. @Entity @Table(name =
Let's just say I have a table called TABLE_NAME that looks like this: id

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.