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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

SQL Get Totals

  • 10-05-2011 3: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