Solved

T-SQL - Get Same Address1 with Different Address2

Posted on 2016-08-03
6
38 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 49

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 49

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now