0
votes

The table structure is as follows:

NOTE: The names used are for illustrative purposes only.

Table T1 - col1 INT, col2 CHAR, col3 VARCHAR

Table T2 - col1 INT, col2 VARCHAR col3 CHAR

Table T3 - col1 INT - col1 from Table 2 col2 INT - col2 from Table T col3 INT

SELECT tt1.col2, COUNT(tt1.col1) FROM T1 tt1, T2 tt2, T3 tt3 WHERE tt2.col1 = tt3.col1 AND      
tt3.col2 = tt1.col1 GROUP BY tt1.col1, tt1.col2 HAVING EVERY (tt2.col3 = 'something');

This shows an error that matches the title of the question; However, no error is reported if I remove the HAVING clause.

Is the query syntactically correct?

1
What are you trying to do? You can move your having clause into the where. The specific problem is that you've added the word "EVERY", which is not Oracle syntax. - Ben
@Ben: What if I need every to check if every tuple has 'something' in col3, in table tt2? - user980411
Then it can still go in the where clause. You're using implicit inner joins so you're not going to turn an outer join into an inner join mistakenly. - Ben
where tt2.col3 is not null then null. Absence of null means it has something. - xQbert
@Ben: i.e. WHERE .... AND ALL(tt2.col3 = 'string')... ??? - user980411

1 Answers

2
votes
SELECT tt1.col2, COUNT(tt1.col1) 
FROM T1 tt1 
JOIN T3 tt3 ON (tt3.col2 = tt1.col1)
JOIN T2 tt2 ON (tt2.col1 = tt3.col1)
WHERE tt2.col3 = 'something'
GROUP BY tt1.col2;

It doesn't appear that you need a HAVING clause. You use a HAVING clause when you need to filter results after your GROUP BY has been executed. For example, if you wanted only the records with a count(tt1.col1) greater than 10, you would use a HAVING clause. (HAVING count(tt1.col1) > 10)