2017-07-17 11 views
0

どのパラメータが選択されているかによって、異なる列を使用して並べ替えることができます。 私はそれを単一の列に対しては機能させることができますが、複数で苦労しています。 ご協力いただければ幸いです。条件と複数のソート列を含むcase文で並べ替える

ORDER BY 
CASE WHEN @a like '%apples%' or @a like '%onions%' then 
DeliveryScheduleItems.DropSequence, 
DeliveryAddresses.PostalCode, 
Customers.CustomerId 
Else 
DeliverySchedules.DeliveryDate, 
DeliveryAddresses.PostalCode, 
Customers.CustomerId 
End 

答えて

0

ORDER BY 
CASE 
WHEN @a like '%apples%' or @a like '%onions%' then 
DeliveryScheduleItems.DropSequence, 
DeliveryAddresses.PostalCode, 
Customers.CustomerId 
WHEN @a not like '%apples%' AND @a not like '%onions%' then 
DeliverySchedules.DeliveryDate, 
DeliveryAddresses.PostalCode, 
Customers.CustomerId 
End 
+0

ご回答いただきありがとうございます。これは単一の並べ替え列では有効ですが、複数ではできません。 '、'の近くの構文が正しくありません – jaybee

0

あなたは複数の条件を必要とし、これを試してみてください:

ORDER BY (CASE WHEN @a like '%apples%' or @a like '%onions%' 
       THEN DeliveryScheduleItems.DropSequence 
      END), 
     (CASE WHEN @a like '%apples%' or @a like '%onions%' 
       THEN NULL 
       ELSE DeliverySchedules.DeliveryDate 
      END), 
     DeliveryAddresses.PostalCode, 
     Customers.CustomerId 

は、二つの異なる列には2つのCASE表現の使用を注意してください。おそらくDeliveryDateDropSequenceには異なる種類があります。それらを別々に注文する方がいいです。

関連する問題