Solved

Need T-SQL Statement grouping households

Posted on 2016-10-08
6
32 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:charlesbaldo
  • 2
  • 2
  • 2
6 Comments
 
LVL 65

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 12

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 12

Expert Comment

by:Nakul Vachhrajani
ID: 41835376
Sorry, Jim. Didn't mean to overlap with your answer.
1
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Closing Comment

by:charlesbaldo
ID: 41835378
Thank you
0
 
LVL 65

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:charlesbaldo
ID: 41835479
Thank you both very much
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

776 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