Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to sort a return SQL query

Author  Topic 

CG_dude
Starting Member

3 Posts

Posted - 2009-05-28 : 22:58:11
Hi All, SQL is very new to me, the perl script I'm running the SQL query in, I did not write. However, everything works fine, but now I'm using and changing the script for another report, and I have everything I need and the query works, except, I need it to sort with what I tell it too, with the values in the array I give. Example

My select statement is quering the end time of batch jobs ( A, B, C, D,). I need it to print out in this order, ( C, B, A, D). Belwo is what my query looks like;

"select job.job_name, job_status.last_start, job_status.last_end from job, job_status
where job.joid = job_status.joid and job.job_name in
('JOB_A',
'JOB_B',
'JOB_C',
'JOB_D')
order by job.job_name");

while(@status = &dbnextrow($dbproc)){
$name = ($status[0]);
$stime = &GETTIME($status[1]);
$etime = &GETTIME($status[2]);
$spacer = "";

print "$title </br>";
print "$name, $etime<br/>";
print "$spacer</br>";
print "$spacer</br>";

Again, not sure how to do this, This is quering from SYBASE

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-28 : 23:17:42
you can use CASE WHEN . .

order by case when job_name = 'JOB_C' then 1
when job_name = 'JOB_B' then 2
when job_name = 'JOB_A' then 3
when job_name = 'JOB_D' then 4
end


by the way, this is a Microsoft SQL Server forum. Though it shared the basic T-SQL with Sybase but both are not the same. You will get better response in the Sybase forum


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

CG_dude
Starting Member

3 Posts

Posted - 2009-05-29 : 00:03:08
Thanks very much khtan, it worked like a charm.
Go to Top of Page
   

- Advertisement -