SELECT AssetValue
FROM Assets
WHERE (AssetType = 'Country')
Very simple Select Statement, but it outputs
Afghanistan
Albania
Algeria
....
How do I make United States and Canada appear on the top of this? Is this possible with SQL or do I need to do something in my ASP code? My PK is a INT Identity.
We could use FIRST_VALUE() in SQL Server to find the first value from any table. FIRST_VALUE() function used in SQL server is a type of window function that results in the first value in an ordered partition of the given data set.
To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.
The FIRST() function returns the first value of the selected column.
SELECT AssetValue
FROM Assets
WHERE (AssetType = 'Country')
ORDER BY CASE AssetValue
WHEN 'US' THEN 1
WHEN 'Canada' THEN 2 ELSE 3 END, AssetValue
If you do not wish to hard code, the most generic solution is having an extra field to indicate the preferential items. You would call it OrderPreference
. The higher OrderPreference, the first the item would appear. Every item else would have OrderPreference equals ZERO. The query:
SELECT AssetValue
FROM Assets
WHERE (AssetType = 'Country')
ORDER BY OrderPreference desc, AssetValue
By the looks of it, Assets is a table you use for other things (not only countries), so you could use this approach to solve the same problem for others asset types, if they come up.
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