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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:11:08+00:00 2026-05-14T03:11:08+00:00

I’m in need of some basic TSQL help. Here’s my table layout: Orders Table

  • 0

I’m in need of some basic TSQL help. Here’s my table layout:

Orders Table

  • SSN
  • ZipCode

ZipLookup Table

  • ZipCode
  • State

All columns are varchars.

How would I get a list of States with the number of distinct SSN’s in each state? Preferably, if a particular SSN has orders from multiple states only the state with the most orders would be counted for that SSN.

Thank you for any tips you can give me.

  • 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-14T03:11:08+00:00Added an answer on May 14, 2026 at 3:11 am

    First of all, you’d better make sure that you’re allowed to be storing SSNs, because there are a whole bunch of privacy laws and regulations that may prohibit it.

    The query would look like this:

    SELECT z.State, COUNT(DISTINCT o.SSN) AS SsnCount
    FROM ZipLookup z
    INNER JOIN Orders o
        ON o.ZipCode = z.ZipCode
    GROUP BY z.State
    

    If you need to only count the SSN in its most-frequently-used State:

    WITH StateSSNs AS
    (
        SELECT
            o.SSN, z.State,
            ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) AS RowNum
        FROM Orders o
        INNER JOIN ZipLookup z
            ON z.ZipCode = o.ZipCode
        GROUP BY o.SSN, z.State
    )
    SELECT z.State, COUNT(*) AS SsnCount
    FROM ZipLookup z
    INNER JOIN StateSSNs s
        ON s.State = z.State
    WHERE s.RowNum = 1
    GROUP BY z.State
    

    Performance isn’t going to be very good, I think this will require at least one full scan and maybe two, but if you want anything better then you’ll need to normalize the schema.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.