简体   繁体   中英

Using Alias in Case Statement

I want to alias the case as Brutobedrag but i can't get it done.

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

WHEN ITS 'BC'

SELECT ( CASE 'BC' WHEN 'BC' THEN 1 WHEN 'AC' THEN 4 WHEN 'CL' THEN 3 ELSE ' ' END ) UNION (SELECT (CASE 'BC' WHEN 'BC' THEN 2 ELSE ' ' END)) UNION (SELECT (CASE 'BC' WHEN 'BC' THEN 22 ELSE ' ' END)) UNION (SELECT (CASE 'BC' WHEN 'BC' THEN 24 ELSE ' ' END)) UNION (SELECT (CASE 'BC' WHEN 'BC' THEN 5 ELSE ' ' END))

ANS:1 2 5 22 24

IT WILL BE USE FULL TO GET MULTIPLE VALUES WITH SINGLE CONDITION IN CASE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM