Results 1 to 3 of 3

Thread: SUM query

  1. #1
    Join Date
    Sep 2003
    Posts
    176

    Unanswered: SUM query

    My table XYZ looks like this -

    Col1....Col2....Col3
    10.0 20.0 30.2
    20.0 20.0 30.0
    etc.
    Each of these Cols is CHAR(16). I need to convert them to decimal(12,2).
    I need to add the 3 Cols and insert the result into another table. The result should also be of the type decimal(12,2). I am not able to figure out the SUM query.

    INSERT INTO Total
    (Col4)
    (SELECT SUM(CONVERT(decimal(12,2),Col1)+CONVERT(decimal(12 ,2),Col2)+CONVERT(decimal(12,2),Col3))
    )
    FROM XYZ

    End Result should be -
    ---------------------
    60.20
    70
    and so


    Kindly help me in completing this query.I know I am not right in the above.

    Thanks,

    Vivek

  2. #2
    Join Date
    Sep 2003
    Location
    Dallas, Texas
    Posts
    311
    INSERT INTO Total
    (Col4)
    SELECT Col1 + Col2 + Col3
    FROM XYZ

  3. #3
    Join Date
    Oct 2003
    Posts
    268
    You are very close. However, you don't need to use SUM. That is only when you want to SUM values across different rows.

    Code:
    SELECT
    	CONVERT(DECIMAL(12, 2), COL1) + CONVERT(DECIMAL(12, 2), COL2) + CONVERT(DECIMAL(12, 2), COL3)
    FROM
    	XYZ

Posting Permissions

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