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

  • Home
  • SEARCH
  • 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 7680209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:03:45+00:00 2026-05-31T18:03:45+00:00

I’m stuck with a certain query, it seems to be simple but it seems

  • 0

I’m stuck with a certain query, it seems to be simple but it seems I might have to split it in different ways to get it to accomplish what I want to do. I tried combining, recombining and I could use some guidance.

Here’s a sample table, the output and expected output.

Shop_Table

shop_id |     item_type  | date

   1          soaps         2000-01-01
   2          food          2000-01-02
   3          appliances    2000-01-03
   4          electronics   2000-01-10
   5          furniture     2000-01-13
   6          misc.         2000-01-15

Instance_Table

instanceid|    shop_id    | firstname | lastname | number_of_items

   11            3            jane        doe          2
   22            2            jane        doe          3
   33            1            jane        doe          5
   44            4            jane        doe          6
   55            6            jane        doe          1
   66            5            jane        doe          2

The Query

SELECT 
IF
( 
    Shop_Table.item_type
    IN
        (
            'soaps',
            'food'
         ),
    'indexpensives',
    Shop_Table.item_type
), 
COALESCE(SUM(Person.number_of_items), 0) AS item_sum
FROM Shop_Table

INNER JOIN Instance_Table AS Instance
ON Instance.shop_id = Shop_Table.shop_id
AND Instance.firstname =  'jane'
AND Instance.lastname =  'doe'
AND Shop_Table.date BETWEEN DATE(  '2000-01-01' ) AND DATE(  '2000-01-03' )

GROUP BY Shop_Table.item_type

Which returns:

  item_type     |     item_sum

indexpensives   |        8
appliances      |        2

Output

Which is great so far, and is where I’m stuck. What I really want is the following output:

Expected Output

  item_type     |     item_sum

inexpensives    |        8
appliances      |        2
electronics     |        0
furniture       |        0
misc.           |        0

I think it’s pretty straight forward what I want to do. What’s making it a little tough is that it has a little bit of everything.

  • Grouping and combining sum results (soaps and food into one result)
  • Returning 0 for the rest of the item types when the date is outside the specified range.

Update:

Turned out a little more complicated than I thought, but it’s what I wanted. Thank you everyone for helping out.

SELECT 
IF
( 
    Shop_Table.item_type
    IN
        (
            'soaps',
            'food'
         ),
    'inexpensives',
    Shop_Table.item_type
), 
COALESCE(SUM(Person.number_of_items), 0) AS item_sum
FROM Shop_Table
LEFT OUTER JOIN Instance_Table AS Instance
ON Instance.shop_id = Shop_Table.shop_id
AND Instance.firstname =  'jane'
AND Instance.lastname =  'doe'
AND Shop_Table.date BETWEEN DATE(  '2000-01-01' ) AND DATE(  '2000-01-03' )
GROUP BY IF
( 
    Shop_Table.item_type
    IN
        (
            'soaps',
            'food'
         ),
    'inexpensives',
    Shop_Table.item_type
)
  • 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-31T18:03:47+00:00Added an answer on May 31, 2026 at 6:03 pm

    Change your query to this and you should get the totals you’re looking for:

    SELECT 
    IF
    ( 
        Shop_Table.item_type
        IN
            (
                'soaps',
                'food'
             ),
        'indexpensives',
        Shop_Table.item_type
    ), 
    SUM(Person.number_of_items) AS item_sum
    FROM Shop_Table
    
    LEFT OUTER JOIN Instance_Table AS Instance
    ON Instance.shop_id = Shop_Table.shop_id
    AND Instance.firstname =  'jane'
    AND Instance.lastname =  'doe'
    AND Shop_Table.date BETWEEN DATE(  '2000-01-01' ) AND DATE(  '2000-01-03' )
    
    GROUP BY Shop_Table.item_type
    

    Explanation
    An outer join will select all rows from one table and matching rows from another. So, a LEFT OUTER JOIN will select all rows from the table on the left side of the join (Shop_Table in this case) and matching rows from the table on the right side of the join (Instance_Table in this case).

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.