Results 1 to 2 of 2

Thread: If

  1. #1
    Join Date
    Dec 2002
    Posts
    1

    If

    Anybody know how to do this?
    I want to creat a SQL Query that SUMS an amout if a condition is positive or SUBTRACT The amout if it is NEGATIVE;
    Something like this ( does not work)

    SELECT *,SUM(QTD) AS QTT FROM FT GROUP BY REF

    What i want is that QTD assumes the value QTD*-1 If a condition occurs , in my case IF CM > 50 (CM is an int number that is on the DB)

    Thanks for any help

  2. #2
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    If you are only interested in SUM then you can use something like this (it is for Northwind database)

    declare @Total numeric
    set @Total = 0
    select @Total = @Total +
    case
    when Quantity > 5 then Quantity*-1
    else Quantity
    end
    from [Order Details]
    select @Total

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •