简体   繁体   中英

Is it possible to pass a value to an order by clause using SQL?

I have strange requirement on sql store procedure. I have rqst,key and keyType tables. Rqst table columns hold the FK of different master tables like status,requesType and processType. Key table hold the rqstId as FK in one of the column. keyType is master table used in key table.

My dats looks some thing like this
RqstTable

rqstId appId statsId procesTypId requestType updtBy createDt  orchiveId
10125   3       102     5           4          Test    date    c1235a

keyTable

keyId rqstId keyTypeId KeyValue  
123   10125  2          9586  

KeyTypeTable

KeyTypeId KeyTypeName Description     
1           key1        key1des    
2           key2        key2des

My requirement is user will provide these Input fields status,requestType, processType and date . Also It should be order by key3 from keyTypeTable .

As per my searching we can not pass value to column in order by clause and As per documentation we can pass expression as part of order by. I tired scalr subquery like

select * from rqst a,
left join key b on b.rqstId = a.rqstId
left join KeyType c on c.keyTypeId = b.KeyTypeId
where a.procesTypId = 5 and a.requestType = 4 and a.statsId = 102
order by KeyTypeName; -- but want some thing like KeyTypeName='key2'

But the result is same as with out order by clause.it did not work.

My expected out would be

KeyValue  rqstId  orchiveId   
9586      10125   c1235a 

I am writting my sql using Oracle for this.

can any one suggest me or is it possible to have such kind of order by in my requirement.

try this:

select b.KeyValue,
       a.rqstId,
       a.orchiveId,
       case when c.KeyTypeName = 'key2' then 0 else 1 end as KeyTypeNameForOrder
  from rqst a, key b, keyType c
 where a.statusId = 102
   and a.requestType = 4
   and a.processTypeId = 5
   and b.rqstId = a.rqstId     
   and c.KeyTypeId = b.keyTypeId
 order by 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM