1

テーブルから列を取得したい。条件に応じて、何を使用できるか。

たとえば、注文コメントの追加、注文コメントのキャンセル、注文コメントの延期、アクション(追加、キャンセル、延期)、受信金額(Y / N)のフィールドがあります。

次に、列をフェッチして注文コメントを追加し、注文コメントをキャンセルし、アクションと受け取った金額に応じて注文コメントを延期する必要があります。

if(action='add' and amount received='Y')
then
i've to fetch add order comments column
elseif(action='postpone' and amount received='Y')
then
i've to fetch postpone order comments column
else (action='cancel')
then i've to fetch cancel order comments

sqlまたはplsqlでこれを実行する方法。selectステートメントでこの条件が必要です。

4

3 に答える 3

3

「sqlまたはplsql」では、「sql」はMSSQLServerで使用されるT-SQLを指していると想定していることに注意してください。そうでない場合は、お使いの言語で適切な同等のものを使用してください。

CASE(T-SQL)ステートメントを使用する必要があります( PL-SQLと同等

たとえば、T-SQLでは次のようになります。

SELECT OrderId AS OrderId
       CASE 
           WHEN Action = 'add' AND amountRcd = 'Y' THEN addOrderComment
           WHEN Action = 'postpont' AND amountRcd = 'Y' THEN postponeOrderComment
           WHEN Action = 'cancel' THEN cancelOrderComment 
           ELSE 'Unrecognised action'
       END AS Comment
FROM tblOrders

また、指定したルールで、amountRcdフィールドがでない場合Yは、コメントとして「認識されないアクション」が表示されることにも注意してください。これを防ぐためにルールを明確にする必要があると思います。

于 2012-11-15T11:55:24.067 に答える
0

私があなたの問題を正しく理解している場合、これを達成する別の方法は、3つの別々のクエリを実行し、それらを結合することです。

select orderID as OrderID, addOrderComments as Comment
from tblOrders
where Action = 'add' AND amountRcd = 'Y'
union 
select orderID as OrderID, postponeOrderComment as Comment
from tblOrders
where Action = 'postpone' AND amountRcd = 'Y'
union
select orderID as OrderID, cancelOrderComment as Comment
from tblOrders
where Action = 'cancel'
于 2012-11-15T12:03:39.777 に答える
0

これを試して

select order comment case when action ='add' 
     and amount received ='y'
  else select postpone order comments when action ='postpone'
    and amount received='y'
  else select cancel when action ='cancel' end 
  from table
于 2012-11-15T11:55:57.003 に答える