Results 1 to 4 of 4
  1. #1
    Join Date
    May 2004

    Unanswered: conditional query

    Hi there!

    I'm struggling to write the correct SQL to do the following task:

    there are 3 fields in a table: SSize1, SSize2, SSize3.
    the data type of all three is integer.
    i need to do a simple calculation to determine SSize based on the following condition:
    if SSize3 <>0 then
    SSize = SSize1 + (SSize2/SSize3)
    else SSize = SSize1
    end if

    can anyone please help me? thanks in advance!


  2. #2
    Join Date
    Feb 2004
    In front of the computer
    Provided Answers: 54
    Does your SQL dialect support the CASE operator? That would be how I'd approach solving your problem.


  3. #3
    Join Date
    Apr 2004
    do you want to get the SSize per each row?
    If so, I think you have to store these 3 fields first in a temp table. Then use a cursor, or something like that,for your calculations to compute SSize per row.

    If the SSize would get the total of these 3 fields, get first the sum for each of these fields. Placed it in integer variables, then proceed to your calculations. Try this

    select @SSize1 = sum(SSize1), @SSize2 = sum(SSize2), @SSize3 = sum(SSize3)
    from table_name

    if @SSize3 <> 0 then
    SSize = @SSize1 + (@SSize2/@SSize3)
    SSize = @SSize1
    end if

    hope this would work on you

  4. #4
    Join Date
    Jan 2004
    Croatia, Europe
    Provided Answers: 5
    Or, perhaps,

    SELECT DECODE(SSize3, 0, SSize1, SSize1 + (SSize2 / SSize3)) SSize
    FROM table_name;

Posting Permissions

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