Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL calculation

Posted on 2012-03-27
11
Medium Priority
?
292 Views
Last Modified: 2012-03-27
Hi Experts,

Ok to keep it simple lets assume I have 2 columns:
QTY
TRANSTYPE

Im creating a view so bare in mind this is part of my SELECT clause. I need...

IF (QTY <> 0 AND TRANSTYPE='ADJ' then QTY = QTY * -1)

I dont think I can do this in a CASE statement?

Thanks,
Dean.
0
Comment
Question by:deanlee17
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 37771480
SELECT
    ...,
    CASE WHEN (QTY <> 0 AND TRANSTYPE='ADJ') then QTY * -1 ELSE QTY END AS QTY,
    ...
...
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37771489
Try this:

SELECT Qty * iif(QTY <> 0 AND TRANSTYPE='ADJ' ,-1,1) as QtyAdjusted, TransType ....
0
 

Author Comment

by:deanlee17
ID: 37771508
Will these adjust the existing QTY column? I dont want to add a new column, I want to overwrite the existing one.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37771529
DECLARE @NewQuantity bigint;

SELECT @NewQuantity = CASE WHEN (QTY <> 0 AND TRANSTYPE='ADJ') then QTY * -1 ELSE QTY END AS QTY from table;

update table
set qty=@NewQuantity
where ---
0
 

Author Comment

by:deanlee17
ID: 37771533
Scott, this failed, it created a new column 'AS Expr1', it also didnt even put the values into minuses.
0
 

Author Comment

by:deanlee17
ID: 37771544
BuggyCoder, I'm trying to create a view, can this code be added and run in the view?
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37771556
DECLARE @NewQuantity bigint;

SELECT @NewQuantity = CASE WHEN (QTY <> 0 AND TRANSTYPE='ADJ') then QTY * -1 ELSE QTY END AS QTY from table;

select @NewQuantity;
0
 

Author Comment

by:deanlee17
ID: 37771588
BuggyCoder,

This is my code so far, how do I add yours? I cannot have 2 SELECT statements in a view, surely?....



SELECT DISTINCT
                      TOP (100) PERCENT dbo.tblMestecData.idMESTEC, dbo.tblMestecData.MESDATE, dbo.tblMestecData.LOCATION, dbo.tblMestecData.ALLOCATION,
                      dbo.tblMestecData.OWNERSHIPTYPE, dbo.tblMestecData.PARTNBR, dbo.tblMestecData.VERSION, dbo.tblMestecData.PARTDESCRIPTION, dbo.tblMestecData.QTY,
                      dbo.tblMestecData.TRANSTYPE, dbo.tblMestecData.REASONCODE, dbo.tblMestecData.REASONDESC, dbo.tblMestecData.TRANSCOMMENT,
                      dbo.tblMestecData.GRNNBR, dbo.tblMestecData.SERIALNBR, dbo.tblMestecData.CURRENCY, dbo.tblMestecData.UNITCOST,
                      dbo.tblMestecData.TOTALBASELINECOST, dbo.tblMestecData.COMPARTNBR, CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND
                      UNITCOST > 0 THEN Price ELSE V1Price END AS DEFV1PRICE, CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND
                      UNITCOST > 0 THEN Currency ELSE V1Currency END AS V1_CURRENCY, CASE WHEN CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND
                      UNITCOST > 0 THEN Currency ELSE V1Currency END = 'EUR' THEN EUR WHEN CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND
                      UNITCOST > 0 THEN Currency ELSE V1Currency END = 'USD' THEN USD ELSE 0 END AS EX_RATE, CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND
                      UNITCOST = 0 THEN 0 ELSE CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND UNITCOST > 0 THEN Price ELSE V1Price END * QTY END AS MAT_COST,
       
FROM         dbo.tblMestecData LEFT OUTER JOIN
                      dbo.stocksale ON dbo.tblMestecData.PARTNBR = dbo.stocksale.PartNumber LEFT OUTER JOIN
                      dbo.tblV1Price ON dbo.tblMestecData.PARTNBR = dbo.tblV1Price.V1Item CROSS JOIN
                      dbo.tblExRates
WHERE     (dbo.tblMestecData.MESDATE > '2012-01-29') AND (dbo.tblMestecData.MESDATE < '2012-02-25')
ORDER BY dbo.tblMestecData.TRANSTYPE
0
 

Author Comment

by:deanlee17
ID: 37771602
Mbizup: Error in list of function arguments: '<' not recognized.
Unable to parse query text.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 37771704
>> Scott, this failed, it created a new column 'AS Expr1', it also didnt even put the values into minuses <<

Something's not right, because it explicilty labeled the column as "QTY".

Maybe you have to use a different name than one that already exists on the table??

You also don't need to check for QTY <> 0, since 0 * -1 will still be zero.



SELECT DISTINCT
                      TOP (100) PERCENT dbo.tblMestecData.idMESTEC, dbo.tblMestecData.MESDATE, dbo.tblMestecData.LOCATION, dbo.tblMestecData.ALLOCATION,
                      dbo.tblMestecData.OWNERSHIPTYPE, dbo.tblMestecData.PARTNBR, dbo.tblMestecData.VERSION, dbo.tblMestecData.PARTDESCRIPTION, dbo.tblMestecData.QTY,
                      dbo.tblMestecData.TRANSTYPE, dbo.tblMestecData.REASONCODE, dbo.tblMestecData.REASONDESC, dbo.tblMestecData.TRANSCOMMENT,
                      dbo.tblMestecData.GRNNBR, dbo.tblMestecData.SERIALNBR, dbo.tblMestecData.CURRENCY, dbo.tblMestecData.UNITCOST,
                      dbo.tblMestecData.TOTALBASELINECOST, dbo.tblMestecData.COMPARTNBR, CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND
                      UNITCOST > 0 THEN Price ELSE V1Price END AS DEFV1PRICE, CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND
                      UNITCOST > 0 THEN Currency ELSE V1Currency END AS V1_CURRENCY, CASE WHEN CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND
                      UNITCOST > 0 THEN Currency ELSE V1Currency END = 'EUR' THEN EUR WHEN CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND
                      UNITCOST > 0 THEN Currency ELSE V1Currency END = 'USD' THEN USD ELSE 0 END AS EX_RATE, CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND
                      UNITCOST = 0 THEN 0 ELSE CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND UNITCOST > 0 THEN Price ELSE V1Price END * QTY END AS MAT_COST,
        CASE WHEN (TRANSTYPE='ADJ') then QTY * -1 ELSE QTY END AS QTY_NEW

FROM         dbo.tblMestecData LEFT OUTER JOIN
                      dbo.stocksale ON dbo.tblMestecData.PARTNBR = dbo.stocksale.PartNumber LEFT OUTER JOIN
                      dbo.tblV1Price ON dbo.tblMestecData.PARTNBR = dbo.tblV1Price.V1Item CROSS JOIN
                      dbo.tblExRates
WHERE     (dbo.tblMestecData.MESDATE > '2012-01-29') AND (dbo.tblMestecData.MESDATE < '2012-02-25')
ORDER BY dbo.tblMestecData.TRANSTYPE
0
 

Author Comment

by:deanlee17
ID: 37771846
Thanks Scott, that worked fine.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

824 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