Hello I wonder how can it be done in SQL server.
My code doesn't work.
SELECT * FROM
TABLE_NAME
WHERE NAME = 'United Kingdom'
ORDER BY Name
Here is the sample:
And here is expected result:
Thank you for your help.
Answer: In Order by clause you can use a number which will indicate the ordinal position of the column name used in the select statement. For example Order by 2 means order by the second column values specified in the SELECT statement.
The RANK() function is a window function could be used in SQL Server to calculate a rank for each row within a partition of a result set. The same rank is assigned to the rows in a partition which have the same values. The rank of the first row is 1.
SELECT name, value, rn FROM ( SELECT name, value, ROW_NUMBER() OVER (PARTITION BY name ORDER BY value DESC ) AS rn FROM t ) tmp WHERE rn <= 3 ORDER BY name, rn ; Tested in: Postgres.
Use a case
in the order by
:
SELECT *
FROM TABLE_NAME
ORDER BY (CASE WHEN NAME = 'United Kingdom' THEN 1 ELSE 2 END), Name;
ORDER BY
accepts multiple keys. The first puts the desired values first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With