Advertisement
dragonbs

Highest Peak Name and Elevation by Country

Sep 28th, 2023
1,921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.73 KB | None | 0 0
  1. SELECT TOP(5)
  2.     [CountryName] AS [Country],
  3.     ISNULL([Result].[PeakName], '(no highest peak)') AS [Highest Peak Name],
  4.     ISNULL([Result].[Elevation], 0) AS [Highest Peak Elevation],
  5.     ISNULL([Result].[MountainRange],'(no mountain)') AS [Mountain]
  6. FROM(SELECT
  7.         c.[CountryName],
  8.         p.[PeakName],
  9.         p.[Elevation],
  10.         m.[MountainRange],
  11.         DENSE_RANK() OVER (PARTITION BY c.[CountryName] ORDER BY p.[Elevation] DESC) AS [Rank]
  12.      FROM [Countries] c
  13.             LEFT JOIN [MountainsCountries] mc ON c.[CountryCode] = mc.[CountryCode]
  14.             LEFT JOIN [Mountains] m ON mc.[MountainId] = m.[Id]
  15.             LEFT JOIN [Peaks] p  ON m.[Id] = p.[MountainId]
  16.           ) AS [Result]
  17.  WHERE [Rank] = 1
  18.  ORDER BY [Country], [Highest Peak Name]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement