1

I have a DB where there are [Session] and [SessionAsset] tables.

SessionAsset has a ThumbnailUrl that I want to get.

There are two SessionAssets associated with a single Session.

What would be the best way to take just 1 of those ThumbnailUrls? If I join Session with SessionAsset on SessionId (primary key), then there will be duplicate rows.

1
  • use SELECT TOP 1 <columnnames> Commented Feb 8, 2019 at 0:36

2 Answers 2

1

Assuming that both records in SessionAssets have the same ThumbnailUrl, one solution would be to use a subquery that selects distinct SessionId/ThumbnailUrl tuples.

SELECT s.*, sa.ThumbnailUrl
FROM Session s
INNER JOIN ( -- or maybe LEFT JOIN ?
    SELECT DISTINCT SessionID, ThumbnailUrl FROM SessionAssets
) sa ON sa.SessionId = s.SessionID

If the ThumbnailUrls are different and you want to get just one of them, then you might want to use aggregation :

SELECT s.*, sa.ThumbnailUrl
FROM Session s
INNER JOIN (
    SELECT SessionID, MAX(ThumbnailUrl) ThumbnailUrl 
    FROM SessionAssets 
    GROUP BY SessionID
) sa ON sa.SessionId = s.SessionID
Sign up to request clarification or add additional context in comments.

Comments

0

Join two tables through [SessionId], and use DISTINCT keyword to eliminate duplicated [ThumbnailUrl]s.

SELECT DISTINCT ThumbnailUrl
FROM SessionAsset sa
JOIN Session s on s.SessionId = sa.SessionId

3 Comments

@HarshaB How do you mean? Isn't this the question asked for?
You shouldn't just dump code as an answer, explain what it does and how it solves the problem of the question.
@SalomonZhang : As Mark Rotteveel suggested in above comment, just add some explanation. So that SO will not consider it as low quality post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.