I'm building a sql query in Access 2013. This is my query:
SELECT
COUNT(Age) FROM Members AS [Under 12] WHERE Age <= 12,
COUNT(Age) FROM Members AS [13-18] WHERE Age BETWEEN 13 AND 18
but I keep getting this error:
Syntax error (comma) in query expression 'Age <= 12, COUNT(Age) FROM SukkotMembers AS [13-18] WHERE Age BETWEEN 13 AND 18`
The problem is with the comma at the end of line two, but I can't see why. When I run just one line it works fine. I am really new to SQL Just so you are aware.
Also I just realized that the AS [Under 12]
and AS [13-18]
are not working and I also have no idea why
First: the syntax you are using is not valid SQL as you have more than one FROM
clause which is not allowed; the same applies to the where
clause, which by the way operates on the whole set.
You can't just take bits and pieces of SQL and patch together to get a working query - there is a rather strict and well-defined syntax that you must adhere to.
What you want is either to use subqueries that return scalar values or to use conditional counting:
SELECT
COUNT(IIF(Age<=12,age,null)) AS [Under 12]
,COUNT(IIF(Age BETWEEN 13 AND 18,age,null)) AS [13-18]
FROM Members
Also, FROM Members AS [13-18]
assigns an alias to the table, not a column.
Using subqueries (which I guess might have been your intent):
SELECT DISTINCT
(SELECT COUNT(*) FROM MEMBERS WHERE AGE<=12) AS [UNDER 12] ,
(SELECT COUNT(*) FROM MEMBERS WHERE AGE BETWEEN 13 AND 18) AS [13-18]
FROM MEMBERS
You can also sum:
SELECT
Sum(Abs(Age <= 12)) AS [Under 12],
Sum(Abs(Age BETWEEN 13 AND 18)) AS [13-18]
FROM
Members
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.