3

I have this query:

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;

It shows me the error: Unknown column 'total_ecuation' in 'field list'

how I can use the alias as a column?

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 回答