Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need T-SQL Statement grouping households

Posted on 2016-10-08
6
Medium Priority
?
54 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 2000 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 14

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 14

Expert Comment

by:Nakul Vachhrajani
ID: 41835376
Sorry, Jim. Didn't mean to overlap with your answer.
1
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

721 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