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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:16:59+00:00 2026-05-11T15:16:59+00:00

Imagine I have the following 3 tables in SqlServer: Customer (CustomerID, FirstName, LastName) Address

  • 0

Imagine I have the following 3 tables in SqlServer:

Customer (CustomerID, FirstName, LastName) Address (AddressID, CustomerID, Line1, City, State) Product (ProductID, CustomerID, Description) 

A customer can have multiple delivery addresses and mulitple products.

What I would like to do is to list the number of customers for each State where the State is determined by the most recent Address record. Such as ‘How many customers last received a product in each State?’. Therefore I’m not interested in any previous Address records for the Customer, only the most Recent (determined by AddressID).

State | Number of Customers -------------------------- CA    | 32 GA    | 12 TX    | 0 OH    | 18 

I would normally do something like:

SELECT a.State, count(c.CustomerID) FROM Product p INNER JOIN Customer c ON c.CustomerID = p.CustomerID LEFT JOIN Address a ON a.CustomerID = c.CustomerID WHERE p.ProductID = 101 GROUP BY a.State 

However, as a Customer may have multiple Addresses will the customer only be counted in the State of the most recent Address record?

P.S. The above is purely an example scenario to easily explain the joins I am trying to achieve and does not reflect an actual system design.

  • 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. 2026-05-11T15:16:59+00:00Added an answer on May 11, 2026 at 3:16 pm

    Try this:

    SELECT a.State, count(c.CustomerID) FROM Product p INNER JOIN Customer c ON c.CustomerID = p.CustomerID LEFT JOIN Address a ON a.CustomerID = c.CustomerID        AND a.AddressID =          (            SELECT MAX(AddressID)             FROM Address z             WHERE z.CustomerID = a.CustomerID         ) WHERE p.ProductID = 101 GROUP BY a.State 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So imagine you have the following two tables: CREATE movies ( id int, name
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
Let's imagine that I have to make a table with the following structure with
Imagine I have the following code (simplified regarding my real context of course): <div
I came across the following weird chunk of code.Imagine you have the following typedef:
This is probably a silly question, but here it goes.Imagine you have the following
Imagine that i have the following code: <a:repeat value=#{bean.getList()} var=x > <li class=la> <span>
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do
Imagine the following situation: I have two branches: DEV and MAIN. I'm working on
Imagine following scenario: We have a lot of parallel development going on in several

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.