Forum Discussion

DataComplianceNerd's avatar
DataComplianceNerd
Copper Contributor
Jan 28, 2020
Solved

Finding Multiple Values

I have a table that has thousands of rows in it. I need to write a query to pull out lines where a person is serving more than one function.   Below in my example I want Micky below to appear in my query because he has multiple functions.  Minnie would not appear because she has the same function the whole time. 

 

Example: 

 

IDFirstLastDateAmountFunction
12833MickyMouse07/26/191,219226
12833MickyMouse07/12/191,219227
12833MickyMouse09/20/191,219228
56025MinnieMouse08/23/191,219226
56025MinnieMouse09/06/191,219226
56025MinnieMouse08/09/191,219226
  • George_Hepworth's avatar
    George_Hepworth
    Jan 30, 2020

    George_Hepworth 

     

    Based on this sample data, these queries return three employee IDs with 2 (or more) Function Codes within the range of the data. To apply date range filtering, you can join this query back to the payroll data and filter it on date.

     

    SELECT Payrolldata.ID, Payrolldata.FunctionCode
    FROM Payrolldata
    GROUP BY Payrolldata.ID, Payrolldata.FunctionCode;

     

    Save this as UniqueEmployeeFunctionCodes

     

    Use it in this query.

     

    SELECT UniqueEmployeeFunctionCodes.ID, Count(UniqueEmployeeFunctionCodes.FunctionCode) AS MultipleFunctionCodes
    FROM UniqueEmployeeFunctionCodes
    GROUP BY UniqueEmployeeFunctionCodes.ID
    HAVING (((Count(UniqueEmployeeFunctionCodes.FunctionCode))>1));

     

     

16 Replies

  • Thank you for your help, @George Hepworth. I followed your instructions and everything is working as expected, except when I try to run the second query. I am getting an error message of: The LEVEL clause includes a work or argument that is misspelled or missing." It doesn't like the HAVING as it appears on a list of reserved words for Access.
    • DataComplianceNerd's avatar
      DataComplianceNerd
      Copper Contributor

      George_Hepworth here is what I am using:

      Query 1: 

      SELECT Payroll.ID, Payroll.[Pay Run], Payroll.FCode
      FROM Payroll
      GROUP BY Payroll.ID, Payroll.[Pay Run], Payroll.FCode;

       

      Query 2: 

      SELECT DistinctJobFunctions.ID, DistinctJobFunctions.[Pay Run], DistinctJobFunctions.FCode
      FROM DistinctJobFunctions;
      GROUP BY DistinctJobFunctions.Pay_Run, DistinctJobFunctions.ID
      HAVING (((Count(DistinctJobFunctions.FCode))>1));

       

      I get the error when running the second query and after I close the error, it highlights the word HAVING.  

       

      Thanks for all your help. 

    • George_Hepworth's avatar
      George_Hepworth
      Silver Contributor

      DataComplianceNerd 

       

      "LEVEL clause"?

       

      HAVING is indeed a reserved word in SQL, it is a way to say, in lay terms "only include records which meet this criteria", i.e. records "HAVING this criteria."

       

      Can you provide the two SQL statements from your two queries? Maybe someone can spot something to help.

       

  • George_Hepworth's avatar
    George_Hepworth
    Silver Contributor

    DataComplianceNerd 

    You can do this with two queries.

    the first will group People by jobs, so you get one row per person per job.

     

    SELECT Table1.FirstName, Table1.LastName, Table1.JobFunction
    FROM Table1
    GROUP BY Table1.FirstName, Table1.LastName, Table1.JobFunction;

     

    Save that as "DistinctJobFunctions" and use this query in a second one.

     

    SELECT DistinctJobFunctions.FirstName, DistinctJobFunctions.LastName, Count(DistinctJobFunctions.JobFunction) AS CountOfJobFunction
    FROM DistinctJobFunctions
    GROUP BY DistinctJobFunctions.FirstName, DistinctJobFunctions.LastName
    HAVING (((Count(DistinctJobFunctions.JobFunction))>1));

Resources