[sqlalchemy] Re: how can I do such a sorted query?

2007-11-08 Thread Rick Morrison
Most database engines support a couple of  SQL functions that help in cases
like this, read your database docs for either the ISNULL or the COALESCE
function.

Another technique is to use an SQL CASE statement.

For all three methods the idea is to supply a default value to substitute
when the value in question is NULL.

In your case that substituted value would be the value of the 'f_date'
column, so in essence the logic would be use the value of 's_date'
if it's NOT NULL, otherwise use the value of 'f_date'.

SA has constructs for both the SQL function method and the CASE statemen
method; all are documented (well, I think the CASE is)

Rick


On 11/8/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:


 a table, say like this:
 name  f_dates_dtae
 
 name12007-10-01   null
 name22007-06-03   2007-10-07
 name32007-09-20   null
 name4...   ...

 the 'f_date' column always contains a value but the 's_date' can
 sometimes be null.
 now I want to get a collection -- all items will be in it and if the
 's_date' is not null it must
 be ordered with 'f_date' of those the 's_date' is null. Just like
 this:

 name   f_dates_date
 ---
 name3 2007-09-20 null
 name1 2007-10-01 null
 name2 2007-06-03 2007-10-07

 any idea? use select querymaybe with 'UNION'?
 thanks in advance!

 artman


 


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: how can I do such a sorted query?

2007-11-08 Thread artman

Thanks for your help,Rick.
Followed your advice, I've solved the problem. Following is the
solution:

select([table_name]).order_by(case([(table_name.c.s_date==None,table_name.c.f_date)],else_=table_name.c.s_date))

Thanks again :)



On 11月9日, 上午1时48分, Rick Morrison [EMAIL PROTECTED] wrote:
 Most database engines support a couple of  SQL functions that help in cases
 like this, read your database docs for either the ISNULL or the COALESCE
 function.

 Another technique is to use an SQL CASE statement.

 For all three methods the idea is to supply a default value to substitute
 when the value in question is NULL.

 In your case that substituted value would be the value of the 'f_date'
 column, so in essence the logic would be use the value of 's_date'
 if it's NOT NULL, otherwise use the value of 'f_date'.

 SA has constructs for both the SQL function method and the CASE statemen
 method; all are documented (well, I think the CASE is)

 Rick

 On 11/8/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:





  a table, say like this:
  name  f_dates_dtae
  
  name12007-10-01   null
  name22007-06-03   2007-10-07
  name32007-09-20   null
  name4...   ...

  the 'f_date' column always contains a value but the 's_date' can
  sometimes be null.
  now I want to get a collection -- all items will be in it and if the
  's_date' is not null it must
  be ordered with 'f_date' of those the 's_date' is null. Just like
  this:

  name   f_dates_date
  ---
  name3 2007-09-20 null
  name1 2007-10-01 null
  name2 2007-06-03 2007-10-07

  any idea? use select querymaybe with 'UNION'?
  thanks in advance!

  artman- 隐藏被引用文字 -

 - 显示引用的文字 -


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---