Group by Household in SQL Server 2005

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

LVL 1
s_sykesAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
yes, that shall work just fine, as long as you put the whole UNION into a "inline view", for example:

;WITH cte as ( SELECT .... , ROW_NUMBER() OVER (PARTITION BY HouseID ) rn 
 FROM ( select ... from  ... where .. union all select ... from where ... etc ... ) sq
)
SELECT * 
  FROM cte 
WHERE rn = 1 

Open in new window


or with another CTE:


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

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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:
http://www.experts-exchange.com/A_3203.html
0
 
s_sykesAuthor 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?
0
 
s_sykesAuthor 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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.