在Case语句中使用Alias

时间:2013-09-27 10:54:24

标签: sql case alias

我想将此案名称为Brutobedrag,但我无法完成。

SELECT     
    Invoices.InvoiceNr AS Factuurnummer, 
    case(Invoices.IsDebit)
        when 0 then -1*(Invoices.NetAmount + Invoices.BTW + COALESCE (Invoices.BPM, 0)) 
        else Invoices.NetAmount + Invoices.BTW + COALESCE (Invoices.BPM, 0)
    end 
from
    Brands INNER JOIN
    Invoices ON Brands.Id = Invoices.Brand INNER JOIN
    SalesPersons ON Invoices.SalesPerson = SalesPersons.Id LEFT OUTER JOIN
    Payments ON Invoices.Id = Payments.InvoiceId

where
    Invoices.InvoiceDate > '2011-12-31'

group by
    Invoices.InvoiceNr,
    Invoices.NetAmount,
    invoices.BTW,
    invoices.BPM,
    Invoices.IsDebit

2 个答案:

答案 0 :(得分:0)

case when Invoices.IsDebit = 0 
     then -1*(Invoices.NetAmount + Invoices.BTW + COALESCE (Invoices.BPM, 0)) 
     else Invoices.NetAmount + Invoices.BTW + COALESCE (Invoices.BPM, 0)
end as Brutobedrag 

答案 1 :(得分:0)

当它'BC'时

选择(CAS''BC'当'BC'然后1'当AC'那么4                                     当'CL'那么3                                    ELSE''END)                                 UNION(选择(案例'BC'当'BC'那么2                                   ELSE''END))                                 UNION(选择(案例'BC'当'BC'那么22                                    ELSE''END))                                  UNION(选择(案例'BC'当'BC'那么24                                   ELSE''END))                                  UNION(选择(案例'BC'当'BC'那么5                                   ELSE''END))

ANS:1 2 五 22 24

完全可以使用单个条件获取多个值