Results 1 to 6 of 6
  1. #1
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419

    Access WITH subquery


    Does access have WITH subquery,

    for example creating a variable that stores a whole subquery such as:

    Code:
    WITH variableTEST (a, b, c, d, e) as 
           (SELECT a1, a2, a3, max(ab), count(*) as nbids
            FROM A x natural join B y
            WHERE x.something > y.something
            GROUP by a1, a2, a3)
    
    WITH variableTWO (f, g, h, i, j, k, j) as 
          (SELECT A.bidder, W.a, W.b, W.c, W.d, W.e, A.something
          FROM variableTEST W natural join B A 
          WHERE W.sprice = B.bidprice)
    
    SELECT h FROM variableTWO WHERE now - j <7
    Can it do something like this, if not what alternative do they have to do something similar?
    Using WITH and subquery and variable, and storing variables and then later use it again in another subquery, can
    it do something like this in access SQL?

  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
    I have never seen that. Did you try typing into the SQL view of query builder? What happened?

    Queries can be nested. I don't really know what you are trying to do. Maybe:

    SELECT h FROM (SELECT A.bidder, W.a, W.b, W.c, W.d, W.e, A.something
    FROM (SELECT a1, a2, a3, max(ab), count(*) as nbids
    FROM A x natural join B y
    WHERE x.something > y.something
    GROUP by a1, a2, a3) AS variableTEST W natural join B A
    WHERE W.sprice = B.bidprice) AS variableTWO WHERE now - j <7
    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
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419
    The error I get after putting in the query builder is:

    invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'

    this is the over all code that I believe should work on SQL server,

    Code:
     
    WITH WIN (suid, iid, stime, price, nbids) as 
    
    
        (SELECT bid.seller, bid.iid, bid.starttime, max(bid.bidprice), count(*) as nBIDs
         FROM auction INNER JOIN bid  ON (auction.seller = bid.seller) and (auction.iid = bid.iid) and (auction.startdate = bid.starttime)
         WHERE bid.bidtime < auction.enddate AND bid.bidprice >= auction.minbid AND now >=auction.enddate
         GROUP BY bid.seller, bid.iid, bid.starttime)
    
    
    WITH WINdetails (buid, suid, iid, stime, sprice, nbids, btime ) as 
        (SELECT b.bidder, w.suid, w.iid, w.stime, w.sprice, w.nbids, b.bidtime
        FROM WIN W INNER JOIN BID B WHERE w.sprice = b.bidprice)
       
       SELECT iid from WINDETAILS WHERE now - enddate < 7
    What I am trying to do is find the person who won the bid within the last week.
    This code below finds the first part ("all the person who won the bid") but it doesn't set the criteria within the last week.

    Code:
     
    SELECT bid.seller, bid.iid, bid.starttime, max(bid.bidprice), count(*) as nBIDs
                  FROM auction INNER JOIN bid  ON (auction.seller = bid.seller) and (auction.iid = bid.iid) and (auction.startdate = bid.starttime)
                  
                  WHERE bid.bidtime < auction.enddate AND bid.bidprice >= auction.minbid AND now >=auction.enddate
                  GROUP BY bid.seller, bid.iid, bid.starttime

    this code below is suppose to limit them to the items that were sold last week.

    Code:
     
    SELECT iid from WINdetails WHERE now - enddate < 7
    The code you provided is actually not similar to the first one I put up, because the one I put up is using variable to represent all the fields,

    example:
    WITH WIN (suid, iid, stime, price, nbids) as

    bid.seller = suid
    bid.iid = iid
    bid.starttime = stime
    max(bid.bidprice) = price
    and count(*) = nbids

    Here is the sample data, highlighted in yellow, the bottom row is the winning bid because it's the highest bid price.
    Click image for larger version. 

Name:	sample DATA.png 
Views:	15 
Size:	31.0 KB 
ID:	14003

  4. #4
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419
    I think I figured it out with your help on nesting SELECT statements.
    This is the code I think should do it.

    Code:
     
    
    
    SELECT bid.seller, bid.iid, bid.starttime
    
    
    FROM (
    
    
    SELECT bid.seller, bid.iid, bid.starttime, auction.enddate, max(bid.bidprice), count(*) as nBIDs
                  FROM auction INNER JOIN bid  ON (auction.seller = bid.seller) and (auction.iid = bid.iid) and (auction.startdate = bid.starttime)
                  
                  WHERE bid.bidtime < auction.enddate AND bid.bidprice >= auction.minbid AND now >=auction.enddate
                  GROUP BY bid.seller, bid.iid, bid.starttime, auction.enddate
    
    
    ) 
    
    
    
    
    WHERE now - ENDdate < 7


    But I still wonder if the above code that I have doable in access.

    What if I wanted, the name of the buyer and seller, and the item then how do I get those to show in the query since it will be difficult because I
    am doing a GROUP BY and with the GROUP BY I have to not include certain fields otherwise the GROUP BY won't work.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Actually, I don't understand aggregating with BidID. Are there multiple records for each BidID?
    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
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419
    yes bidid has multiple records, bid.iid is the item and many people can bet on the same item. :-)

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

Similar Threads

  1. Subquery
    By Daryl2106 in forum Access
    Replies: 20
    Last Post: 01-13-2013, 07:21 AM
  2. TOP subquery
    By helpaccess in forum Queries
    Replies: 5
    Last Post: 08-30-2011, 10:28 AM
  3. Access SQL Subquery Problem
    By dfenton21 in forum Access
    Replies: 2
    Last Post: 07-26-2011, 07:54 AM
  4. Access Subquery Max Date/Time field
    By ruci1225 in forum Access
    Replies: 1
    Last Post: 04-08-2011, 07:33 AM
  5. Subquery
    By combine21 in forum Queries
    Replies: 2
    Last Post: 09-17-2010, 04: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