3

私はこのクエリを持っています:

select qa_returns_items.item_code, 
               (CASE status_code
                 WHEN 11 THEN (qa_returns_items.item_quantity - qa_returns_residues.item_quantity) 
                 WHEN 12 THEN (qa_returns_items.item_quantity + qa_returns_residues.item_quantity)   END) as total_ecuation , 
                qa_returns_residues.item_unitprice,
               ( qa_returns_residues.item_unitprice * total_ecuation) as item_subtotal,
               (qa_returns_residues.item_discount * item_quantity) as item_discount,
               ( ( qa_returns_residues.item_unitprice * total_ecuation) -   
                 (qa_returns_residues.item_discount * item_quantity) ) as item_total
from qa_returns_residues, qa_returns_items
where
    total_ecuation > 0 
AND qa_returns_items.item_code = qa_returns_residues.item_code;

エラーが表示されます:「フィールドリスト」の不明な列「total_ecuation」

エイリアスを列として使用するにはどうすればよいですか?

4

2 に答える 2

3

サブクエリの使用を検討してください。クエリを読みやすくするために、テーブルエイリアスを追加しました。

select  *
,       item_unitprice * total_ecuation as item_subtotal
,       (item_unitprice * total_ecuation) - item_discount as item_total
from    (
        select  ri.item_code
        ,       case status_code
                when 11 then ri.item_quantity - rr.item_quantity 
                when 12 then ri.item_quantity + rr.item_quantity
                end as total_ecuation
        ,       rr.item_unitprice
        ,       rr.item_quantity
        ,       rr.item_discount * rr.item_quantity as item_discount
        from    qa_returns_residues rr
        join    qa_returns_items ri
        on      ri.item_code = rr.item_code
        ) as SubQueryAlias
where   total_ecuation > 0
于 2012-06-24T17:48:08.017 に答える
2

名前付きサブクエリを使用するようにクエリを書き直すか、where句でcaseステートメント全体を繰り返す必要があります...

于 2012-06-24T17:21:00.020 に答える