Solved

SQL calculation

Posted on 2012-03-27
11
286 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

789 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