Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Multiple AND in having

Author  Topic 

mitasid
Yak Posting Veteran

51 Posts

Posted - 2006-08-21 : 23:47:56
Hi Guys

I need to use 3 conditions using AND in my query.
Its in the following format:

SELECT Indicatorid, DHBName, PHOName, PracticeName, SUM(Numerator) AS Expr1, SUM(Denominator) AS Expr2
FROM PhoNew
GROUP BY Indicatorid, DHBName, PHOName, PracticeName
HAVING indicator id = 1 AND dhb name = 'canterbury dhb ' AND practicename = 'canterbury community pho- 585342'


If i use two conditions in AND, query works correctly but when i use the third conditions, I get a syntax error

Please help

Thanks
Mita

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-08-21 : 23:56:18
quote:
HAVING indicator id = 1 AND dhb name = 'canterbury dhb ' AND practicename = 'canterbury community pho- 585342'


it should be Indicatorid and DHBName is it ?

"I get a syntax error"
What is the error message ?



KH

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-08-22 : 00:54:27
Also put these comparisons at WHERE, not HAVING.

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2006-08-22 : 08:55:14
As Peso is saying you need to use the WHERE-statement instead. HAVING is like the WHERE but for the aggregated columns instead. Example:

SELECT Indicatorid, DHBName, PHOName, PracticeName, SUM(Numerator) AS Expr1, SUM(Denominator) AS Expr2
FROM PhoNew
WHERE Indicatorid = 1 AND DHBName = 'canterbury dhb ' AND practicename = 'canterbury community pho- 585342'
GROUP BY Indicatorid, DHBName, PHOName, PracticeName
HAVING SUM(Numerator) > 52 AND SUM(Denominator) < 4

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page
   

- Advertisement -