jeudi 29 janvier 2015

Multiple cases and order_by at the same query in sql sentence?

I have a quite complex search for SQL and after couple of attempts trying to use multiple cases for the query, I wonder if it is posible to do all in one query or if I need to split the task.


Table invoices includes, among others, customer_id, customer_version, startDate (DateTime) and status.


This is the goal:


From invoices, when customer_id='123' and customer_version="321" select invoice according to status.


if there are one or more past_due invoices, pick the older one.


if there is no past_due invoice, pick the first open invoice,


if there is no open invoice, pick the last paid invoice,


if there is no paid invoice, pick the future invoice,


if there is no future invoice, pick the last draft,


if there is no draft, then retrieve a null value for all invoice.


Is it possible to query with cases, using different startDate orders (ASC, DESC) checking for nulls and picking by status, at the same SELECT sql phrase? Can some one give an example?


Aucun commentaire:

Enregistrer un commentaire