Results 1 to 5 of 5
  1. #1
    BristolGarry is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Posts
    2

    SQL Syntax Error - Assistance please

    Hi Folks - I am trying to create a query that will return the number of visitors we have each month, as well as the number of unique visitors in that same month. i.e. In April we had 135 visitors, 85 of which were unique (some visitors come in multiple times.) The SQL that I have in my query so far is returning the number of visitors per month, but I am not able to nail down the syntax for the "unique ID" part.



    This is what I have at the moment:
    SELECT DISTINCTROW Format$([Daily Visits Table].[Visit Date],'mmmm yyyy') AS [Visit Date By Month], Round(Sum([Daily Visits Table].TimeSpent)/60,2) AS [Sum Of TimeSpent], Count(*) AS [Count Of Daily Visits Table], (SELECT COUNT(*) AS IDCount
    FROM ((SELECT DISTINCT ID FROM Daily Visits Table) AS DistinctID)) AS DistinctIDs
    FROM [Daily Visits Table]
    GROUP BY Format$([Daily Visits Table].[Visit Date],'mmmm yyyy'), (SELECT COUNT(*) AS IDCount
    FROM ((SELECT DISTINCT ID FROM Daily Visits Table) AS DistinctID)), Year([Daily Visits Table].[Visit Date])*12+DatePart('m',[Daily Visits Table].[Visit Date])-1;

    When I run this I am getting the error message: "Syntax error in query expression "
    (SELECT COUNT(*) AS IDCount FROM ((SELECT DISTINCT ID FROM Daily Visits Table) AS DistinctID)),"

    What am I doing wrong here?

    Thanks!

    BristolGarry


  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    If you use a query ,you won't get any syntax errors.

  3. #3
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    This is why we repeatedly inform people that you should NOT use special characters (except maybe for underscore) or have spaces in object names. You will get caught eventually.

    [Daily Visits Table], not Daily Visits Table
    What I don't get is, are you writing this out? That would explain it. If you're using the query builder, then the missing brackets are a puzzle. If you must write your own sql, then put it into sql view of the query builder when it doesn't work. The offending portion is usually highlighted. In this case, Access would likely fix it automatically.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    In addition to the other comments, since your subquery is referencing the same table as the main query, you need to differentiate it by aliasing it otherwise it will look at the main table. Also looks like you have made it more complex than it needs to be

    (SELECT COUNT(*) FROM (SELECT DISTINCT ID FROM [Daily Visits Table] AS Tmp)) AS DistinctID

    What I don't get is, are you writing this out?
    its a subquery - sql doesn't check the syntax until it is executed

    not tested but you are not grouping by the distinctID subquery, instead by a calculation on that subquery. May work, may not

  5. #5
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Never mind. I had my threads mixed up.

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

Similar Threads

  1. Replies: 7
    Last Post: 06-04-2015, 09:46 AM
  2. Replies: 3
    Last Post: 01-23-2014, 07:49 AM
  3. Assistance with SQL syntax.
    By gm_lowery in forum Access
    Replies: 6
    Last Post: 06-27-2012, 12:07 PM
  4. Replies: 6
    Last Post: 05-30-2012, 12:32 PM
  5. Incomplete Syntax Clause (syntax error)
    By ajetrumpet in forum Programming
    Replies: 4
    Last Post: 09-11-2010, 10:47 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