Results 1 to 6 of 6
  1. #1
    rbennion is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Jun 2017
    Posts
    17

    Subquery with date range

    Hey all,

    I'm pulling my hair out trying to learn the nuances of Access SQL for a new job and i'm not used to it. I am trying to do a nested query that should be quite simple but everything i try is a dead-end. I have a Count in the original SELECT statement which might be throwing me off. See code below. The first one runs fine, but doesn't allow me to specify for a date range. The second code is with my nested query that will not run, giving me an error stating "The SELECT statement includes a reserved word or an argument name that is misspelled, missing or the punctuation is incorrect". No matter where i move it in the query (after the FROM) it gives me errors.

    SELECT tbl1.TranFacName, Count(tbl1.TranFacName) AS NumOfTransfers
    FROM tbl1
    WHERE tbl1.TranFacName Is Not Null
    GROUP BY tbl1.TranFacName;

    now the subquery:

    SELECT tbl1.TranFacName, Count(tbl1.TranFacName) AS NumOfTransfers
    (SELECT tbl1.ArrivalDate FROM tbl1
    WHERE tbl1.ArrivalDate BETWEEN #1/1/2015# AND #12/31/2015#)
    FROM tbl1
    WHERE tbl1.TranFacName Is Not Null
    GROUP BY tbl1.TranFacName;

    I just need to be able to edit the date range for the NumOfTransfers attribute. Is there just a better way to do this entirely?


    Thanks in advance!

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    The first one runs fine, but doesn't allow me to specify for a date range
    Why not? Does it give an error?

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    It looks like you have 2 table queries..
    Q1 should pull data in the date range.
    Q2, uses Q1 to count.

    Q2: select item,count(item) from Q1

  4. #4
    rbennion is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Jun 2017
    Posts
    17
    I can't put the ArrivalDate in the original query because its not in the aggregate (Count), right?

  5. #5
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    No, just add ArrivalDate as an additional field and select "WHERE" in the totals line.

  6. #6
    rbennion is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Jun 2017
    Posts
    17
    HAHAH i'm such a novice. thanks. That worked fine, i just built it in Design View and gave the bird to SQL View. Code looks as follows and runs fine:

    SELECT tbl1.TranFacName, Count(tbl1.TranFacName) AS NumOfTransfers
    FROM tbl1
    WHERE (((tbl1.TranFacName) Is Not Null) AND ((tbl1.ArrivalDate) Between #1/1/2015# And #12/31/2015#))

    I want to keep using SQL View cause i want to continue learning that skill but Design View is soo much more user friendly sometimes.

    Thanks again

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

Similar Threads

  1. Replies: 4
    Last Post: 11-20-2018, 11:57 PM
  2. Replies: 3
    Last Post: 01-26-2016, 01:56 PM
  3. Replies: 3
    Last Post: 10-10-2015, 10:22 AM
  4. add time in to a date sorted subquery
    By breakingme10 in forum Queries
    Replies: 5
    Last Post: 06-06-2014, 12:46 PM
  5. Access Subquery Max Date/Time field
    By ruci1225 in forum Access
    Replies: 1
    Last Post: 04-08-2011, 07:33 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