Solved

Need T-SQL Statement grouping households

Posted on 2016-10-08
6
46 Views
Last Modified: 2016-10-08
I have the following data structure and I need a T-SQL that will produce  a list of "Households"

House Number        Name           Street
-----------------              --------------    ---------------------
127                             Joe               Main Street
127                             Jane             Main Street
127                            Betty             Main Street
127                            Lois               Main Street

150                            Irving            Main Street
150                            Ruth              Main Street

210                            Rene             Main Street
210                            Jill                  Main Street

62                              Fred              High  Street
62                              Bob               High Street

77                              Ethel             High Street
77                              Lucy              High Street
77                              Richard         High Street
77                              Vivian            High Street

62                               Gene            Low Street
62                               Edward        Low Street

 I need a T-SQL statement to know how many family households live on each street and produce this output

Street                  Households
--------------------    ------------------------
Main Street        3
High Street         2
Low Street          1
0
Comment
Question by:Charles Baldo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 41835373
Give COUNT(DISTINCT..) a whirl..
SELECT Street, COUNT(DISTINCT [House Number]) as Households
FROM YourTable
GROUP BY Street
ORDER BY COUNT(DISTINCT [House Number]) DESC

Open in new window

0
 
LVL 13

Expert Comment

by:Nakul Vachhrajani
ID: 41835374
You can do a COUNT of DISTINCT values as below:

DECLARE @houseInventory TABLE (HouseNumber INT, 
                               Name VARCHAR(255), 
                               Street VARCHAR(255)
                              );

INSERT INTO @houseInventory (HouseNumber, Name, Street)
VALUES (127, 'Joe'    , 'Main Street'),
       (127, 'Jane'   , 'Main Street'),
       (127, 'Betty'  , 'Main Street'),
       (127, 'Lois'   , 'Main Street'),
       (150, 'Irving' , 'Main Street'),
       (150, 'Ruth'   , 'Main Street'),
       (210, 'Rene'   , 'Main Street'),
       (210, 'Jill'   , 'Main Street'),
       (62 , 'Fred'   , 'High Street'),
       (62 , 'Bob'    , 'High Street'),
       (77 , 'Ethel'  , 'High Street'),
       (77 , 'Lucy'   , 'High Street'),
       (77 , 'Richard', 'High Street'),
       (77 , 'Vivian' , 'High Street'),
       (62 , 'Gene'   , 'Low Street' ),
       (62 , 'Edward' , 'Low Street' );

SELECT hi.Street,
       COUNT(DISTINCT hi.HouseNumber) AS NumberOfHouseholds
FROM @houseInventory AS hi
GROUP BY hi.Street

/* RESULTS
Street        NumberOfHouseholds
------------- ------------------
High Street   2
Low Street    1
Main Street   3
*/

Open in new window


By the way, I trust the 2 spaces between "High" and "Street" on House #62 (Fred) are by accident. In my example, I have only used one space (i.e. "High Street" instead of "High  Street")
0
 
LVL 13

Expert Comment

by:Nakul Vachhrajani
ID: 41835376
Sorry, Jim. Didn't mean to overlap with your answer.
1
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Closing Comment

by:Charles Baldo
ID: 41835378
Thank you
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41835381
No prob.  Two minute difference is not a big deal, and it takes at least that long to get the test T-SQL going.

Thanks for the grade, good luck with your project.  
Jim
0
 

Author Comment

by:Charles Baldo
ID: 41835479
Thank you both very much
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question