Link to home
Start Free TrialLog in
Avatar of s_sykes
s_sykesFlag for United States of America

asked on

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

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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
Avatar of s_sykes

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of s_sykes

ASKER

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