Solved

T-SQL - Get Same Address1 with Different Address2

Posted on 2016-08-03
6
62 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 52

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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 52

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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

628 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