Solved

SQL calculation

Posted on 2012-03-27
11
285 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

803 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