?
Solved

Make Field Negative Based On Data In Another Field

Posted on 2009-04-23
3
Medium Priority
?
229 Views
Last Modified: 2012-05-06
I have a query that will return rows with an sales order type and then the tax amount for the sales order.  If the order type was an invoice then I want the tax amount to not change.  However, if the order type is a return I want the tax amount to be negative.  I can do this in my actual report, but I would prefer to do it in the underlying query.

So in the code below:
   if a.soptype='4' then b.txdtottx * -1

Any ideas?
select a.soptype as SOPType, a.sopnumbe as OrderNumber, a.docdate as DocumentDate, a.custnmbr as CustomerNumber, a.custname as CustomerName, a.locncode as Warehouse, a.pymtrmid as Terms, a.city as City, a.state as State, a.shipmthd as ShippingMethod, a.taxschid as TaxScheduleID, a.voidstts as VoidStatus, b.taxdtlid as TaxDetailID, b.txdtottx as TotalTax from
(select soptype, sopnumbe, docdate, custnmbr, custname, locncode, pymtrmid, city, state, shipmthd, taxschid, voidstts from sop30200
) A
left join
(select sopnumbe, lnitmseq, taxdtlid, txdtottx from sop10105) B
on a.sopnumbe = b.sopnumbe
where (MONTH(docdate)= MONTH(GetDate()) AND (YEAR(docdate)= YEAR(GetDate()))) and ((a.soptype = '3' or a.soptype = '4') and a.voidstts<>1) and b.lnitmseq='0'
order by b.taxdtlid, a.docdate

Open in new window

0
Comment
Question by:r270ba
3 Comments
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 1600 total points
ID: 24219835
Try...

case when a.soptype='4' then -1 else 1 end * b.txdtottx
select a.soptype as SOPType, a.sopnumbe as OrderNumber, a.docdate as DocumentDate, a.custnmbr as CustomerNumber, a.custname as CustomerName, a.locncode as Warehouse, a.pymtrmid as Terms, a.city as City, a.state as State, a.shipmthd as ShippingMethod, a.taxschid as TaxScheduleID, a.voidstts as VoidStatus, b.taxdtlid as TaxDetailID, case when a.soptype='4' then -1 else 1 end * b.txdtottx as TotalTax from
(select soptype, sopnumbe, docdate, custnmbr, custname, locncode, pymtrmid, city, state, shipmthd, taxschid, voidstts from sop30200
) A
left join
(select sopnumbe, lnitmseq, taxdtlid, txdtottx from sop10105) B
on a.sopnumbe = b.sopnumbe
where (MONTH(docdate)= MONTH(GetDate()) AND (YEAR(docdate)= YEAR(GetDate()))) and ((a.soptype = '3' or a.soptype = '4') and a.voidstts<>1) and b.lnitmseq='0'
order by b.taxdtlid, a.docdate

Open in new window

0
 
LVL 8

Assisted Solution

by:Hadush
Hadush earned 400 total points
ID: 24219870
You can have a case statment on the query
Case when a.soptype='4' then b.txdtottx*-1
else b.txdtottx end TotalTax  

select a.soptype as SOPType
, a.sopnumbe as OrderNumber
, a.docdate as DocumentDate
, a.custnmbr as CustomerNumber
, a.custname as CustomerName
, a.locncode as Warehouse
, a.pymtrmid as Terms
, a.city as City
, a.state as State
, a.shipmthd as ShippingMethod
, a.taxschid as TaxScheduleID
, a.voidstts as VoidStatus
, b.taxdtlid as TaxDetailID
 
,Case when a.soptype='4' then b.txdtottx*-1
    else b.txdtottx end TotalTax 
from (select soptype
, sopnumbe
, docdate
, custnmbr
, custname
, locncode
, pymtrmid
, city
, state
, shipmthd
, taxschid
, voidstts 
from sop30200 ) A 
left join (select sopnumbe
       , lnitmseq
       , taxdtlid
       , txdtottx 
       from sop10105) B 
       on a.sopnumbe = b.sopnumbe 
       where (MONTH(docdate)= MONTH(GetDate()) 
       AND (YEAR(docdate)= YEAR(GetDate()))) 
       and ((a.soptype = '3' or a.soptype = '4') 
       and a.voidstts<>1) and b.lnitmseq='0' order by b.taxdtlid, a.docdate 

Open in new window

0
 

Author Closing Comment

by:r270ba
ID: 31574003
I have accepted both solutions.  I gave Brandon 400 points and Hadush 100 points.  I used Brandon's solutions b/c it worked and he was the first.  Hadush's solution, even though I did not try it, looks like it would work as well.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question