Results 1 to 6 of 6
  1. #1
    pnpez is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    18

    Cool SYNTAX ERROR Summing Columns in Access SQL

    Hello, I can't figure out why I keep getting SYNTAX error for this query. Can someone help?

    SELECT
    HMS.[CountOfClaim Number],
    HMS.[Other Payer Paid],
    HMS.Net_Recovery


    FROM
    (SELECT "Med_Mth1" AS File,
    Count(Recovery_Month1_Med.[Claim Number]) AS [CountOfClaim Number],
    Format(Sum([Other Payer Amount Paid]*0.01), "Currency") AS [Other Payer Paid],
    Format(Sum([Other Payer Amount Paid]-([Other Payer Amount Paid]*0.18))*0.01, "Currency") AS Net_Recovery
    FROM Recovery_Month1_Med


    union SELECT "Med_Mth2" AS File,
    Count(Recovery_Month2_Med.[Claim Number]) AS [CountOfClaim Number],
    Format(Sum([Other Payer Amount Paid]*0.01),"Currency") AS [Other Payer Paid],
    Format(Sum([Other Payer Amount Paid]-([Other Payer Amount Paid]*0.18))*0.01,"Currency") AS Net_Recovery
    FROM Recovery_Month2_Med




    union SELECT "Med_Mth3" AS File,
    Count(Recovery_Month3_Med.[Claim Number]) AS [CountOfClaim Number],
    Format(Sum([Other Payer Amount Paid]*0.01),"Currency") AS [Other Payer Paid],
    Format(Sum([Other Payer Amount Paid]-([Other Payer Amount Paid]*0.18))*0.01,"Currency") AS Net_Recovery
    FROM Recovery_Month3_Med


    union SELECT "RX_Mth1" AS File,
    Count(Recovery_Month1_RX.[Claim Number]) AS [CountOfClaim Number],
    Format(Sum([Other Payer Amount Paid]*0.01),"Currency") AS [Other Payer Paid],
    Format(Sum([Other Payer Amount Paid]-([Other Payer Amount Paid]*0.18))*0.01,"Currency") AS Net_Recovery
    FROM Recovery_Month1_RX


    union SELECT "RX_Mth2" AS File,
    Count(Recovery_Month2_RX.[Claim Number]) AS [CountOfClaim Number],
    Format(Sum([Other Payer Amount Paid]*0.01),"Currency") AS [Other Payer Paid],
    Format(Sum([Other Payer Amount Paid]-([Other Payer Amount Paid]*0.18))*0.01,"Currency") AS Net_Recovery
    FROM Recovery_Month2_RX



    UNION SELECT "RX_Mth3" AS File,
    Count(Recovery_Month3_RX.[Claim Number]) AS [CountOfClaim Number],
    Format(Sum([Other Payer Amount Paid]*0.01),"Currency") AS [Other Payer Paid],
    Format(Sum([Other Payer Amount Paid]-([Other Payer Amount Paid]*0.18))*0.01,"Currency") AS Net_Recovery
    FROM Recovery_Month3_RX


    GROUP BY
    [CountOfClaim Number]
    [Other Payer Paid]
    Net_Recovery)


    AS HMS


    LEFT OUTER JOIN


    SELECT(
    Sum(HMS.[CountOfClaim Number]) Count
    Sum(HMS.[Other Payer Paid]) OtherPaid
    Sum (HMS.Net_Recovery) Net


    FROM HMS)

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    separate each 'block' into its own query, test it, then the query design will tell you the error.
    the final union query will be:

    select * from Q1
    union
    select * from Q2
    union
    select * from Q3


  3. #3
    pnpez is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    18
    Thanks ranman, the UNION queries work, but I get a "SYNTAX error in FROM CLAUSE", and it points to the last 'Select':
    SELECT(
    Sum(HMS.[CountOfClaim Number]) Count
    Sum(HMS.[Other Payer Paid]) OtherPaid
    Sum(HMS.Net_Recovery) Net


    FROM HMS)

  4. #4
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Have you tried running this as a separate query

    Code:
    SELECT(
    Sum(HMS.[CountOfClaim Number]) Count
    Sum(HMS.[Other Payer Paid]) OtherPaid
    Sum (HMS.Net_Recovery) Net
    
    
    FROM HMS)
    
    
    Because it looks completely malformed and shouldn't work, there is no alias for the any of the fields and you aren't selecting anything from anything?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    If your DB was correctly structured, you would not get these problems?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    My observations:


    -- Code should be enclosed with code tags (the # on the quick reply tool bar).

    -- Be aware that "File" is a reserved word in Access. "Count" is also a reserved word AND a built in function.

    -- Should not use spaces in object names.

    -- Your naming convention is terrible. Is "Recovery_Month3_Med" a table or a query? Better if you had "tblRecovery_Month3_Med" or "qryRecovery_Month3_Med".
    So you have 24 tables?
    Recovery_Month1_Med through Recovery_Month12_Med
    Recovery_Month1_RX through Recovery_Month12_RX

    A normalized table structure would be one table for each:
    Recovery_Month_Med
    Recovery_Month_RX

    then filter by dates to get quarterly results.


    -- It looks like there are missing commas or you didn't type them in if you manually entered the code
    Code:
    GROUP BY 
    [CountOfClaim Number]
    [Other Payer Paid]
    Net_Recovery)
    Code:
    LEFT OUTER JOIN
    
    
    SELECT(
     Sum(HMS.[CountOfClaim Number]) Count
     Sum(HMS.[Other Payer Paid]) OtherPaid
     Sum(HMS.Net_Recovery) Net
    
    
     FROM HMS)

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

Similar Threads

  1. Summing multiple columns
    By SDickson in forum Access
    Replies: 7
    Last Post: 11-19-2015, 02:49 PM
  2. Replies: 7
    Last Post: 01-02-2013, 11:23 PM
  3. problem with summing 2 columns
    By kwooten in forum Queries
    Replies: 1
    Last Post: 03-01-2012, 02:44 PM
  4. summing columns from two tables
    By scotty22 in forum Queries
    Replies: 3
    Last Post: 07-22-2011, 09:32 AM

Tags for this Thread

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