Quantcast
Channel: Answers for "To write an sql query"
Viewing all articles
Browse latest Browse all 4

Answer by Fatherjack

$
0
0
Using the build script from @Gopi Muluka, (but changing all tables to be temporary objects for the example) I wouldnt choose to use a CTE and simply do it with a self join: SELECT [r].[stars] AS [First review], [r].[ratingDate] [First review date], [r3].[name] AS [Reviewer], [m].[title] AS [Film], [m].[year] AS [Year], [m].[director] AS [Director], [r2].[stars] AS [Second review], [r2].[ratingDate] AS [Second review date] FROM [#Rating] AS r INNER JOIN [#Reviewer] AS r3 ON [r].[rID] = [r3].[rID] INNER JOIN [#Movie] AS m ON [r].[mID] = [m].[mID] INNER JOIN [#Rating] AS r2 ON [r].[mID] = [r2].[mID] AND [r].[rID] = [r2].[rID] WHERE [r2].[ratingDate] > [r].[ratingDate] AND [r2].[stars] > [r].[stars] writing the query this way is about half the cost of using the CTE method.

Viewing all articles
Browse latest Browse all 4

Trending Articles