I query seperately two sets of data which returns identical columns.
The final column of both sets is a rank. My problem is that both sets (excluding the rank can contain the same data)
I then want to union the sets so I have a distinct list of data, however I want the final set ordered by rank.
This is causing me problems because the results dont get unioned properly as the identical records with different rankings remain, I only really want the top ranked result from the duplicates when put together into one set.
Any ideas how to do this, i.e. if it is in the first set dont include the record from the second set!
I have tried inserting both into a temp table (without the rank, and doing a distinct but then it keeps the record from the second set.Code:SELECT DISTINCT c.gsp_course_id , c.supplier_id , c.course_code , c.title , duration , 999 as RANKING FROM [Live].[tbl_Courses] c INNER JOIN ( SELECT course_code, location, datediff(dd,[start],[end])+1 as duration FROM [Live].[VW_Events] WITH (NOLOCK) WHERE start > CURRENT_TIMESTAMP ) evfuture ON c.course_code = evfuture.course_code WHERE ( (c.course_code LIKE ('%' + @search_text + '%')) OR (c.title LIKE ('%' + @search_text + '%')) ) UNION SELECT DISTINCT 0 , 1 , co.Course_CourseCode , co.Course_Title , co.Course_Duration , Key_TBL.Rank as RANKING FROM Corp_Rpt.Common.VW_COURSES co INNER JOIN ( SELECT Event_CourseCode FROM Corp_Rpt.Common.VW_EVENTS WITH (NOLOCK) WHERE Event_StartDate > CURRENT_TIMESTAMP ) evfuture ON co.Course_CourseCode = evfuture.Event_CourseCode INNER JOIN Live.tbl_CourseOutlines col on co.Course_CourseCode = col.course_code and co.Course_Active !='N' INNER JOIN CONTAINSTABLE (Live.tbl_CourseOutlines, *, @criteria ) AS KEY_TBL ON col.gsp_outline_id = Key_TBL.[key]





Reply With Quote