Results 1 to 4 of 4
  1. #1
    beckysright is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2012
    Posts
    9

    Sum in Union Query

    I am fairly new to writing in SQL and I have a union query that I can't seem to get right. I know it is possible but I can't seem to make it work for me. I need to add together the TotalDays by EmpName. I also need to keep the week ending in there. I would really appreciate it if anyone could tell me how to do this? Thank you!



    SELECT
    IIf([EmployeeComm]=1111,"Name1",IIf([EmployeeComm]=2222,"Name2",IIf([EmployeeComm]=2553,"Name3",IIf([EmployeeComm]=2923,"Name4",IIf([EmployeeComm]=3333,"Name5",IIf([EmployeeComm]=4444,"Name6",IIf([EmployeeComm]=4807,"Name7",IIf([EmployeeComm]=9999,"Name8","Null")))))))) AS EmpName, HoursWorked.WeekEnding, HoursWorked.NoDaysWorked AS TotalDays
    FROM Employees INNER JOIN (HospitalBillingLevels INNER JOIN (PhysicianAgreements INNER JOIN (HoursWorked INNER JOIN InsuranceChargeperInvoice ON HoursWorked.InvNo = InsuranceChargeperInvoice.InvNo) ON PhysicianAgreements.PhysDealNo = HoursWorked.PhysAgreement) ON HospitalBillingLevels.HospitalAgreement = HoursWorked.HospAgreement) ON Employees.EmpID = HospitalBillingLevels.EmployeeCommis
    WHERE (((HoursWorked.WeekEnding)=#7/13/2013#))
    UNION ALL
    SELECT IIf([EmployeeCommis]=1111,"Name1",IIf([EmployeeCommis]=2222,"Name2",IIf([EmployeeCommis]=2553,"Name3",IIf([EmployeeCommis]=2923,"Name4",IIf([EmployeeCommis]=3333,"Name5",IIf([EmployeeCommis]=4444,"Name6",IIf([EmployeeCommis]=4807,"Name7",IIf([EmployeeCommis]=9999,"Name8","Null")))))))) AS EmpName, HoursWorked.WeekEnding, HoursWorked.NoDaysWorked AS TotalDays
    FROM Employees INNER JOIN (HospitalBillingLevels INNER JOIN (PhysicianAgreements INNER JOIN (HoursWorked INNER JOIN InsuranceChargeperInvoice ON HoursWorked.InvNo = InsuranceChargeperInvoice.InvNo) ON PhysicianAgreements.PhysDealNo = HoursWorked.PhysAgreement) ON HospitalBillingLevels.HospitalAgreement = HoursWorked.HospAgreement) ON Employees.EmpID = HospitalBillingLevels.EmployeeCommis
    WHERE (((HoursWorked.WeekEnding)=#7/13/2013#));
    Click image for larger version. 

Name:	query.jpg 
Views:	5 
Size:	43.7 KB 
ID:	13161
    Attached Thumbnails Attached Thumbnails query.jpg  

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Don't understand why you are doing a UNION of two identical queries.

    If you want aggregate calcs, then do a GROUP BY (Totals) query or build a report using Grouping & Sorting with aggregate calcs in header/footer sections.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    beckysright is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2012
    Posts
    9
    It looks very similar but the Joins are different. It shows the number of days worked on the physician commission side and the hospital side. The only difference is where the names are connected.... but they are different.

    I have tried to do groups prior to the union query and it didn't work.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    I have read and compared both SELECT queries letter by letter several times and the joins are the same. The only difference is the field referenced in the IIf expression. Why are you hard coding ID's and names? Is there not a table that relates EmployeeComm and EmployeeCommis numbers with names? Which tables are these fields in?

    Why is it not working, what happens - error message, wrong results?

    Did you try a report? It will allow display of detail data as well as aggregate calcs.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 10-22-2012, 07:43 PM
  2. Union and Sum all in one query
    By joewilly1 in forum Queries
    Replies: 1
    Last Post: 10-12-2012, 08:18 AM
  3. Union Query
    By jlclark4 in forum Queries
    Replies: 3
    Last Post: 02-25-2011, 08:21 PM
  4. Union Query Help
    By jo15765 in forum Queries
    Replies: 7
    Last Post: 01-06-2011, 05:46 PM
  5. Need help with a Union Query
    By jdowdy in forum Access
    Replies: 1
    Last Post: 10-13-2009, 05:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums