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

The Archive Base Latest Questions

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

I have two tables in SQL Server 2008 R2: Store: Record: ————– ———————— Shape

  • 0

I have two tables in SQL Server 2008 R2:

Store:                      Record:
--------------              ------------------------
Shape       ID              PickedShape     Finished
Circle      1               Circle          Y
Circle      2               Circle          N
Circle      3               Square          N
Square      1               Square          N
Square      2               Oval            Y
Triangle    1               Oval            Y

I want to write a SQL query to output all the possible shapes and their counts across the two tables. I can write it in a silly way, like

select 
    'Circle', (select count(1) from Store where Shape = 'Circle'), 
    (select count(1) from Record where PickedShape = 'Circle'), 
    (select count(1) from Record where Finished = 'Y' and PickedShape = 'Circle') 
UNION
select 
    'Square', (select count(1) from Store where Shape = 'Square'), 
    (select count(1) from Record where PickedShape = 'Square'), 
    (select count(1) from Record where Finished = 'Y' and PickedShape = 'Square') 
UNION...

and go on, but that just silly and not efficient.

I think a more clever way is to use group by. Since some people may not like spoon feeding others, so here is what I tried

SELECT 
    Shape, COUNT(Shape) AS Available, Picked, Finished
FROM 
    Store 
FULL JOIN 
    (SELECT PickedShape, COUNT(1) As Picked, SUM(CASE WHEN Finished='Y' THEN 1 ELSE 0 END) AS Finished 
     FROM Record 
     GROUP BY PickedShape) t2 ON Store.Shape = t2.PickedShape
GROUP BY 
    Shape, Picked, Finished

and the output is

Shape   Available   Picked  Finished
NULL        0       2       2
Circle      3       2       1
Square      2       2       0
Triangle    1       NULL    NULL

You can see where the problems are.

First I want to have ‘Oval’ under Shape instead of NULL. Using FULL JOIN gives me all the variations in both the tables, but not showing them…

Second, I want Picked and Finished to show 0 instead of NULL for the missing ones.

Third, I want the SQL to be more efficient if possible.

How do you solve those problems?

Thank you!

  • 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:59:21+00:00Added an answer on June 14, 2026 at 4:59 am

    I a little bit modified your query
    In first query you grouped by Shape, but ‘Oval’ not exists in first query. Hence Shape receive from derived query.Triangle not exists in derived query, hence its quantity equals 0.

    SELECT 
      COALESCE(Shape, t2.PickedShape) AS Shape, COUNT(Shape) AS Available,
      ISNULL(Picked, 0) AS Picked, ISNULL(Finished, 0) AS Finished
    FROM 
     Store 
    FULL JOIN 
      (SELECT PickedShape, COUNT(1) As Picked, SUM(CASE WHEN Finished='Y' THEN 1 ELSE 0 END) AS Finished 
       FROM Record 
       GROUP BY PickedShape) t2 ON Store.Shape = t2.PickedShape
    GROUP BY 
      Shape, t2.PickedShape, Picked, Finished
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server 2008: I have two tables, dtlScheme and dtlRenewal, with a one
In SQL Server 2008 I have two tables. One table for users: id_user -
I'm using SQL Server 2008. Let's say I have two hypothetical tables like below:
This is SQL Server 2008. I have these two tables and a join: DECLARE
I have SQL Server 2008 Ent and OLTP database with two big tables. How
I have two tables with the exact same schema in SQL Server 2008 Standard
I have two tables in a SQL Server 2008 environment with the following structure
Using SQL Server 2008 R2 (Version 10.50.4000.0): I have two tables. I want to
I have two tables in the same DB on SQL Server 2008. When I
I am using SQL Server 2008 R2. I have two tables, ShoppingList and ShoppingListFood

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.