Solved

Need T-SQL Statement grouping households

Posted on 2016-10-08
6
51 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

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…
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 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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

623 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