MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old May 6th, 2008, 01:38 PM
winddancer winddancer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: Germany
Posts: 201 winddancer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 14 m 20 sec
Reputation Power: 6
Send a message via ICQ to winddancer
Order by with two date fields - possible with mysql?

Hi all,

this might probably a dumb question, so please bare with me.

I am doing a SELECT on a table which has two date fields. One for the create date and one for the startdate.

I output my results in chronological order with:

...ORDER BY startdate DESC, crdate DESC ...

Startdate is not required field so it might also be empty. The result might be incorrect because of this.

What I want to do is: use the latest date for each dataset Startdate or Createdate.

example:

dataset 1
created: 01-01-2008
startdate:
-> Should use created.

dataset 2
created: 03-01-2008
startdate: 02-02-2008
-> Should use startdate.

dataset 3
created: 14-01-2008
startdate: 16-01-2008
-> Should use startdate.

List should look like this:

1. dataset 2
2. dataset 3
3. dataset 1

I don't know however how I can do this with mysql. I hope I could make myself clear!

Any hint is greatly apreciated!

Thanks and kind regards

Sven

Reply With Quote
  #2  
Old May 6th, 2008, 04:03 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,727 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 19 h 22 m 42 sec
Reputation Power: 848
ORDER BY GREATEST(startdate,COALESCE(startdate,crdate)) DESC
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old May 6th, 2008, 04:35 PM
winddancer winddancer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: Germany
Posts: 201 winddancer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 14 m 20 sec
Reputation Power: 6
Send a message via ICQ to winddancer
Hi there,

thanks for the reply!

Unfortunately this didn't change a thing. I read through the description of COALESCE option and it sounds like it's what I need.

I have a very simple query like this:

SELECT * FROM db_jpp ORDER BY GREATEST(starttime,COALESCE(starttime,crdate)) DESC

However jobs with no starttime (value of the field is 0) are being listed at the very end. The rest is ordered correctly.

I also tried something like this:

SELECT * FROM db_jpp ORDER BY COALESCE(starttime,crdate) DESC

also no change :/

btw. it's linux timestamps.

any idead why this might happen!?

Kind regards

Sven

Reply With Quote
  #4  
Old May 6th, 2008, 04:40 PM
winddancer winddancer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: Germany
Posts: 201 winddancer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 14 m 20 sec
Reputation Power: 6
Send a message via ICQ to winddancer
OMG! my fault! I should have read more carefully.

COALESCE returns the first NONE NULL!!! value. which would be fitting for 0.

using only GREATEST is working like a charm!

thanks for the help!

Kind regards

Sven

Reply With Quote
  #5  
Old May 6th, 2008, 05:27 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,727 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 19 h 22 m 42 sec
Reputation Power: 848
so when you said "Startdate is not required field so it might also be empty" what did you mean by "empty"?

obviously if COALESCE didn't do the job for you, then the "empty" values weren't NULL, they were something else????

Reply With Quote
  #6  
Old May 6th, 2008, 06:52 PM
winddancer winddancer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: Germany
Posts: 201 winddancer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 14 m 20 sec
Reputation Power: 6
Send a message via ICQ to winddancer
Quote:
Originally Posted by r937
so when you said "Startdate is not required field so it might also be empty" what did you mean by "empty"?

obviously if COALESCE didn't do the job for you, then the "empty" values weren't NULL, they were something else????


yes, sorry that was my fault. The fields aren't actually empty.

They have a linux timestamp or they are default '0'. So not empty.

Sorry.

Reply With Quote
  #7  
Old May 6th, 2008, 06:58 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,727 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 19 h 22 m 42 sec
Reputation Power: 848
don't worry, i am just glad you got your solution


Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Order by with two date fields - possible with mysql?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway