Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

JOIN issue Expand / Collapse
Author
Message
Posted Friday, October 4, 2013 9:20 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 9:54 AM
Points: 236, Visits: 440
I am having a join issue because of duplicate value in my join keys. I cannot change the data source or the structure of tables. So how can I get my required output as described below.


CREATE TABLE Table1
(
Id INT,
Name VARCHAR(50)
);


CREATE TABLE Table2
(
Name VARCHAR(50),
Code VARCHAR(10)
);

INSERT INTO Table1 VALUES (1, 'Pen');
INSERT INTO Table1 VALUES (2, 'Pen');

INSERT INTO Table2 VALUES ('Pen', 'p1');
INSERT INTO Table2 VALUES ('Pen', 'p2');



Here is my current query


SELECT DISTINCT a.Id, a.Name, b.Code
FROM Table1 a
INNER JOIN Table2 b ON a.Name = b.Name



Here is the current output

Id Name Code
1 Pen p1
1 Pen p2
2 Pen p1
2 Pen p2


But here is what i want

Id Name Code
1 Pen p1
2 Pen p2



So basically i want to join the first record from table1 to first matching record in table2, and second record (with same name) from table1 should match to the second record (with same name) from table2 and so on.
Post #1501637
Posted Friday, October 4, 2013 9:34 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:37 PM
Points: 3,374, Visits: 7,300
This could cause you some serious problems. Until you become able to change anything, here are two possible solutions, but try to convince someone to authorize the change.

SELECT *
FROM Table1 t1
JOIN Table2 t2 ON LEFT( t1.Name, 1) + CAST( t1.Id AS varchar(10)) = t2.Code

SELECT *
FROM Table1 t1
JOIN Table2 t2 ON t1.Id = CAST( REPLACE( t2.Code, 'p', '') AS Int)




Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1501647
Posted Friday, October 4, 2013 9:38 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: 2 days ago @ 5:11 AM
Points: 94, Visits: 328
if the logic is like this then you may use RIGHT (not in where becouse there is performance problem)

you can try on this way:

select * from TABLE1 t join
( select *, right(code,1)code form TABLE2 )q on t.id = q.code and t.name = q.name

or
select *, right(code,1)code into #temp form TABLE2

and after that you can join two tables.
Post #1501650
Posted Friday, October 4, 2013 9:51 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:37 PM
Points: 3,374, Visits: 7,300
SrcName (10/4/2013)
if the logic is like this then you may use RIGHT (not in where becouse there is performance problem)

you can try on this way:

select * from TABLE1 t join
( select *, right(code,1)code form TABLE2 )q on t.id = q.code and t.name = q.name

or
select *, right(code,1)code into #temp form TABLE2

and after that you can join two tables.

You might be just following ideas to improve performance without knowing the complete part. Functions on the WHERE clause and on the ON clause for a JOIN. The reason is because SQL Server won't be able to use indexes but there's no way that an index might be helpful for this join.

Your second suggestion implies that you have to copy table2 into a temp table. That will have an overhead for the extra read and write for the whole table and again, no index gain.



Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1501658
Posted Friday, October 4, 2013 10:11 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 9:54 AM
Points: 236, Visits: 440
Thanks for the suggestions guys. I am sorry i should not have used "P1" & "p2" as codes, these are just my examples codes actual values won't have any correlation with Id (e.g. actual code value could be "2956" or "4684" etc) so i cannot use what you guys suggested.


I want a solution that doesn't depend on values. I was thinking something like, get minimum code for the first matching record, then get the minimum code (greater than the code applied on first record) for the second record and so on.
Post #1501664
Posted Friday, October 4, 2013 10:54 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:37 PM
Points: 3,374, Visits: 7,300
You might want to add a column to your queries, using ROW_NUMBER().
Here's an example, but without some data more similar to the real data, it's hard to give a good option.
WITH t1 AS(
SELECT *,
ROW_NUMBER() OVER( Order BY Id) AS rn
FROM Table1
),
t2 AS(
SELECT *,
ROW_NUMBER() OVER( Order BY Code) AS rn
FROM Table2
)
SELECT *
FROM t1
JOIN t2 ON t1.rn = t2.rn




Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1501687
Posted Friday, October 4, 2013 10:54 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:28 PM
Points: 13,138, Visits: 11,977
UDBA (10/4/2013)
Thanks for the suggestions guys. I am sorry i should not have used "P1" & "p2" as codes, these are just my examples codes actual values won't have any correlation with Id (e.g. actual code value could be "2956" or "4684" etc) so i cannot use what you guys suggested.


I want a solution that doesn't depend on values. I was thinking something like, get minimum code for the first matching record, then get the minimum code (greater than the code applied on first record) for the second record and so on.


I suppose you could rownumber but this is really sloppy to say the least. How do you define what is the first row? You are basically just picking an arbitrary value as your join condition which is bizarre.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1501688
Posted Friday, October 4, 2013 12:44 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 9:54 AM
Points: 236, Visits: 440
Luis Cazares (10/4/2013)
You might want to add a column to your queries, using ROW_NUMBER().
Here's an example, but without some data more similar to the real data, it's hard to give a good option.
WITH t1 AS(
SELECT *,
ROW_NUMBER() OVER( Order BY Id) AS rn
FROM Table1
),
t2 AS(
SELECT *,
ROW_NUMBER() OVER( Order BY Code) AS rn
FROM Table2
)
SELECT *
FROM t1
JOIN t2 ON t1.rn = t2.rn




Luis you beauty....THANK YOU.....it worked perfect on my real data and gives exactly what i was looking for.
Post #1501733
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse