Question

I have this query:

SELECT 
     * 
FROM 
     PENDIENTESCOBRO 
ORDER BY 
     NPENDIENTE ASC
     , DFECHA

NPENDIENTE can be a negative or positive number. I want that when this field is higher than 0, stops using this order and takes ORDER BY DFECHA.

How can I do this?

Was it helpful?

Solution

This should do it:

SELECT *
FROM PENDIENTESCOBRO
ORDER BY
  CASE WHEN NPENDIENTE>0 THEN 1 ELSE NPENDIENTE END ASC,
  DFECHA

This is because you can use CASE espressions in SQLite ORDER BY clauses. The CASE I put makes all positive (>0) NPENDIENTE values have the same order value... so rows are then ordered by DFECHA.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top