SOLVED

Inserting empty rows

%3CLINGO-SUB%20id%3D%22lingo-sub-2098302%22%20slang%3D%22en-US%22%3EInserting%20empty%20rows%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2098302%22%20slang%3D%22en-US%22%3E%3CP%3E%3CEM%3EThis%20should%20be%20simple%2C%20but%20my%20SQL%20skills%20are%20rusty.%26nbsp%3B%20I%20have%20a%20query%20that%20extracts%20the%20year%20and%20a%20count%20of%20incidents%20from%26nbsp%3B%20a%20table.%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CEM%3ESELECT%20year%2C%20Count(year)%20AS%20incidents%3CBR%20%2F%3EFROM%20Table2%3CBR%20%2F%3EWHERE%20((Table2.cause_ID%3D15)%20OR%20(Table2.cause_ID%3D16))%3CBR%20%2F%3EGROUP%20BY%20year%3B%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3EOf%20course%2C%20many%20years%20have%20no%20matching%20cause_ID%20%3D%2015%20or%2016.%26nbsp%3B%20I%20would%20like%20an%20output%20of%20year%20and%200%20for%20those%20years.%3C%2FEM%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2098302%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3E2013%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAccess%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2098331%22%20slang%3D%22en-US%22%3ERe%3A%20Inserting%20empty%20rows%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2098331%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F943315%22%20target%3D%22_blank%22%3E%40Clayton_cramer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20want%20to%20see%20all%20years%20regardless%20of%20whether%20there%20are%20any%20matching%20cause_IDs%20for%20those%20years%2C%20then%20you%20can't%20use%20the%20WHERE%20clause%20to%20exclude%20them.%3C%2FP%3E%3CP%3EInstead%2C%20you%20need%20to%20use%20a%20conditional%20for%20the%20Count(year)%20to%20return%20the%20actual%20Count%20where%20there%20are%20matching%20cause_IDs%20or%200%20where%20there%20are%20none.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESELECT%20year%2C%20Iif(%5Bcause_ID%5D%20%3D%2015%20or%20%5Bcause_ID%5D%3D16)%2C%20count(year)%2C%200)%20AS%20incidents%3C%2FP%3E%3CP%3EFROM%20Table2%3C%2FP%3E%3CP%3EGROUP%20BY%20year%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2098874%22%20slang%3D%22en-US%22%3ERe%3A%20Inserting%20empty%20rows%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2098874%22%20slang%3D%22en-US%22%3E%3CP%3Ewrong%20number%20of%20arguments%20used%20in%20query%20function%20lif(%5Bcause_ID)%20%3D%2015%20or%20%5Bcause_ID%5D%3D16'.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

This should be simple, but my SQL skills are rusty.  I have a query that extracts the year and a count of incidents from  a table.

SELECT year, Count(year) AS incidents
FROM Table2
WHERE ((Table2.cause_ID=15) OR (Table2.cause_ID=16))
GROUP BY year;

 

Of course, many years have no matching cause_ID = 15 or 16.  I would like an output of year and 0 for those years.

20 Replies

@Clayton_cramer 

If you want to see all years regardless of whether there are any matching cause_IDs for those years, then you can't use the WHERE clause to exclude them.

Instead, you need to use a conditional for the Count(year) to return the actual Count where there are matching cause_IDs or 0 where there are none.

 

SELECT year, Iif([cause_ID] = 15 or [cause_ID]=16), count(year), 0) AS incidents

FROM Table2

GROUP BY year;

wrong number of arguments used in query function lif([cause_ID) = 15 or [cause_ID]=16'.

@Clayton_cramerlif looks like a typo for if, but "undefined function 'if' in expression."

@Clayton_cramer 

 

Iif() is the immediate If function; that's how it is spelled.

 

There must be a different syntax error in my suggested approach. It's aircode since I don't have visibility to your actual table(s). See if you can suss it out, but post back if not.

@George Hepworth 

Wait, there may be a missing left paren. Add a second one after the first one  

@George Hepworth  I added that parenthesis and now I get "Your query does not include the specified expression 'lif([cause_ID]15 Or [cause_ID=16,count(year),0)' as part of an aggregate function.

 

SELECT year, Iif(([cause_ID] = 15 or [cause_ID]=16), count(year), 0) AS incidents

FROM Table2

GROUP BY year;

 

I would upload the database, but it is huge: every mass murder in American history from 1657 to 1914 (so far).  year is a number, cause_ID is a number.

@Clayton_cramer 

 

Reread what you entered, please.  What you posted here is now missing a closing square bracket. It's also missing an equal sign. Trouble-shooting often requires taking a closer, second look at things.

I put everything in a format to simplify matching parens and brackets

SELECT year, iif (
  (
    (
    [cause_ID] = 15
    )
  or
    (
    [cause_ID]=16
    )
  )
,
count(year), 0) -- matches opening ( of iif
AS incidents

FROM Table2

GROUP BY year;

 

All parens and brackets match.  "Your query does not include the specified expression... as part of an aggregate function."  I put the [cause_ID] clauses in their own parens to make sure iif was treating that as a single boolean operation.  Even removing those parens, I get the same error message.

@Clayton_cramer 

 

That's a different error. Let's try adding the expression to the GROUP BY.

Wrong approach, sorry. See the next post.

@Clayton_cramer 


And my apologies. I didn't try it out before I sent it and I think this will actually give you the result.


SELECT year, Sum(IIF([cause_ID] = 15 or [cause_ID]=16) , 0,1) AS incidents

FROM Table2

GROUP BY year

 

That avoids the complication of having the Aggregate in either the GROUP BY or in the SELECT. 

@George Hepworth"Wrong number of arguments used with function in query expression" followeed by the Sum(IIF expression.  Shouldn't Sum only take one expression?  It appears as if the IIF... 0,1 are being treated as three arguments to SUM.

@Clayton_cramer 

 

I've always believed in the power of "trial and error" as a way to resolve such problems.

What happens when you change the suggested approach the way you describe?

IIF needs an expression and two values for TRUE and FALSE evaluations of the expression.  It should return a single VALUE and SUM should add all those values for all rows.  What you provided makes perfect sense--except to Access, which is still complaining about wrong number of arguments.  It is almost like IIF does not recognize that expression.

@Clayton_cramer If you can, I would like some sample data to work with in order to see what might the problem is. Thanks.

BTW.  Access most definitely will evaluate the expression once it is properly written, so please include your query with that expression with the sample data. Thanks.

I am embarrassed to say that I cannot figure out how to export my database with just a few records in it, so it is a size that I can upload.

@George HepworthI tried attaching the database:

Correct the highlighted errors and try again.

  • The attachment's massmurder.accdb content type (application/msaccess) does not match its file extension and has been removed.

@Clayton_cramer If you can, compress it into a ZIP file and see if that will upload. If not, we can try an alternate method.

best response confirmed by Clayton_cramer (Occasional Contributor)
Solution

@Clayton_cramer If you don't like IIF, then use IN:

SELECT 
    year, 
    Abs(Sum([cause_ID] In (15, 16))) AS incidents
FROM 
    Table2
GROUP BY 
    year;

@Gustav Brockperfect!