How to multiply two value from two fields

Discussion in 'SQL Server' started by fdtoo, Apr 12, 2006.

  1. fdtoo

    fdtoo New Member

    Joined:
    Apr 12, 2006
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    I have the following fields in table A:

    Code:
     GL_ID|GL_Name_VC |    Amount    |Period_TI|Year_SI
    ===================================================
      1000|  Inventory|   8,000.00   |  01     |  2005
    ===================================================
      1000|  Inventory|  -3,000.00   |  02     |  2005
    ===================================================
      1000|  Inventory|   5,000.00   |  02     |  2005
    ===================================================
    the fields above have the following datatype:

    Code:
     Fields        | Datatype                 
     ===================================
     GL_ID         | Integer            
     GL_Name_VC    | Variable Character
     Amount        | Integer
     Period_TI     | TinyInteger
     Year_SI       | SmallInteger
    The above database is running on Microsoft SQL Server 2000 and i would like to query
    for a report that looks something as below:

    Code:
    GL_ID|GL_Name_VC |Period_01|Period_02 |Total_YTD
    ================================================
    1000 | Inventory | 8,000   |  2,000   |10,000
    
           Percentage|   10%   |   20%    |   0
    ================================================
           Total     |   800   |    400   | 1,200
    The Total row is calculated by multiplying the percentage row by the Inventory amount in
    each Period.

    Guys, hope someone out there can help me with the sql command for the above report?
     
  2. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    Code:
    SELECT GL_ID, GL_Name_VC, 
    SELECT Period_TI FROM <tablename> WHERE Period_TI = 1 as Period_01, 
    SELECT Period_TI FROM <tablename> WHERE Period_TI = 1 as Period_02, 
    (Period_01 + Period_02) as Total_YTD
    FROM <tablename>
    GROUP BY GL_ID
    Now multiplying the percentage will give you the totla row.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice