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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:17:17+00:00 2026-05-18T22:17:17+00:00

I have a SQL table AddressCount that has 3 fields: address , state ,

  • 0

I have a SQL table AddressCount that has 3 fields: address, state, and numEntities. What I want to do is get the sum of numEntities for each address + state combination based on a like. This is somewhat hard to describe, but I think the following correlated query represents correctly what I’m trying to accomplish:

SELECT Address, State, 
    (SELECT SUM(NumEntities)
    FROM AddressCount innerQry 
    WHERE innerQry.address LIKE '%' + outerQry.address + '%' 
        AND innerQry.state = outerQry.state) As NumEntities
FROM AddressCount outerQry

Basically, I want to end up with a table that has an address, a state, and the sum of numEntities for all addresses like that address surrounded by wildcards. So, if the address is “25 Courtland Rd” and the state is NJ, I want to get the sum of numEntities for all rows that have a state of NJ and an address like ‘%25 Courtland Rd%’. I’d love to be able to accomplish this with a GROUP BY LIKE, but I don’t think this is possible with SQL. The basic concept is to accomplish something like this:

SELECT address, state, sum(numEntities)
FROM AddressCount
GROUP BY (LIKE '%' + address + '%')

The correlated query sql statement I posted above seems accurate, but I’d like to accomplish the same thing, if possible, by an inner join of the table on itself. I recently read that it’s possible to join tables on a LIKE expression, so I want to accomplish something like the following:

SELECT t1.Address, t1.State, SUM(t2.NumEntities)
FROM AddressCount t1
INNER JOIN AddressCount t2
    ON t1.state = t2.state
    AND t2.address LIKE '%' + t1.address + '%'

This doesn’t seem to work, though… any idea how I can accomplish this?

  • 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-18T22:17:18+00:00Added an answer on May 18, 2026 at 10:17 pm

    If I understand you correctly, adding GROUP BY t1.state, t1.address should work.

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

Sidebar

Related Questions

I have a SQL table which has a number of fields ID | Value
I have SQL table that has a varchar(8) column that occasionally has binary data
We have a SQL Server table containing Company Name, Address, and Contact name (among
I have a T-SQL table variable (not a table) which has an auto incrementing
I have a MS SQL table that I don't have any control over and
I have a table (SQL 2000) with over 10,000,000 records. Records get added at
I have a SQL table where in each row I store the country and
I have a SQL Table Roles in DB. I want to find out if
i have a sql table full of address' like this Hattie:07903 000066::Dad:07854 000095::Tom:07903 000044::Allistair:07765
I have a SQL table called StudentMarks.which consist of StudentID,SubjectName,SubjectMark I want to write

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.