Results 1 to 5 of 5

Thread: Sum fields

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    584

    Sum fields

    hi please help.how write the query abs((attendmatch.timediff)-(employee.Workhours)and <>(employee.category='Restday') AS Diffhours

    i want to get the Diffhours result abs((attendmatch.timediff)-(employee.Workhours)) AS Diffhours ,if overtime.categoryday =restday then diffours is =timediff ,if overtime.category is not = restday then Diffhours

    this is current code:
    Code:
    SELECT DISTINCT attendmatch.employeeno, attendmatch.overtimedate, employee.Workhours, attendmatch.timediff, abs((attendmatch.timediff)-(employee.Workhours)and <>(employee.category='Restday') AS Diffhours, overtime.othours,overtime.categoryday,overtime.department
    FROM attendmatch, employee, overtime
    WHERE attendmatch.employeeno=employee.employeeno and attendmatch.employeeno=overtime.employeeno and attendmatch.overtimedate=overtime.overtimedate
    GROUP BY attendmatch.employeeno, employee.employeeno, attendmatch.overtimedate, employee.Workhours, attendmatch.timediff, overtime.othours, overtime.categoryday,
    overtime.department
    thanks for help.
    Last edited by gracehskuo; Jul 24th, 2008 at 03:50 AM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Sum fields

    Moved To Database Development

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Sum fields

    You need to always tell us what version of SQL you use - MS SQL, mySql, Access - they all have different syntax for this.

    If it's MS SQL

    Sum(Case When overtime.categoryday='restday' Then SomeField Else 0 End)

    This SUM() will aggregate based on another fields value.

    CASE isn't in ACCESS - instead you use an IIF() function that has three parameters.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    584

    Re: Sum fields

    szlamany:
    hi,this database is using ms.access.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Sum fields

    Then the syntax changes to (I believe - I don't do ACCESS)

    Sum(IIF(overtime.categoryday='restday',SomeField,0))

    Does this make sense?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width