views:

51

answers:

2

i am new to access sql and i built a select statement that looks like this:

SELECT [Lab Occurrence Form].[Practice Code], 
       Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [CountOf1 0 Preanalytical (Before Testing)], 
       [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]
  FROM [Lab Occurrence Form]
 WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between #9/1/2009# And #9/30/2009#))
having ([CountOf1 0 Preanalytical (Before Testing)] != 0)
GROUP BY [Lab Occurrence Form].[Practice Code], [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)];

its not liking my HAVING clause. what is wrong with it? how do i do a != ??

+1  A: 

Use <>. IIRC, != would work in MySQL though

Michael Buen
tried that and it didnt work, when i tried to run a query it actually asked me for the value of [CountOf1 0 Preanalytical (Before Testing)]
I__
+2  A: 

It's not working because the HAVING clause has to be after the GROUP BY - use:

  SELECT [Lab Occurrence Form].[Practice Code], 
         Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [CountOf1 0 Preanalytical (Before Testing)], 
         [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]
    FROM [Lab Occurrence Form]
   WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between #9/1/2009# And #9/30/2009#))
GROUP BY [Lab Occurrence Form].[Practice Code], [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]
  HAVING Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) != 0);

Also, you typically can't use the column alias in GROUP BY/HAVING clauses.

!= is ANSI 92? standard, but <> should also work.

OMG Ponies
!= may very well be ANSI 92 SQL, but it's not supported by the Access SQL92 "compatibility" mode -- only <> works.
David-W-Fenton