How to add values from a column and show the total value SQL 2005?

  • Hello Everyone,

    I have a table 'employee' with 3 columns, EMPNO, HRS_WRKD, PAYCODE_NAME

    This test employee worked 40 hours in REG paycode and 12 hours in OT

    I run the following query to get totals on worked paycodes

    select EMPNO, sum(HRS_WRKD), PAYCODE_NAME

    from employee

    I get the following result

    EMPNO HRS_WRKD PAYCODE_NAME

    1234 40 REG

    1234 12 OT

    What query I need to run to get the following result?

    EMPNO HRS_WRKD

    1234 52

    Thank You,

  • This:

    select EMPNO, sum(HRS_WRKD), PAYCODE_NAME

    from employee

    Is probably actually:

    select EMPNO, sum(HRS_WRKD), PAYCODE_NAME

    from employee

    group by EMPNO, PAYCODE_NAME

    The way to get the second query is get rid of both copies of "PAYCODE_NAME"; both the one in the Select and the one in the Group By.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Awesome, thanks SSCrazy for such a quick reply, I'm so glad I joined this group 🙂

  • You're welcome.

    By the way, "SSCrazy" is a title, because of my number of posts. My name is Gus, and my screen name is "GSquared". It shows up above the post, in the darker-blue row.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Oops sorry about that Gus (GSquared), I'm a newbie and I know it shows 🙂

    Thanks again.

  • No problem. Welcome to a great SQL site.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply