Solved

Need T-SQL Statement grouping households

Posted on 2016-10-08
6
26 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 11

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 11

Expert Comment

by:Nakul Vachhrajani
ID: 41835376
Sorry, Jim. Didn't mean to overlap with your answer.
1
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now