0

I have a table tag with 2 fields ID, Text. I'd like to make a query with distinct value of Text and add 'All' On first position (it's used in a SSRS parameter value) I made the following query:

SELECT 'All' [Text] Union
Select DISTINCT [text] from tag

the problem is there are some text starting with number and came before the All Text in the query. How can I make the query in order to get All in the first Row

2
  • 1
    Sample data would be helpful Commented Dec 15, 2016 at 18:52
  • Let say there are Rows like the following: 1, 1w 2, SS 3, TT Commented Dec 15, 2016 at 18:55

2 Answers 2

2
SELECT 'All' AS [Text], 0 AS Sorter
UNION
SELECT [text], 1 AS Sorter
FROM tag
ORDER BY 2,1

If you just want the text, try:

WITH alltext AS (
SELECT 'All' AS [Text], 0 AS Sorter
UNION
SELECT [text], 1 AS Sorter
FROM tag
)
SELECT text FROM alltext ORDER BY sorter, text

Hope that helps.

Sign up to request clarification or add additional context in comments.

1 Comment

i removed distinct which isn't necessary when using union. also the order by in the first query had to be order by 2,1
0

You can use union all to prevent ordering:

SELECT 'All' [Text] UNION ALL
(Select DISTINCT [text] from tag order by [text])

2 Comments

I don't see why ALL would fix this problem. I'd like to have 'All' In the first Row, but in the table I have some value like 1, 2 etc,and the query is ordering by value and they're coming before the 'All' Value.
amended my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.