Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 single records with joins

Author  Topic 

tech1
Starting Member

49 Posts

Posted - 2011-07-07 : 17:35:18
This is a seperate question than my other thread.

I have tables:

Tracks
TrackID
TrackName
ArtistID (FK)
GenreID (FK)

Stages
StageID
StageName

Occasions
OccasionID
OccasionName

TrackOccasions
ID
TrackID
OccasionID
StageID

A track is associated with one or more occasions BUT ALSO can be for 1 or more stages for that occasion

an occasion has one or more stages

im sure you can figure this out.

My query gets tracks based on say, an artist name.
The query is fine as I am doing inner joins. great.
Problem: I only want to get a singular record for a track where the artist name matches for an occasion.

Its difficult to explain. Maybe this will help:

TrackID: 1
Artist: Elton John
TrackName: Candle In the Wind
Occasion: FirstOccasion
Stage: Initial



TrackID: 1
Artist: Elton John
TrackName: Candle In the Wind
Occasion: FirstOccasion
Stage: Middle


TrackID: 1
Artist: Elton John
TrackName: Candle In the Wind
Occasion: FirstOccasion
Stage: End


so what I want is just to get the one record from the above search BUT the join for TrackOccasions must remain.

how can I do this even if I have to abstract it out even more?



SPROC:


SELECT a.ArtistName, g.GenreName, g.GenreID,
t.TrackID, t.TrackName, t.FullDuration, t.NonRegisteredPlaybackStartTime,
t.NonRegisteredPlaybackEndTime, t.RegisteredPlaybackStartTime,
t.RegisteredPlaybackEndTime, t.TrackDescription, t.TrackURL, t.GuidanceNotes, t.Religious, t.CatNo, t.Era, t.Tempo, t.Vocals,
s.StageID, s.StageName, t.[Year],
ROW_NUMBER() OVER (ORDER BY t.[TrackID] ASC) AS RowNumber
FROM Tracks t

INNER JOIN Artist a ON
a.ArtistID = t.ArtistID
INNER JOIN Genres g ON
g.GenreID = t.GenreID
INNER JOIN TrackOccasion tOcc ON
tOcc.TrackID = t.TrackID
INNER JOIN Stages s ON
s.StageID = tocc.StageID


WHERE a.ArtistName LIKE '%' + @keyword + '%'

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2011-07-07 : 19:54:02
WHERE a.ArtistName LIKE '%' + @keyword + '%'
AND RowNumber = 1

???

=======================================
I have never met a man so ignorant that I couldn't learn something from him. -Galileo Galilei, physicist and astronomer (1564-1642)
Go to Top of Page
   

- Advertisement -