Results 1 to 15 of 15
  1. #1
    countingguru is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    17

    How can I setup two new calculated fields in a Union Query?

    Dear Forum



    I have a union query (QryProjectCost)that assembles three fields from three different tables:

    SELECT Projectnumber,CostType, Amount
    FROM TblLaborCost
    UNION ALL
    SELECT Projectnumber,CostType, Amount
    FROM TblMiscCost
    UNION ALL
    SELECT Projectnumber,CostType, Amount
    FROM TblShippingCost

    However, I need to setup two additional calculated fields in the Union Query to render the total cost per project and another to render the percentage of each cost record relative to its total project cost.
    I believe the DSUM() AS ProjectTotalCost may be appropriate here for total cost, but need help in preparing it.

    How do I add these two new fields into the SQL statement without crashing the query?

    Thanks !

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Suggest you build a report with the UNION as RecordSource. Use report group & sorting with aggregate calcs functionality.

    Why do you have 3 tables? If the field structure is the same then should be one table.
    Last edited by June7; 11-05-2012 at 07:47 PM.
    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
    countingguru is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    17
    Actually I have change order here:

    I need to create another UNION ALL SELECT Statement (below) that totals the above costs (from tables: Labor, Misc and Shipping) unique to projectnumber.
    We run flash reports in Excel linked to Access.

    Thanks again.

  4. #4
    countingguru is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    17
    Field structure is not the same. Tables are inherent to the project management/scheduling system. I have to create a Union query to create a single table source.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    What is '(below)' referring to?

    Why do you anticipate the query crashing? Have you already tried DSum? Assuming CostType is text, try:

    SELECT Projectnumber, CostType, Amount, DSum("Amount","TblLaborCost","CostType='" & [CostType] & "'") As TotalType, Amount/TotalType As Pct
    FROM TblLaborCost
    UNION ALL
    SELECT Projectnumber, CostType, Amount, DSum("Amount","TblMiscCost","CostType='" & [CostType] & "'") As TotalType, Amount/TotalType As Pct
    FROM TblMiscCost
    UNION ALL
    SELECT Projectnumber, CostType, Amount, DSum("Amount","TblShippingCost","CostType='" & [CostType] & "'") As TotalType, Amount/TotalType As Pct
    FROM TblShippingCost;
    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.

  6. #6
    countingguru is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    17
    Thank you for this, now I get a problem linking to Excel "Query too Complex", do you know what this indicates?

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    What are 'flash' reports?

    You want Excel linking to this query?

    Does the query run without error?

    I never tried to link from Excel to a UNION query. Does the link work without the DSum?
    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.

  8. #8
    countingguru is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    17
    Thanks again. Yes, Union Query does not link directly to Excel. I created another query to retrieve Union Query and it works now.....however....DSUM() is running an #Error, unfortunately.

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Don't know, query and DSum look good.

    Would have to examine db if you want to provide. Follow instructions at bottom of my post.
    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.

  10. #10
    countingguru is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    17
    I need the Total to capture the whole dataset unique to projectnumber and costtype.

    I am getting #Error on this:

    SELECT sq.Projectnumber,sq.CostType, sq.Amount, DSUM("Amount","sq","Projectnumber="&Projectnumber& "AND CostType='"&CostType&"'") AS Total
    UNION ALL
    SELECT Projectnumber,CostType, Amount
    FROM TblMiscCost
    UNION ALL
    SELECT Projectnumber,CostType, Amount
    FROM TblShippingCost) As sq;

  11. #11
    countingguru is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    17
    Sorry, syntax error, this is statement that derives an Error:

    SELECT sq.Projectnumber,sq.CostType, sq.Amount, DSUM("Amount","sq","Projectnumber="&Projectnumber& "AND CostType='"&CostType&"'") AS Total
    FROM
    (SELECT Projectnumber,CostType, Amount
    FROM TblLaborCost
    UNION ALL
    SELECT Projectnumber,CostType, Amount
    FROM TblMiscCost
    UNION ALL
    SELECT Projectnumber,CostType, Amount
    FROM TblShippingCost) As sq;

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Think domain aggregate functions have to reference saved Access query, not a nested subquery. So save the UNION then use it in the SELECT query. Also, put spaces on each side of the & characters and in front of the AND operator after the quote mark.
    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.

  13. #13
    countingguru is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    17
    Thanks June7, but no dice. I still got the #error..

  14. #14
    countingguru is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    17
    Also. I am getting "Query too complex" error now

  15. #15
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Going in circles, will have to analyse db to help.
    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: 2
    Last Post: 06-10-2012, 01:10 PM
  2. Query Date Range Based on Calculated Fields
    By wilsoa in forum Queries
    Replies: 6
    Last Post: 03-08-2012, 02:41 PM
  3. Replies: 1
    Last Post: 05-04-2011, 03:51 PM
  4. Union Query (choosing between two fields)
    By naveehgreen in forum Programming
    Replies: 1
    Last Post: 06-17-2010, 03:24 PM
  5. union query with a calculated field
    By grad2009 in forum Queries
    Replies: 9
    Last Post: 03-31-2010, 04:50 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