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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:12:43+00:00 2026-06-14T04:12:43+00:00

I have an SQL query like the following: SELECT store_id, SUM(quantity_sold) AS count FROM

  • 0

I have an SQL query like the following:

SELECT store_id, SUM(quantity_sold) AS count
FROM sales_table
WHERE store_id IN ('Store1', 'Store2', 'Store3')
GROUP BY store_id;

This returns a row for each store that has rows in sales_table, but does not return a row for those that do not. What I want is one row per store, with a 0 for count if it has no records.

How can I do this, assuming that I do not have access to a stores table?

  • 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-14T04:12:44+00:00Added an answer on June 14, 2026 at 4:12 am
    with stores (store_id) as (
       values ('Store1'), ('Store2'), ('Store3')
    )
    select st.store_id, 
           sum(sal.quantity_sold) as cnt
    from stores st
      left join sales_table sal on sal.store_id = st.store_id
    group by st.store_id;
    

    If you do have a stores table, then simply do an outer join to that one instead of “making one up” using the common table expression (with ..).

    This can also be written without the CTE (common table expression):

    select st.store_id, 
           sum(sal.quantity_sold) as cnt
    from (
      values ('Store1'), ('Store2'), ('Store3')
    ) st
      left join sales_table sal on sal.store_id = st.store_id
    group by st.store_id;
    

    (But I find the CTE version easier to understand)

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

Sidebar

Related Questions

I have an sql query like this: SELECT IF( (SELECT COUNT(*) FROM `test_table2` WHERE
Hi guys I have an SQL query like this: SELECT * FROM PEOPLE P
i have this sql query select * from table where name like ? but
In T-SQL you could have a query like: SELECT * FROM Users WHERE User_Rights
I have a sql query which looks like this - NSString *createSQL = @SELECT
I have an SQL query like this: set @fromdate = '2012/01/01 00:00:00' set @todate
I have built a T-SQL query like this: DECLARE @search nvarchar(1000) = 'FORMSOF(INFLECTIONAL,hills) AND
I have a SQL query in my ASP.net web app that looks like this:
I have a list of groups in a SQL query and would like them
i've a table like: i have to make sql query to get the average

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.