Solved

T-SQL - Get Same Address1 with Different Address2

Posted on 2016-08-03
6
47 Views
Last Modified: 2016-08-03
Hi Experts!

Have the following data and need a query to find the Pid that has same
Address1, but different Address2.
Query should RETURN 1, 4, and 5 -- this is for SQL Server 2012

PiD   Address1          Address2
----- -----------------       --------------
1     123 ABC St         Suite 201
1     123 XYZ St         Suite 201
1     123 Cookie St    Suite 201
1     123 ABC St        Suite 201
1     123 ABC St        Suite 301
2     457 Marvel St    Suite 1001
2     1 DownTown Lane  
3     2 Busy Rd         Apt 2001
3     2 Busy Ave        Apt 20
3     2 Busy Ave        Apt 20
4     10 CutOver Lane   Suite 201
4     10 CutOver Lane   Suite 301
4     10 CutOver Lane   Suite 401
5     10 CutOver Lane   Suite 201
5     10 CutOver Lane   Suite 301


DECLARE @PersonAddress TABLE
(
  PiD int, 
  Address1 varchar(50),
  Address2 varchar(50)
)

INSERT INTO @PersonAddress (PiD, Address1, Address2)
 SELECT 1, '123 ABC St', 'Suite 201' UNION ALL
 SELECT 1, '123 XYZ St', 'Suite 201' UNION ALL
 SELECT 1, '123 Cookie St', 'Suite 201' UNION ALL
 SELECT 1, '123 ABC St', 'Suite 201' UNION ALL
 SELECT 1, '123 ABC St', 'Suite 301' UNION ALL
 SELECT 2, '457 Marvel St', 'Suite 1001' UNION ALL
 SELECT 2, '1 DownTown Lane', '' UNION ALL
 SELECT 3, '2 Busy Rd', 'Apt 2001' UNION ALL
 SELECT 3, '2 Busy Ave', 'Apt 20' UNION ALL
 SELECT 3, '2 Busy Ave', 'Apt 20' UNION ALL
 SELECT 4, '10 CutOver Lane', 'Suite 201' UNION ALL
 SELECT 4, '10 CutOver Lane', 'Suite 301' UNION ALL
 SELECT 4, '10 CutOver Lane', 'Suite 401' UNION ALL
 SELECT 5, '10 CutOver Lane', 'Suite 201' UNION ALL
 SELECT 5, '10 CutOver Lane', 'Suite 301' 

Open in new window


TIA!
0
Comment
Question by:allanau20
  • 4
  • 2
6 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41741629
try this:
select a.PiD from
(
Select PiD, Address1
from @PersonAddress
group by PiD, Address1
having count(Address1) > 1
) a inner join
@PersonAddress b on a.PiD = b.PiD and a.Address1 = b.Address1
group by a.PiD
having count(distinct b.Address2) > 1

Open in new window

0
 
LVL 5

Author Comment

by:allanau20
ID: 41741634
Thanks Ryan; let me try it out..
0
 
LVL 5

Author Comment

by:allanau20
ID: 41741651
Ryan,

Here's an example where it shouldn't return the PiD,but it did:

DECLARE @PersonAddress TABLE
(
  PiD int, 
  Address1 varchar(50),
  Address2 varchar(50)
)

INSERT INTO @PersonAddress (PiD, Address1, Address2)
SELECT 30, '73-345 Power Pl', 'Suite 200' UNION ALL
SELECT 30, '73-345 Power Pl', 'Suite 200' UNION ALL
SELECT 30, '73-345 Power Pl', 'Suite 200' UNION ALL
SELECT 30, '73-345 Power Pl', 'Suite 200' UNION ALL
SELECT 30, '73-345 Power Pl', 'Suite 200' UNION ALL
SELECT 30, '73-345 Power Pl', 'Suite 200' UNION ALL
SELECT 30, '75-240 Krazy Dr', 'Suite 6B' UNION ALL
SELECT 30, '75-240 Krazy Dr', 'Suite 6B' UNION ALL
SELECT 30, '75-240 Krazy Dr', 'Suite 6B' UNION ALL
SELECT 30, '75-240 Krazy Dr', 'Suite 6B' UNION ALL
SELECT 30, '75-240 Krazy Dr', 'Suite 6B' UNION ALL
SELECT 30, '75-240 Krazy Dr', 'Suite 6B' UNION ALL
SELECT 30, '98-151 Saint St', 'Suite 142' UNION ALL
SELECT 30, '98-151 Saint St', 'Suite 142' UNION ALL
SELECT 30, '98-151 Saint St', 'Suite 142' UNION ALL
SELECT 30, '98-151 Saint St', 'Suite 142' UNION ALL
SELECT 30, '98-151 Saint St', 'Suite 142' 

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 51

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41741659
oppps, you may try this instead:
select a.PiD from
(
select a.PiD, a.Address1, b.Address2 from
	(
	Select PiD, Address1
	from @PersonAddress
	group by PiD, Address1
	having count(Address1) > 1
	) a inner join
	@PersonAddress b on a.PiD = b.PiD and a.Address1 = b.Address1
	group by a.PiD, a.Address1, b.Address2
) a
group by a.PiD, a.Address1
having count(a.PiD) > 1
order by a.PiD

Open in new window

0
 
LVL 5

Author Comment

by:allanau20
ID: 41741662
let me test it out ..
0
 
LVL 5

Author Comment

by:allanau20
ID: 41741669
gracias!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

828 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