0

I am having a table with data like:

ID  OrderId TAX%    Quantity UnitCost
1     5      28    2       1280
2     5      18    1       1180

I want the output like:

ORDERID    TaxableValue       TaxValue
   5           3000               740

How can I achieve this? I tried with:

SELECT orderid,
       ROUND((SUM(UnitCost*Quantity)*TAX)/(100+TAX),2) AS taxValue,
       ROUND(SUM(unitCost*quantity)-ROUND((SUM(UnitCost*Quantity)*TAX)/(100+TAX),2),2) AS taxableValue 
FROM table1 
GROUP BY OrderId;

But the query above is not working.

Sorry all, That is the Tax percentage. This is the new Updated Query. Please consider this.

2
  • 6
    What error do you get? Commented Nov 14, 2017 at 15:10
  • i am getting data like Order Id -> 5, tax Value->81.81, taxable value->292.19 Commented Nov 14, 2017 at 15:32

3 Answers 3

2

Pretty sure it is as simple as doing a little math.

select OrderID
    , TaxableValue = SUM(UnitCost - TAX)
    , TaxValue = SUM(TAX * Quantity)
from YourTable
group by OrderID

EDIT: The math was slightly off on the TaxableValue

For those who want proof that this works.

declare @Something table
(
    ID int
    , OrderId int
    , TAX int
    , Quantity int
    , UnitCost int
)

insert @Something values
(1, 5, 28, 2, 228)
, (2, 5, 18, 1, 118)

select OrderID
    , TaxableValue = SUM(UnitCost - TAX)
    , TaxValue = SUM(TAX * Quantity)
from @Something
group by OrderID
Sign up to request clarification or add additional context in comments.

19 Comments

You bet brother.
I'm doubt about your output with OP's expected result
@SeanLange It's nice, you got it
@SaiRam again....the query posted in this answer is EXACTLY what you stated you want as output. If it is not correct then you need to update your question and explain what you want.
Then can you update your desired output based on this?
|
0

This is how it can be done.

SELECT 
orderid, 
sum(unitcost*quantity) - sum(tax*quantity) as taxablevalue,
sum(tax*quantity) as taxvalue
FROM table1  
GROUP BY OrderId;

Comments

0
select OrderId
    , SUM((unitcost * quantity * tax)/(100+tax)) as TaxValue
    , SUM(unitcost * quantity)-SUM((unitcost * quantity * tax)/(100+tax)) as taxableValue
from table1
group by OrderId

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.