+ Reply to Thread
Results 1 to 2 of 2

Thread: sql help

  1. #1
    Registered User
    Join Date
    11-22-2009
    Location
    Newcastle,England
    MS-Off Ver
    Excel 2003
    Posts
    78

    sql help

    Hi people, Im struggling with this quite hard to explain database query.

    How would I count only once the (many) occurance of a record within a one to many relationship.

    I have the following tables:

    tblCheck:

    lTableID
    StaffNumber
    CheckCompletedDate


    tblError:

    lCheckID
    StaffNumber
    ErrorType


    The red ID's show the link between the tables. I have a sql string that will count the number of times checks have been carried out between current date and CheckCompletedDate-30days for a staffnumber.

    I then however, want to count whether this check resulted in an error. At present I can count the number of times an error occured but sometimes this can be more than once for a single Check. I want a simple yes/no count was there an error or not.

    I will then be grouping this by StaffNumber.

    So my output would be:
    From current date to date-30days:

    StaffNumber // ChecksCarriedOut
    100010134 // 4
    234234212 / / 1

    ---------

    StaffNumber // Errorfoundincheck?
    100010134 // 2
    234234212 // 0


    This will show how many checks had been carried out on different pieces of a staffs work and how many times a piece of work was wrong (not how many errors were within this as a result of the check, just simply if the work was right or wrong).

    Using the above examples 4 seperate pieces of work were checked and 2 of these contained errors (the number of errors i dont care about, but my tblErrors shows all of these errors, i just want to know if a check resulted in an error).

    I will then be able to calculate a percentage for the number of pieces of work checked against the number of these that were incorrect (even if they contained 20 errors or 1 error, it would still be classed as incorrect).


    The query which counts the # of checks correctly:
    Code:
     Set Rs = Db.OpenRecordset("SELECT sStaffNumber, count(*) AS checkcount FROM tblcheck WHERE dteCheckCompletedDate BETWEEN date() AND date()-30  Group By sStaffNumber;")
    The query which counts the # of errors within these checks (but this is currently the sum of every error within that check so if a case contained 10 errors it counts all 10, i want to just see it say 1 error against this check):

    Code:
       Set Rs = Db.OpenRecordset("Select tblError.sStaffNumber, Count(*) As ErrorCount FROM TblError LEFT JOIN tblCheck ON (tblError.lCheckID = tblCheck.lTableID) Where tblCheck.dteCheckCompletedDate BETWEEN date() AND date()-30 GROUP BY tblError.sStaffNumber")
    Thanks for any help sorry to go on but its hard to explain. Posted this on another forum also but no answer found.

  2. #2
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: sql help

    Quote Originally Posted by munkee View Post
    Posted this on another forum also but no answer found.
    In which case - provide the links in case it is answered in the meantime.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0