Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

SQL Get Totals

  • 10-05-2011 04:34PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 3,141 ✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 2,781 ✭✭✭amen


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


Advertisement