Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

SQL Get Totals

Options
  • 10-05-2011 4:34pm
    #1
    Registered Users Posts: 3,992 ✭✭✭


    OK So i've the following Table:
    table.jpg
    And what I need to do is go through this table and get the total "TimeValue" for each instance that the "EmplNo" and "CodeDescNo" are the same.

    So for example the first three lines, we have 6 and 7 as the first two columns, for each row. What I need to do is add up the "TimeValue" Column for these three instances and deposit the result in a new table which has the layout

    EmplNo--CodeDescNo--Total
    6--7--777
    6--691--0
    6--754--0
    7--8--180

    etc etc..

    After messing about a bit i'm not very far, i'm guessing that a cursor may be needed?

    Anyone have any suggestions?

    Thanks.!


Comments

  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    quite straightforward to do by grouping the select by the EmplNo and CodeDescNo columns


  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    edit: I assumed Mysql... will leave answer below anyway - not sure of the INSERT INTO syntax for SQL Server

    Use SUM to get the total time so sql would look something like:
    SELECT SUM(time) as total, emp, code FROM table1 GROUP BY emp, code
    

    And then to copy these result into a another table you would wrap this inside an INSERT INTO statement, eg:
    INSERT INTO table2 (total, emp, code) SELECT SUM(time) as total, emp, code FROM table1 GROUP BY emp, code
    


  • Registered Users Posts: 3,992 ✭✭✭Korvanica


    Thanks all, got it sorted...Chances are ill be back with more sql questions :S

    Only starting with it and have to face walls of SQL..

    talk about deep end ;)


  • Registered Users Posts: 2,781 ✭✭✭amen


    is this for work or college?
    if work ask them send to you on a course


Advertisement