1

I am trying to sort my sql result with two columns but its not working for second column which is date.

I know may be there some solution posted before but I am not able to find that.

My query:

SELECT 
    order_id,order_name, delivery_date, created_at 
FROM 
    orders 
WHERE 
    store_client_id = '1192' 
    AND delivery_date != '' 
ORDER BY 
    order_id DESC, 
    CAST('delivery_date' AS DATE) DESC 
LIMIT 0,15

And result I am getting:

enter image description here

Order id is being sorted properly and date is not working.

Any help in this regard is highly appreciated in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Deepak Lakhara
  • 323
  • 2
  • 9
  • First of all you need to get rid of quotes wrapping `delivery_date` in `CAST('delivery_date' AS DATE)`, and why do you need casting to date, isn't already that column is of date type ? – Barbaros Özhan Aug 23 '19 at 06:36
  • This is how Order by 2 columns work It will first sort by first coloumn, then if 2 or more rows have same data for 1st coloumn only then those rows which are having same data for first coloum will be sorted based on 2nd coloum. So I dont see a problem in the output and it seems to be working as expected. Let me know if I missed anything from the question – Sameer Aug 23 '19 at 06:43
  • @ Barbaros Özhan Okay, If I try without cast then too it is not working. – Deepak Lakhara Aug 23 '19 at 07:07
  • @Sameer It is not sorting the dates. That is the problem. – Deepak Lakhara Aug 23 '19 at 07:09
  • 1
    remove quotes from delivery_date.your query first sort rows based on order_id.If more then one rows have same order_id then it sort based on date.I don't see any problem here. if you want to sort based on dates then swap order of delivery_date and order_id. – Primit Aug 23 '19 at 07:22
  • @PKP I have tried both but the result is none. same as attached screenshot. When I switch first delivery_date first and then order id it works but not in reverse order. – Deepak Lakhara Aug 23 '19 at 07:40
  • is there any rows which have same order_id? – Primit Aug 23 '19 at 07:45
  • @PKP No, You can see also in my screenshot attached. – Deepak Lakhara Aug 23 '19 at 07:55

0 Answers0