?
Solved

Group by Household in SQL Server 2005

Posted on 2011-04-27
4
Medium Priority
?
264 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

0
Comment
Question by:s_sykes
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35480236
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
 
LVL 1

Author Comment

by:s_sykes
ID: 35480573
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
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 35480585
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
 
LVL 1

Author Comment

by:s_sykes
ID: 35480634
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

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

809 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