简体   繁体   English

是否可以使用SQL将值传递给order by子句?

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

I have strange requirement on sql store procedure. 我对sql存储过程有奇怪的要求。 I have rqst,key and keyType tables. 我有rqst,key和keyType表。 Rqst table columns hold the FK of different master tables like status,requesType and processType. Rqst表列保存不同主表的FK,例如status,requesType和processType。 Key table hold the rqstId as FK in one of the column. 键表将rqstId保留为FK之一。 keyType is master table used in key table. keyType是密钥表中使用的主表。

My dats looks some thing like this 我的爸爸看起来像这样
RqstTable 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 . 我的要求是用户将提供这些输入字段status,requestType, processType and date Also It should be order by key3 from keyTypeTable . 还应该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. 根据我的搜索,我们不能将value传递给order by子句;根据文档,我们不能将expression传递为order by的一部分。 I tired scalr subquery like 我累了scalr子查询

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. 但是结果与out order by子句相同。它没有用。

My expected out would be 我的预期是

KeyValue  rqstId  orchiveId   
9586      10125   c1235a 

I am writting my sql using Oracle for this. 我为此使用Oracle写我的sql。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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