|
|
| Author |
Message |
| Phil. |
This post is not being displayed .
|
 Phil. World Chat Champion

Joined: 02 Feb 2002 Karma :     
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| herulach |
This post is not being displayed .
|
 herulach World Chat Champion
Joined: 19 Apr 2010 Karma :  
|
 Posted: 08:27 - 29 May 2010 Post subject: |
 |
|
What Keith said, or, if you need to display them, something like:
| Code: | cast (case
when date > 0 then date
else "SOMETIME IN THE DISTANT FUTURE"
as datetime ) as 'SortingDate' |
then order by your new field.
Depending on your table, it might be faster to do 2 queries, drop the 0s from the first one, then append a second query including only the zeros. ____________________ YBR 125>FZS600 |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Phil. |
This post is not being displayed .
|
 Phil. World Chat Champion

Joined: 02 Feb 2002 Karma :     
|
 Posted: 10:33 - 29 May 2010 Post subject: |
 |
|
Hi
Yes, I do need to pull all the records, including those with empty dates.
To help explain it here's a working query:
| Code: |
SELECT `orders_id` , `due_by_date` FROM orders WHERE `orders_status` = '2' ORDER BY `due_by_date`
|
The actual one is much bigger but this shows the problem. Due dates are set by the user (on a small number of orders). The default for 'due_by_date' field is '0000-00-00 00:00:00'.
I need to pull all the orders as in the query above, order them by due_by_date. The query above pulls about 500 rows, order by due_date desc will produce results like in my OP. This isn't ideal for the user, really they need to be ASC, but that puts all the results with '0000-00-00 00:00:00' first which is even worse.
So instead of:
2010-05-28 00:00:00
2010-05-27 00:00:00
0000-00-00 00:00:00
I need:
2010-05-27 00:00:00
2010-05-28 00:00:00
0000-00-00 00:00:00
Hope I have done a better job of explaining it this time.
Thanks for the suggestion Herulach, I'm not familiar with the syntax though, any chance you could amend the above with your suggestion and I will give it a run, see what impact it has on the speed of the query?
Thanks for the help as always. |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| supZ |
This post is not being displayed .
|
 supZ World Chat Champion

Joined: 03 Feb 2009 Karma :   
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Alexio |
This post is not being displayed .
|
 Alexio World Chat Champion

Joined: 27 Aug 2009 Karma :    
|
 Posted: 16:28 - 29 May 2010 Post subject: |
 |
|
Or if you're feeling really lazy you could always just execute two queries. One where date_time != '0000-00-00 00:00:00' and one where date_time = '0000-00-00 00:00:00'.
Now you have two data sets which you can just process one after the other, still have all of the data you required and everything in the order you wanted too.
Only a small bit of extra overhead performance  ____________________ will never give up his CG. I look at my fuel gauge more as a progress bar than a fuel gauge.
G: With my GSXR I do often effectively use it as a scooter with a clutch in town.
ms51ves3: why does it need 500 miles? Are you teaching it how to be a piston? |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Frost |
This post is not being displayed .
|
 Frost World Chat Champion

Joined: 26 May 2004 Karma :  
|
 Posted: 17:25 - 29 May 2010 Post subject: |
 |
|
Performance isn't everything. Maintainability and clarity is everything. So many times i've seen someone try to do something really complex with a single 50 line quirey, yeah its quick, but i don't have a fucking clue how to change it to incorporate some new change.  |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Phil. |
This post is not being displayed .
|
 Phil. World Chat Champion

Joined: 02 Feb 2002 Karma :     
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| herulach |
This post is not being displayed .
|
 herulach World Chat Champion
Joined: 19 Apr 2010 Karma :  
|
 Posted: 17:48 - 30 May 2010 Post subject: |
 |
|
| supZ wrote: | | herulach wrote: | What Keith said, or, if you need to display them, something like:
| Code: | cast (case
when date > 0 then date
else "SOMETIME IN THE DISTANT FUTURE"
as datetime ) as 'SortingDate' |
then order by your new field.
Depending on your table, it might be faster to do 2 queries, drop the 0s from the first one, then append a second query including only the zeros. |
i dont believe that would work.
at least in mssql it would whinge you're trying to convert a date to a string (in other words you're trying to order a date and string field type)
if it were me i think id add a silly date to it (like 01/01/4000) then use your code (im presuming you're displaying this in a webpage using php) to change it.
e.g.
| Code: | select name, (case when duedate = '19000101' then '40000101' else duedate end) as 'showdate'
from testable
order by showdate |
|
Thats what I meant except I couldn't remember the proper date syntax. Date >0 should work though, if you just compare a date to a number it does it unformatted.
OP - Keith's code I've quoted above will work fine, and should be reasonably easy to understand afterward - just stick a comment if you're worried about someone else understanding it.
At the risk of sounding flippant, the best way to sort it is to require an input into the Due By date that isn't 1900. A test for the date being in the future at the time of input should be sufficient.
Depending on how your frontend will handle DB errors:
| Code: | ALTER TABLE orders
ADD CHECK (due_by_date>NOW()) |
should work, but if you've no error handling it might balls it up.[/code] ____________________ YBR 125>FZS600 |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| supZ |
This post is not being displayed .
|
 supZ World Chat Champion

Joined: 03 Feb 2009 Karma :   
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Alexio |
This post is not being displayed .
|
 Alexio World Chat Champion

Joined: 27 Aug 2009 Karma :    
|
 Posted: 04:15 - 31 May 2010 Post subject: |
 |
|
https://i8.photobucket.com/albums/a27/alsweetex/forum/mysqrl.jpg
Yeah, I guess there was no really simple way to do it in one query, but there are definitely ways it can be done. I'm sure you could just include some extra documentation / comments. ____________________ will never give up his CG. I look at my fuel gauge more as a progress bar than a fuel gauge.
G: With my GSXR I do often effectively use it as a scooter with a clutch in town.
ms51ves3: why does it need 500 miles? Are you teaching it how to be a piston? |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
 Posted: 13:00 - 01 Jun 2010 Post subject: |
 |
|
Hi
Most solutions are going to be lousy on performance as you will land up sorting on a derived field. Reading it into php and then sorting will also be lousy.
You could try this:-
| Code: |
(
SELECT *
FROM `DateSortTest`
WHERE `SomeDate` >0
ORDER BY SomeDate
)
UNION (
SELECT *
FROM `DateSortTest`
WHERE `SomeDate` =0
)
|
but not convinced that MySQL will maintain the sort order always.
Personally I would be tempted to have a flag to say that the date field has meaningful data in it for that row. Say "PlannedEvent" as a boolean:-
| Code: |
SELECT *
FROM `DateSortTest`
ORDER BY PlannedEvent, SomeDate
|
Not strictly good practice (as you are effectively duplicating data) but in this case I wouldn't worry. And you can always use a trigger to force update the flag field is the date field changes.
All the best
Keith ____________________ Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| supZ |
This post is not being displayed .
|
 supZ World Chat Champion

Joined: 03 Feb 2009 Karma :   
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
Old Thread Alert!
The last post was made 16 years, 101 days ago. Instead of replying here, would creating a new thread be more useful? |
 |
|
|