Assistance with a Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • csolomon
    New Member
    • Mar 2008
    • 166

    #16
    If they never transferred their transEndDt is Null, meaning they are still in their current department


    DeptTrans: deptTransID, deptID, empID, transDt, transEndDt
    Code:
       
       1. 1, 1, 1, 1.1.09, Null
       2. 2, 2, 1, 12.1.08, 12.31.08
       3. 3, 3, 2, 1.2.09, Null
       4. 4, 3, 3, 1.3.09, Null
    Specifically in the above example, empID 1 has transferred departments twice. His current dept is deptID 1 and his end date is Null. He was in deptID 2 all of 2008 and on 1.1.09, he transferred to dept 1 and is still apart of that dept.

    Comment

    • ChipR
      Recognized Expert Top Contributor
      • Jul 2008
      • 1289

      #17
      Ok, assuming you put in a transfer when they start the job.

      How about:

      Code:
      SELECT Employee.empID, Employee.empNo, Employee.empName, DeptTrans.deptID FROM Employee, DeptTrans WHERE Employee.empID = DeptTrans.empID AND transEndDT IS NULL

      Comment

      • csolomon
        New Member
        • Mar 2008
        • 166

        #18
        Correction, I made it updateable :)

        ---
        Correct, when an employee starts a job, they are put in the transfer table. That query works, but it isn't updateable either.

        Originally posted by ChipR
        Ok, assuming you put in a transfer when they start the job.

        How about:

        Code:
        SELECT Employee.empID, Employee.empNo, Employee.empName, DeptTrans.deptID FROM Employee, DeptTrans WHERE Employee.empID = DeptTrans.empID AND transEndDT IS NULL
        Last edited by csolomon; Mar 17 '09, 08:30 PM. Reason: Mistake

        Comment

        • ChipR
          Recognized Expert Top Contributor
          • Jul 2008
          • 1289

          #19
          That simple query by itself cannot be updated? I just tried a query with IS NULL and didn't have a problem. I don't see why that would be.

          Comment

          • ChipR
            Recognized Expert Top Contributor
            • Jul 2008
            • 1289

            #20
            Never mind, it's not updatable after the join. What the heck.

            Comment

            • ChipR
              Recognized Expert Top Contributor
              • Jul 2008
              • 1289

              #21
              Oh, this may happen if fields in your table are not indexed. Any field that you are querying on should be indexed for performance anyway, are yours?

              Comment

              • csolomon
                New Member
                • Mar 2008
                • 166

                #22
                One more thing worth mentioning. my query that determines which employees are restricted, includes the old department they were in. THis will help me to later determine which employee departments should not receive leave. The rule is that if any employee in a department receives an incident, no employee in that dept receives leave for 2 months. If an employee transfers, he is still under restriction until his old department is off probation.

                here is that query:
                Code:
                SELECT (Select top 1 dept from qryDeptTrans where qryDeptTrans.empID=qryRestrictedEmployees.empID and qryDeptTrans.transDt<=qryRestrictedEmployees.incidentDt) AS Dept_At_Time_Of_Incident, qryRestrictedEmployees.empID, qryRestrictedEmployees.empNo, qryRestrictedEmployees.empName, qryRestrictedEmployees.incidentDt, qryRestrictedEmployees.RestrictionEnd, qryCurrentEmpDept.deptID
                FROM qryRestrictedEmployees INNER JOIN qryCurrentEmpDept ON qryRestrictedEmployees.empID = qryCurrentEmpDept.empID;


                Originally posted by ChipR
                That simple query by itself cannot be updated? I just tried a query with IS NULL and didn't have a problem. I don't see why that would be.

                Comment

                • csolomon
                  New Member
                  • Mar 2008
                  • 166

                  #23
                  My fields are indexed

                  Originally posted by ChipR
                  Oh, this may happen if fields in your table are not indexed. Any field that you are querying on should be indexed for performance anyway, are yours?

                  Comment

                  • ChipR
                    Recognized Expert Top Contributor
                    • Jul 2008
                    • 1289

                    #24
                    Well I certainly learned something about why queries are not updatable. I couldn't update my join query until I made at least one side indexed(no duplicates) and the other side indexed also.

                    So I guess the problem is that the last query you posted still is not updatable even with proper indices?

                    Unfortunately, it's 5pm here so I won't get to look at it until tomorrow.

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32645

                      #25
                      Originally posted by ChipR
                      Well I certainly learned something about why queries are not updatable. I couldn't update my join query until I made at least one side indexed(no duplicates) and the other side indexed also.
                      You may find Reasons for a Query to be Non-Updatable some help.

                      Comment

                      • FishVal
                        Recognized Expert Specialist
                        • Jun 2007
                        • 2656

                        #26
                        Well.

                        IMHO, the problem requires a bit of strategical thinking.
                        • If you could get all:
                          • fields required for records-to-be-updated recognition
                          • fields to be updated
                          • values to update fields

                          in one updateable query, then filtering part could be well unupdateable and reside in "IN" clause.
                        • If not, then you may check whether you could reduce joins by getting recognition and/or update values via VBA functions (built-in or your own) in the query.
                        • If this doesn't help, then pure code solution based on recordsets will work in any case.


                        Regards,
                        Fish.

                        Comment

                        • csolomon
                          New Member
                          • Mar 2008
                          • 166

                          #27
                          Hi Chip,

                          I was reading about why queries aren't updateable and read something that said that you can not have a select clause in your select clause. In my query, (Select top 1 dept from qryDeptTrans where qryDeptTrans.em pID=qryRestrict edEmployees.emp ID and qryDeptTrans.tr ansDt<=qryRestr ictedEmployees. incidentDt) AS Dept_At_Time_Of _Incident:

                          Code:
                          SELECT [B](Select top 1 dept from qryDeptTrans where qryDeptTrans.empID=qryRestrictedEmployees.empID and qryDeptTrans.transDt<=qryRestrictedEmployees.incidentDt)[/B] AS Dept_At_Time_Of_Incident, qryRestrictedEmployees.empID, qryRestrictedEmployees.empNo, qryRestrictedEmployees.empName, qryRestrictedEmployees.incidentDt, qryRestrictedEmployees.RestrictionEnd, qryCurrentEmpDept.deptID
                          FROM qryRestrictedEmployees INNER JOIN qryCurrentEmpDept ON qryRestrictedEmployees.empID = qryCurrentEmpDept.empID;
                          This is not allowing my code to be updateable...I need that statement to pull the department at the time of the incident so that I can use it to compare with other employees to determine which ones will or will not receive leave.

                          Originally posted by ChipR
                          Well I certainly learned something about why queries are not updatable. I couldn't update my join query until I made at least one side indexed(no duplicates) and the other side indexed also.

                          So I guess the problem is that the last query you posted still is not updatable even with proper indices?

                          Unfortunately, it's 5pm here so I won't get to look at it until tomorrow.

                          Comment

                          • ChipR
                            Recognized Expert Top Contributor
                            • Jul 2008
                            • 1289

                            #28
                            I'm not sure why you're using the Top 1 actually. Why not:
                            [This is not in code tags because I want to be able to read it without scrolling]
                            Code:
                            SELECT deptTrans.deptID,
                                   deptTrans.empID
                            FROM   deptTrans, qryRestrictedEmployees
                            WHERE  deptTrans.empID = qryRestrictedEmployees.empID
                              AND  qryRestrictedEmployees.incidentDt >= deptTrans.transDt
                              AND  (qryRestrictedEmployees.incidentDt <= deptTrans.transEndDt
                               OR  deptTrans.transEndDt IS NULL)
                            That gives you employees on the restricted list with their departments at the time, if I'm right about what the qryRestrictedEm ployees does.

                            Comment

                            • csolomon
                              New Member
                              • Mar 2008
                              • 166

                              #29
                              You are right Chip, that works, however it is not updateable either.

                              Comment

                              • ChipR
                                Recognized Expert Top Contributor
                                • Jul 2008
                                • 1289

                                #30
                                Ok then, what's the qryRestrictedEm ployees look like?

                                Comment

                                Working...