Question

I have a LineItem table.

InvoiceNo   ApplyTax(bool)   Subtotal  
1              1                10
1              0                10  
1              1                10 
2              0                10
2              1                10

Apply Tax means adding another 7% to subtotal

I need a select statement that returns.

InvoiceNo     Total
1              31.4 ----(10* 1.07 + 10 + 10*1.07)
2              20.7 ----(  10* 1.07 + 10) 

I could come up with this but its giving syntax error.

SELECT InvoiceNo ,SUM(( select case(ApplyTax) WHEN 1 THEN 1.07 ELSE 1)*subtotal) 
From InvoiceTable
Group By InvoiceNo 

Am i missing something really simple? I can't get it right.

Était-ce utile?

La solution

Here is the proper syntax:

SELECT InvoiceNo, SUM((case ApplyTax WHEN 1 THEN 1.07 ELSE 1 end)*subtotal) 
From InvoiceTable
Group By InvoiceNo ;

Notice there is no nested select statement and the case ends with end.

Autres conseils

Put END at the end of your CASE statement..

SELECT inv ,SUM(( select case(ApplyTax) WHEN 1 THEN 1.07 ELSE 1 END)*subtotal) as total
From test 
Group By inv 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top