Results 1 to 4 of 4
  1. #1
    becka11 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Posts
    13

    Aggregating fields in Select Query

    Hi,



    Here is my structure:
    Click image for larger version. 

Name:	Capture.PNG 
Views:	7 
Size:	13.8 KB 
ID:	12195

    I am trying to pull the data in the BackUp_byCo table by Partner. I need this data by Partner name whether the Partner is listed as the initial partner or the current partner in the InitialPartner_PortfolioCo table. The result I need is Partner Name and all of the companies they are associated with (whether they are the initial partner or current partner associated with that PortfolioCoName. Please HELP!! Below are a few SQL's I have tried and have failed.

    SELECT Partners.PartnerName, Backup_byCo.PortfolioCoName, Backup_byCo.FMVUnrealized, Backup_byCo.CostUnrealized, Backup_byCo.UnrealizedGL
    FROM Partners INNER JOIN (InitialPartner_PortfolioCo INNER JOIN Backup_byCo ON InitialPartner_PortfolioCo.PortfolioCoName = Backup_byCo.PortfolioCoName) ON (Partners.PartnerName = InitialPartner_PortfolioCo.CurrentPartner) AND (Partners.PartnerName = InitialPartner_PortfolioCo.InitialPartner)
    WHERE (((Partners.PartnerName)=[InitialPartner_PortfolioCo.CurrentPartner] Or (Partners.PartnerName)=[InitialPartner_PortfolioCo.InitialPartner]))
    ORDER BY Partners.PartnerName;

    And I tried:
    SELECT Partners.PartnerName, Backup_byCo.PortfolioCoName, Backup_byCo.FMVUnrealized, Backup_byCo.CostUnrealized, Backup_byCo.UnrealizedGL
    FROM Partners INNER JOIN (InitialPartner_PortfolioCo INNER JOIN Backup_byCo ON InitialPartner_PortfolioCo.PortfolioCoName = Backup_byCo.PortfolioCoName) ON (Partners.PartnerName = InitialPartner_PortfolioCo.CurrentPartner) AND (Partners.PartnerName = InitialPartner_PortfolioCo.InitialPartner)
    WHERE [InitialPartner_PortfolioCo].[InitialPartner] <> [InitialPartner_PortfolioCo].[CurrentPartner] OR [InitialPartner_PortfolioCo].[InitialPartner] = [InitialPartner_PortfolioCo].[CurrentPartner]
    ORDER BY Partners.PartnerName;

    Any help is greatly appreciated as this is a timely matter! Thank you so much!

  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,956
    Try:

    1. UNION query to get the two partner fields combined as a single field, there is no wizard for UNION, must type (or copy/paste) in SQL view of query designer:

    SELECT ID, PortfolioCoName, InitialPartner AS Partner, "Initial" As Type FROM InitialPartner_PortfolioCo
    UNION SELECT ID, PortfolioCoName, CurrentPartner, "Current" FROM InitialPartner_PortfolioCo;

    2. Use that query in another query that joins with Partners and Backup_byCo
    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
    becka11 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Posts
    13
    Thank you June7! This worked. Only issue is that in the case where the same Partner is listed as both the Initial Partner and the Current Partner for a single company, my new query is double counting that. Can I somehow write in a statement that if for a given company (PortfolioCoName), the same Partner is listed as both Initial and Current (Type), then only pick one? Not sure how I would do something like this. Any help is greatly appreciated!!

    This is my new query:
    SELECT Partners.PartnerName, TotalPartners.PortfolioCoName, Backup_byCo.FMVUnrealized, Backup_byCo.CostUnrealized, Backup_byCo.UnrealizedGL
    FROM (Partners INNER JOIN TotalPartners ON Partners.PartnerName = TotalPartners.Partner) INNER JOIN Backup_byCo ON TotalPartners.PortfolioCoName = Backup_byCo.PortfolioCoName;

  4. #4
    becka11 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Posts
    13
    Looks like I was able to do it with a WHERE clause added to your UNION query as follows:
    SELECT ID, PortfolioCoName, InitialPartner AS Partner, "Initial" As Type FROM InitialPartner_PortfolioCo
    UNION SELECT ID, PortfolioCoName, CurrentPartner, "Current" FROM InitialPartner_PortfolioCo WHERE CurrentPartner <> InitialPartner;

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

Similar Threads

  1. Replies: 3
    Last Post: 04-09-2013, 09:32 PM
  2. Replies: 3
    Last Post: 04-03-2013, 10:39 PM
  3. Aggregating Data
    By mfarley in forum Queries
    Replies: 5
    Last Post: 02-01-2013, 09:13 AM
  4. Replies: 1
    Last Post: 04-02-2012, 04:20 PM
  5. Replies: 1
    Last Post: 08-30-2011, 07:35 AM

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