Results 1 to 8 of 8
  1. #1
    hangbill is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2019
    Posts
    5

    Exclude Some Values From Query

    Please see image, dummy data.
    I need to sum all of marketvalue but exclude the yellow and orange records- these are InvestmentFK 8, 9, 10,12 from earlier dates.

    So need to sum marketvalue for the blue InvestmentFK 8, 9, 10,12 as well as all all unique foreign keys (1,3,4,5 etc)



    Hope u can help.
    Thanks
    Attached Thumbnails Attached Thumbnails Exclude Some Values From Query.png  

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    make an inclusion table, via make table of all investFK, then remove those 4.
    then join the inclusion table to yours shown above,and only get the ones you want.

    or make an exclusion table with the 4 items,
    make a query that uses a subquery for INVESTMENTFK, not in (select Investmentfk from tExclude)
    (similar to a FIND DUPLICATE query via query designer)

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    to clarify what you are asking - you are looking for the latest valuation for each FK

    This requires two queries, the first to determine the latest date for each PK and the second to then get the valuation

    The first query is a simple aggregate query I'll call it maxQry

    Code:
    SELECT InvestmentFK, max(MarketValueDate) as MaxDate
    FROM myTable
    GROUP BY InvestmentFK
    You then join this to your table on FK and dates and sum

    Code:
    SELECT myTable.InvestmentFK, myTable.MarketValueDate, sum(myTable.marketValue) as totalMarketValue
    FROM myTable INNER JOIN maxQry ON myTable.InvestmentFK=maxQry.InvestmentFK AND myTable.MarketValueDate=maxQry.MaxDate
    GROUP BY myTable.InvestmentFK, myTable.MarketValueDate

  4. #4
    hangbill is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2019
    Posts
    5
    Thanks for responding Ranman.
    This is a dynamic situation and some of the foreign keys that are unique in the image (eg 1,3,5,7 etc) might not remain unique. This will happen for example when key 1, say, gets a new market value on some future date. So solution needs to account for changing situation. Something like Sum(markeValue but if InvestmentFK not unique then use only the market value from the most recent date)

  5. #5
    hangbill is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2019
    Posts
    5
    OK Ajax, going to try this. Thanks

  6. #6
    hangbill is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2019
    Posts
    5
    Perfect, thanks so much Ajax.
    Got it working with a few tweaks. Would not have figured this. Must now try understand the joining sql.
    Thanks again

    Code:
    SELECT marketValue.InvestmentFK, Max(marketValue.MarketValueDate) AS MaxDate
    FROM marketValue
    GROUP BY marketValue.InvestmentFK;
    and

    Code:
    SELECT marketValue.InvestmentFK, marketValue.MarketValueDate, sum(marketValue.marketValue) AS totalMarketValue
    FROM marketValue INNER JOIN [Max Value Date] ON (marketValue.InvestmentFK=[Max Value Date].InvestmentFK) AND (marketValue.MarketValueDate=[Max Value Date].MaxDate)
    GROUP BY marketValue.InvestmentFK, marketValue.MarketValueDate;

  7. #7
    hangbill is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2019
    Posts
    5
    Actually seems it is not summing, but rather returning the most recent values. This is not a prob because I want to Dsum them on a report. But then not sure what the Sum function does in this sql

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    to get a single total, remove the grouping columns

    Code:
    SELECT sum(marketValue.marketValue) AS totalMarketValue
    FROM marketValue INNER JOIN [Max Value Date] ON (marketValue.InvestmentFK=[Max Value Date].InvestmentFK) AND (marketValue.MarketValueDate=[Max Value Date].MaxDate)
    Note that having spaces in table/query field names is a bad practice to develop. It will come back to bite you at some point

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

Similar Threads

  1. Crosstab: Exclude "0" values
    By Cronsen in forum Queries
    Replies: 2
    Last Post: 10-18-2018, 11:14 PM
  2. Query to exclude outliers
    By BRZ-Ryan in forum Queries
    Replies: 4
    Last Post: 03-19-2015, 09:05 AM
  3. Exclude TOP N records from query
    By gemadan96 in forum Queries
    Replies: 4
    Last Post: 06-15-2014, 10:11 AM
  4. exclude field in query
    By chrisy in forum Queries
    Replies: 2
    Last Post: 10-28-2011, 09:53 AM
  5. Exclude values that appear in a certain table
    By DB2010MN26 in forum Queries
    Replies: 4
    Last Post: 09-08-2010, 02:33 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