SQL: ¿cómo consultar por item_id y last_updated date cuando updated_date es nulo en muchos casos?

I have a simple query which queries by item_id and it obviously works because it is so simple.

What I need to do is change it to also take into account the updated date, and be something similar to this:

 select item_id , item_name from items order by item_id , update_date desc limit 200

Entonces el requisito es:

  • Order by update_date and tie break by item_id.
  • Where update_date is null, just rank it by item_id.

¿Cómo puedo hacer eso?


preguntado el 12 de junio de 12 a las 17:06

have you run that query? Are you not getting the behavior that you want from that? Could you please show how the output of that query differs from what you need, as well as an example of what you are looking for? -

An example would help-- now I'm not sure what you mean by tie break -

You could try swapping update_date and item_id in your order by -

When update_date is null, you want it to be before or after others results? -

1 Respuestas

When your sorting, SQL will sort by the order the fields are listed in the query in succession, so in the below example

select item_id , item_name, update_date, from items order by update_date desc , item_id  desc;

it will order by update_date first, then order those(sorted) results by item_id, both descending.

Respondido el 12 de junio de 12 a las 17:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.