Results 1 to 4 of 4
  1. #1
    offie is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    176

    I want to group by the last date

    First let me explain what my data is and what I want to get:


    For every Project and every Trade there are multiple Bids
    I want to get the range of the BidAmount so I came up with
    Code:
    SELECT dbo_Project.StatusCode, dbo_BidsReceivedDetail.TradeCode, dbo_BidsReceivedDetail.ProjectCode, dbo_BidsReceivedDetail.AlternateBidNumber,  
    Count(dbo_BidsReceivedDetail.BidAmount) AS CountOfBidAmount, CCur(Val(Min(dbo_BidsReceivedDetail.BidAmount))) AS MinOfBidAmount, 
    CCur(Val(Max(dbo_BidsReceivedDetail.BidAmount))) AS MaxOfBidAmount, 
    CCur(Val((Max([dbo_BidsReceivedDetail].[BidAmount])-Min([dbo_BidsReceivedDetail].[BidAmount])))) AS Range
    
    FROM dbo_Project INNER JOIN dbo_BidsReceivedDetail ON dbo_Project.ProjectCode = dbo_BidsReceivedDetail.ProjectCode
    
    GROUP BY dbo_Project.StatusCode, dbo_BidsReceivedDetail.TradeCode, dbo_BidsReceivedDetail.ProjectCode, dbo_BidsReceivedDetail.AlternateBidNumber
    
    HAVING (((dbo_Project.StatusCode)="AWD" Or (dbo_Project.StatusCode)="COM") 
    AND ((dbo_BidsReceivedDetail.AlternateBidNumber)=0))
    ORDER BY dbo_BidsReceivedDetail.TradeCode;
    But then I realized that bids can be places on different days and I need to only show the last day. I want to do this all in one query if possible.

    I tried to group by Last(dbo_BidsReceivedDetail.BidDate) but apparently that can't be done.

    Any Ideas?

    Thanks!

    Here is an example of the data I am getting this from, I want to use only the highlighted values to get the range of bid amount
    Click image for larger version. 

Name:	Capture.PNG 
Views:	13 
Size:	16.4 KB 
ID:	13153
    Last edited by offie; 07-22-2013 at 11:06 AM. Reason: Added picture

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Returning record based on the Last (or Maximum) value in a field by group requires a subquery or DMax or DLast domain aggregate function.

    Review: http://allenbrowne.com/subquery-01.html#TopN
    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
    offie is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    176
    How exaclty would I do this? To get a Last value you have to group by another field, hence you get 2 fields, from what I've read it seems that subqueries only work when you get one field.

    I tried to add
    (SELECT ProjectCode, Last(BidDate) AS LastOfBidDate
    FROM dbo_BidsReceivedDetail AS Dupe
    GROUP BY Dupe.ProjectCode) AS LastDate
    to the SELECT of the main query and then GROUP BY LastDate,

    I get an error about using a query "that can return more than one field without using the EXISTS reserved word in the main query's FROM clause."

    Comments?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The sql you posted doesn't follow the Top N example I referenced. That example does not use any aggregate functions and does not do any grouping. It does require a unique ID field.
    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: 0
    Last Post: 02-25-2013, 04:43 PM
  2. Replies: 2
    Last Post: 06-21-2012, 07:40 PM
  3. Group by Week for any given start date
    By royalrochelle in forum Queries
    Replies: 3
    Last Post: 02-15-2012, 08:44 PM
  4. Option group and date
    By kigor in forum Forms
    Replies: 0
    Last Post: 04-22-2011, 12:52 AM
  5. columns for group detail but not group header?
    By Coolpapabell in forum Reports
    Replies: 0
    Last Post: 08-21-2009, 08:53 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