Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display string when field value duplicates

Status
Not open for further replies.

rrvveerriissssii

Technical User
Mar 16, 2017
1
US
Is there a way to write an expression that will display text when a field value is duplicated?

My report looks like this:

Region Name
North Amy
South John
East Paul
West Amy

I want it to look like this:
Region Name
North Amy (Also at West)
South John
East Paul
West Amy (Also at North)
 
I think you would need to identify the duplicate Regions in the supporting query.

Code:
DECLARE @Table TABLE (Region VARCHAR(16), Name VARCHAR(8))

INSERT INTO @Table VALUES ('North', 'Amy'), ('South', 'John'), ('East', 'Paul'), ('West', 'Amy')

SELECT t.*, Note = '(Also at ' + dup.Region + ')'
  FROM @Table t
 OUTER
 APPLY (SELECT TOP 1 Region
		  FROM @Table
		 WHERE Name = t.Name
		   AND Region <> t.Region) dup

But, what happens when Amy is at a 3rd location?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top