Solved

T-SQL - Get Same Address1 with Different Address2

Posted on 2016-08-03
6
30 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

760 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

20 Experts available now in Live!

Get 1:1 Help Now