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 12»»

Easy (I hope) join question Expand / Collapse
Author
Message
Posted Thursday, August 1, 2013 2:53 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, August 21, 2014 5:36 AM
Points: 34, Visits: 146
Hi,

I have two tables with many fields in them. I am only interested in a few of the fields, and both of the tables have records that are duplicates on the fields I am interested in. I think if I do a left outer join A on B I will get all the records in A, but some of them will become duplicated because they exist in B more than once. How do I do a join so that I keep all the records from table A and fill in the missing blanks, but do not create additional rows? Here's a small example:

table A
Name | Address
Tai | #


table B
Name | Address
Tai | 123 Main St
Tai | 123 Main St
Tai | 123 Main St
Mike | 456 Broadway

desired output:
Tai 123 Main St

I do not expect there will ever be more than one distinct address for each name. However, if there is more than one distinct address then I would take one randomly and don't care if there are others that I did not get. I've done something similar to this but I had to use ROW_NUMBER() OVER (PARTITION BY... which given my newbie status seems complicated and it seemed like there should be an easier way.

Thanks for the help!
Tai
Post #1480153
Posted Thursday, August 1, 2013 3:12 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:32 PM
Points: 13,302, Visits: 12,168
DISTINCT?

GROUP BY?

It is kind of hard to say without more details about what your actual query looks like.


_______________________________________________________________

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 #1480164
Posted Thursday, August 1, 2013 3:26 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, August 21, 2014 5:36 AM
Points: 34, Visits: 146
My data example was flawed... suppose it is this instead. Then how would the query be written? Thanks.

table A
Name | Address
Tai | #
Mike | #


table B
Name | Address
Tai | 123 Main St
Tai | 897 Main St
Tai | 123 Main St
Mike | 456 Broadway
Mike | 456 Broadway

desired output:
Tai | 123 Main St
Mike | 456 Broadway

OR

desired output:
Tai | 897 Main St
Mike | 456 Broadway

(I don't care which of the two outputs I get... If I use "select a.name, b.address from a left outer join b on a.name = b.name" then I will end up with more than the two rows I am after.)
Post #1480170
Posted Thursday, August 1, 2013 3:36 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 5:47 PM
Points: 12,910, Visits: 32,020
here's my best guess for you: in this case,note how i provided a CREATE TABLE and sample data via INSERT INTO?
if you can do the same in future posts, you'll be able to get tested, working answers from our volunteers.

here i'm using row_number to partition by the name, and since we don't care which record we get, the order by doesn't have much of an impact.

the second query, where i limit it just to the first matching row is what i think you are after:
CREATE TABLE #MySampleData([Name] varchar(30),[Address]varchar(30))
INSERT INTO #MySampleData
SELECT 'Tai ',' 123 Main St' UNION ALL
SELECT 'Tai ',' 897 Main St' UNION ALL
SELECT 'Tai ',' 123 Main St' UNION ALL
SELECT 'Mike ',' 456 Broadway' UNION ALL
SELECT 'Mike ',' 456 Broadway'

SELECT row_number() over(partition by name order by name) AS RW,* FROM #MySampleData

select * FROM
(
SELECT row_number() over(partition by name order by name) AS RW,* FROM #MySampleData
)
WHERE RW = 1



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1480176
Posted Thursday, August 1, 2013 3:37 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:32 PM
Points: 13,302, Visits: 12,168
Here are two examples of how you could do that.

--method #1
with MyAddresses as
(
select a.name, b.address, ROW_NUMBER() over (partition by a.name order by (select null)) as RowNum
from a left outer join b on a.name = b.name
)

select *
from MyAddresses
where RowNum = 1

--Method #2
select *
from
(
select a.name, b.address, ROW_NUMBER() over (partition by a.name order by (select null)) as RowNum
from a left outer join b on a.name = b.name
)x
where x.RowNum = 1



_______________________________________________________________

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 #1480177
Posted Thursday, August 1, 2013 3:56 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, August 21, 2014 5:36 AM
Points: 34, Visits: 146
Thanks to both of you for the code. And thanks for the tip on posting code to create the sample data.

I am actually already doing it the way you both have shown... as I am just getting started though it is very good to know that is the right way, though, instead of a bloated way.

Tai
Post #1480190
Posted Thursday, August 1, 2013 3:59 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 7:30 PM
Points: 562, Visits: 2,618
This is what I came up with

--SAMPLE DATA
IF OBJECT_ID('tempdb..#table_A') IS NOT NULL
DROP TABLE #table_A;
IF OBJECT_ID('tempdb..#table_B') IS NOT NULL
DROP TABLE #table_B;
CREATE TABLE #table_A (name varchar(20), [address] varchar(20));
CREATE TABLE #table_B (name varchar(20), [address] varchar(20));

INSERT #table_A VALUES ('Tai','#'),('Mike','#');
INSERT #table_B VALUES
('Tai','123 Main St'),('Tai','897 Main St'),('Tai','123 Main St'),
('Mike','456 Broadway'),('Mike','456 Broadway');

--The query
WITH distinct_folks(name, r, [address]) AS
( SELECT DISTINCT name, RANK() OVER (PARTITION BY name ORDER BY [address]),
[address]
FROM #table_B)
SELECT b.name, b.[address]
FROM distinct_folks AS b
JOIN #table_A AS a ON b.name=a.name
WHERE r=1



-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)
My blog
Post #1480194
Posted Friday, August 2, 2013 8:32 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, August 21, 2014 5:36 AM
Points: 34, Visits: 146
Further confirmation that this is the right way to do it... I just had expected there would be something that did not require two queries and was thus simpler. Thanks Alan.

Tai
Post #1480435
Posted Friday, August 2, 2013 9:26 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 5:47 PM
Points: 12,910, Visits: 32,020
I just noticed every one of us skipped over a basic group by as an alternative, and went straight to row_number.


this is probably the simplest:
SELECT 
name,
min([address]) as address
From MySampleData
GROUP BY name



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1480464
Posted Friday, August 2, 2013 9:34 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:32 PM
Points: 13,302, Visits: 12,168
Lowell (8/2/2013)
I just noticed every one of us skipped over a basic group by as an alternative, and went straight to row_number.


this is probably the simplest:
SELECT 
name,
min([address]) as address
From MySampleData
GROUP BY name



haha you're right Lowell. I think for me it was the "doesn't matter which one" so I went to row_number instinctively so i could 'randomize' it with a useless order by. Good catch.


_______________________________________________________________

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 #1480470
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse