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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:32:07+00:00 2026-06-11T13:32:07+00:00

I am trying to create a query to list counts for all (US) states

  • 0

I am trying to create a query to list counts for all (US) states in series of joins, so that it shows 0’s for all states defined in a table that does not appear in the rest of the data.

This is what I’ve come up with so far:

SELECT s.StateName,s.StateNum, COUNT(s.StateNum) as [count]
FROM States AS s
INNER JOIN StateIncludeInClueReport as scr ON scr.statenum = s.StateNum
LEFT JOIN Staging_Policy sp ON CONVERT(TINYINT,SUBSTRING(sp.ProducerCode,1,2)) = s.StateNum
left JOIN (SELECT MIN(sip.QuoteId)AS QuoteId,sip.rmId FROM Staging_Policy sip GROUP BY sip.RMID) as sq ON sq.QuoteId = sp.QuoteID
INNER JOIN dbo.ResultMaster AS rm ON rm.rmID = sq.RMID
INNER JOIN dbo.CreditReport AS cr ON rm.rmID = cr.rmID AND cr.PolType = 'AUTO 3.0'
GROUP BY CONVERT(TINYINT,SUBSTRING(sp.ProducerCode,1,2)), s.StateName, s.StateNum
ORDER BY s.StateNum

But I’m still not seeing an records that don’t appear in the rest of the data.

I’ve created a sqlFiddle with the given scema and sample data.

The current output its:

STATENAME   STATENUM    COUNT
Kentucky    16          14
Ohio        34          4

The desired output would be:

STATENAME   STATENUM    COUNT
Arkansas    3           0
Georgia     10          0
Indiana     13          0
Kentucky    16          14
Missouri    24          0
Ohio        34          4
Tennessee   41          0
Texas       42          0
Virginia    45          0

I’m not really a SQL expert and this has really been giving me trouble. Would anyone have some insights into what I’m doing wrong?

  • 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-11T13:32:08+00:00Added an answer on June 11, 2026 at 1:32 pm

    I made a few changes to your query, including using LEFT JOIN on most of the joins:

    SELECT s.StateName,
      s.StateNum,
      isNull(COUNT(cr.PolType), 0) as [count]
    FROM States AS s
    INNER JOIN StateIncludeInClueReport as scr 
      ON scr.statenum = s.StateNum
    LEFT JOIN Staging_Policy sp 
      ON CONVERT(TINYINT,SUBSTRING(sp.ProducerCode,1,2)) = s.StateNum
    LEFT JOIN 
    (
      SELECT MIN(sip.QuoteId)AS QuoteId, sip.rmId 
      FROM Staging_Policy sip GROUP BY sip.RMID
    ) as sq 
      ON sq.QuoteId = sp.QuoteID
    LEFT JOIN dbo.ResultMaster AS rm 
      ON rm.rmID = sq.RMID
    LEFT JOIN dbo.CreditReport AS cr 
      ON rm.rmID = cr.rmID
      AND cr.PolType = 'AUTO 3.0'
    GROUP BY CONVERT(TINYINT,SUBSTRING(sp.ProducerCode,1,2)), s.StateName, s.StateNum
    ORDER BY s.StateNum;
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

I'm trying to create a query so that all items of a given list
I am trying to create a query that will list all books by the
I am trying to create a query that will return a List of Person
I'm trying to create a Linq query for EF that joins on 2 values
I'm trying to create an SQL query in PHP to update a table. Is
I'm trying to create a LINQ query (or queries) that count the total number
I am trying to list all the duplicate records in a table. This table
I'm trying to create a query on SQL server 2005 that will check if
I'm trying to create a table of users that are part of a group
I'm trying to display all the users I have not voted on yet! CREATE

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.