0

How to make single record from the record-set at the bottom of the list.

0. Select All
1. Apple
2. Banana
3. Mango

I need to place "Select All" at the bottom as :

1. Apple
2. Banana
3. Mango
0. Select All

Based on Id, not value .

Furqan Misarwala
  • 1,743
  • 6
  • 26
  • 53
  • Possible duplicate of [How to keep a specific row as the first result of a query (T-SQL)?](https://stackoverflow.com/questions/10176538/how-to-keep-a-specific-row-as-the-first-result-of-a-query-t-sql) – Tab Alleman Mar 05 '18 at 15:11
  • Is your question trying to ***imply*** that your data is two columns; `(id, value)`? If so, perhaps it would be better to actually show that, with column names? – MatBailie Mar 05 '18 at 15:28
  • The solution in the duplicate question can be used whether you base it on id or value. – Tab Alleman Mar 05 '18 at 15:59

3 Answers3

3

You should union a calculated sort field with a value of -ALL- with your main table and then sort the results by the calculated field.

SELECT
    *
FROM
(
    SELECT
       RowOrder=2,
       RowValue='-ALL-'
    UNION
    SELECT
       RowOrder=1,
       RowValue=FruitTable.Value
    FROM
       FruitTable
)AS X
ORDER BY
   X.RowOrder,
   x.RowValue
Ross Bush
  • 14,648
  • 2
  • 32
  • 55
2

Please try like this.

SELECT [Values] from 
(
    SELECT 'Select All' [Values] UNION ALL
    SELECT 'Apple' UNION ALL
    SELECT 'Banana' UNION ALL
    SELECT 'Mango' 
)u
ORDER BY Case [Values] WHEN 'SELECT All' THEN 1 ELSE 0 END,[Values]
Pawan Kumar
  • 1,991
  • 10
  • 12
2

similar to other answers, but I assume you understand the method:

    ;WITH T (List, Sort) AS 
    (
    SELECT 'Apple' , 1
    UNION ALL
    SELECT 'Banana' ,1 
    UNION ALL 
    SELECT 'Mango' ,1 
    ),
    T2 (List , Sort) AS 
    (
    SELECT * 
    FROM T
    UNION ALL
    SELECT 'Select All'  , 2
    )
    SELECT List 
    FROM T2
    ORDER BY Sort
hkravitz
  • 1,345
  • 1
  • 10
  • 20