We help IT Professionals succeed at work.

Group by Household in SQL Server 2005

313 Views
Last Modified: 2012-05-11
I've been using this SQL code for awhile with great success to pull customers from a table called house and a transaction table called Activity.  This works great except that sometimes I would like to just get one record per household instead of one per person.  

Is there a way to add this into the query or just re-query this data?  If there are 2 people at the same address, I'd like to vary who I pull.

I saw this posted on another site, but I'm not exactly sure if this is best for what I'm trying to do.

WITH myCTE AS
(
SELECT
Member_ID
, Name
, Address
, City
, ROW_NUMBER() OVER(PARTITION BY Address, City ORDER BY Address, City) AS dupeNum
FROM
someTable
)

SELECT
*
FROM
myCTE
WHERE
(dupeNum = 1)
SELECT  HouseID, Prefix, Firstname, Middle, Lastname, Suffix, Address, Address2, City, State, Zip, Homephone, Round(Activ.HPC,2) As HPC, Convert(Varchar,Activ.MRDate,101) As MRDate, Activ.Multis, NewID() as Randn, '' as Mailcode,

	     (SELECT TOP 1 Amount
               FROM Activity Activ2
               Where (Activ2.HouseID = Activ.HouseID) and (Activ2.Dated = Activ.MRDate) AND (Activ2.OwnerID IN (2))
              ) as MRC
			  
		
		

FROM         House INNER JOIN (
                      SELECT Activity.HouseID, MAX(Activity.Amount) AS HPC, MAX(Activity.Dated) AS MRDate, COUNT(Activity.Amount) AS Multis
                      FROM Activity
					  WHERE 0=0
					  
						AND (Activity.OwnerID IN (2))
					  
                      GROUP BY Activity.HouseID
                      HAVING 0=0
					  
						
						AND (MAX(Activity.Amount) >=5.0000)
						
						AND (({d '2011-04-27'} - MAX(Activity.Dated))<=(12 * 30))
						
						
                      ) AS Activ ON House.HouseID = Activ.HouseID

Open in new window

Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
you have your "select" , and you just need to put:

;WITH cte as ( SELECT .... , ROW_NUMBER() OVER (PARTITION BY HouseID ) rn FROM ....  )
SELECT * 
  FROM cte 
WHERE rn = 1 

Open in new window


there is not a lot of magic in there.
btw, I have described this in my article also:
https://www.experts-exchange.com/A_3203.html
s_sykesIT Director

Author

Commented:
Cool.  So if I want to do it by Household (multiple members with different ID numbers),  and randomly output the household member I could put:

ROW_NUMBER() OVER (PARTITION BY Lastname,Address,Zip Order By NewID()) rn

Sometimes in my code I will generate more than one of these queries and then Union them together.  Do you think this will still work or only for a single query?
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
s_sykesIT Director

Author

Commented:
I think I'm almost there.  Here's how I wrote the single.  
WITH cte as (SELECT House.HouseID, Prefix, Firstname, Middle, Lastname, Suffix, Address, Address2, City, State, Zip, Homephone, Round(Activ.HPC,2) As HPC, Convert(Varchar,Activ.MRDate,101) As MRDate, Activ.Multis, NewID() as Randn, '' as Mailcode,

	     (SELECT TOP 1 Amount
               FROM Activity Activ2
               Where (Activ2.HouseID = Activ.HouseID) and (Activ2.Dated = Activ.MRDate) AND (Activ2.OwnerID IN (79))
              ) as MRC, ROW_NUMBER() OVER (PARTITION BY Lastname,Address,Zip Order By NewID()) rn
			  
		
		

FROM         House INNER JOIN (
                      SELECT Activity.HouseID, MAX(Activity.Amount) AS HPC, MAX(Activity.Dated) AS MRDate, COUNT(Activity.Amount) AS Multis
                      FROM Activity
					  WHERE 0=0
					  
						AND (Activity.OwnerID IN (79))
					  
                      GROUP BY Activity.HouseID
                      HAVING 0=0
					  
						
						AND (MAX(Activity.Amount) >=5.0000)
						
						AND (({d '2011-04-27'} - MAX(Activity.Dated))<=(24 * 30))
						
						
                      ) AS Activ ON House.HouseID = Activ.HouseID 

WHERE    0=0
			   
    AND (House.Suppressed = 0)
    
    AND (House.State NOT IN ('AA','AE','AP','AS','FM','GU','MH','MP','ON','PR','PW','VI'))
	
)

SELECT * 
FROM cte 
WHERE rn =1

Open in new window

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.