Sorting in query - Leave last registration in first and then sort by a field

Asked

Viewed 1,449 times

2

I’m having to create a query that returns the data ordered by a specific field, for example name, but the first return line should be the last id. You can do this with sql?

Let’s assume I have the user table:

id    nome
1     João
2     Marcelo
3     André
4     Vanessa

The return must be:

id    nome
4     Vanessa
3     André
1     João
2     Marcelo

If anyone has any ideas for that.

Follows a Sqlfiddle

  • 1

    @Marcelodiniz this is the explanation I put in my answer: http://social.technet.microsoft.com/wiki/contents/articles/4436.union-x-union-all-pt-br.aspx

5 answers

6


Possible solution:

SELECT
   id,nome
FROM
   usuario
JOIN
   (SELECT MAX(id) AS ultimo FROM usuario) AS dummy
ORDER BY id != ultimo, nome

Explanation:

  1. The subquery (SELECT MAX(id) AS ultimo FROM minha.Base.minhaTabela) AS dummy causes the id major is returned to all rows under the column name ultimo.

  2. The ORDER BY first orders by id other than id maior, that is to say, false to the greatest id, what makes this the first line of the query.

  3. excluding the special case of id, the ordination is made by the name.

See working on SQL Fiddle

2

An auxiliary table can be used for this scenario:

CREATE TABLE #temp (id int, nome nvarchar(max))
INSERT INTO #temp (id, nome)
    SELECT id, nome from tabela where id = (select max(id) from tabela)
INSERT INTO #temp
    SELECT id, nome from tabela where id <> (select max(id) from tabela) order by nome
SELECT id, nome FROM #temp
DROP TABLE #temp
  • I just don’t know if it’s the best option, in which case, using an auxiliary table, wouldn’t it consume a lot more server resources than the other forms that were put? Not that its not solved, hence the question is server consumption and response time. Valew

  • 2

    Until seeing the @Bacco response I didn’t know if it would be possible to do without an auxiliary table. His is definitely the best (which is why I voted for her). But as technically this answer also solves the problem, I decided to leave it here instead of removing it in case someone else finds a scenario where an extra table is needed.

2

SELECT usr.* 
FROM   usuario AS usr, 
       (SELECT Max(id) AS maxid 
        FROM   usuario) AS src 
ORDER  BY src.maxid != usr.id, 
          usr.nome 

Explanation:

First we define in a subquery which is the largest ID, and make it available for consultation in the main query.

Then we use a comparison directly in the clause ORDER BY.

Upshot:

| ID |    NOME |
|----|---------|
|  4 | Vanessa |
|  3 |   André |
|  1 |    João |
|  2 | Marcelo |
  • 3

    Here it worked smoothly, and in this case, difference of what @Maria passed I understood the pq did not return the last record twice. I thank you

0

just use something like this order by id desc

only complementing for those who failed to understand

SELECT *
FROM teste
ORDER BY id DESC, nome ASC
  • It’s not that simple! I don’t know if you understand, but I have to return the data sorted by name, but the first record returned should be the last one inserted in the table.

  • simple ORDER BY id DESC, ASC name

  • I understood what you put, but unfortunately not quite that way. But still I thank you for the help

  • opa tranquil I hope you have managed to solve

0

Thus:

The first Select takes the information of the last record, while the second makes the total ordering by the name field, when joining the tables it already does what the question expects the union of the two SQL, even eliminating the code that repeats! Why don’t you repeat with a union he makes a distinct for no repetition, second microsoft link, is very well explained.

SELECT id, nome FROM (
      SELECT id, nome FROM testdb.tabela30 ORDER BY id DESC LIMIT 1
) AS tabelaidmax
union
SELECT id, nome from (SELECT id, nome FROM testdb.tabela30 ORDER BY nome asc
) as tabelanomeasc
  • @Otto Unfortunately your answer is not right, take a look at the sqlFiddle I put in the question. And Maria if you can represent a sqlFiddle and command, that I can not represent, but still I appreciate the help of all.

  • Regarding the Sqlfiddle mine is giving error is saying that this with technical problems you know other

  • 1

    And with this example I worked yes. Simple as I understood, the first query returns only the last record and in the second ordered by name. Now I didn’t understand why I didn’t bring the last record twice.

  • 1

    @Marcelodiniz, good question, when you put only UNION it has the job of removing the repeated ones if you for example put UNION ALL it will bring even the repeated.

  • 1

    I really didn’t remember this from UNION . I had read it, but I didn’t really remember, I thank you for the answers and the research.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.