Solved

SQL calculation

Posted on 2012-03-27
11
284 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 69

Accepted Solution

by:
Scott Pletcher earned 500 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
 
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 69

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now