Questions tagged [having-clause]

About the HAVING keyword in SQL.

(Should really be: )
The HAVING clause is the equivalent of WHERE after applying aggregate functions.

Reference

Related tags

494 questions
6
votes
4 answers

Difference between WHERE and HAVING in SQL

Possible Duplicate: SQL: What's the difference between HAVING and WHERE? I have seen various discussions on WHERE and HAVING. I still have a question: is HAVING used only when considering aggregates, or can it be used in more general terms:…
CuriousKen
  • 69
  • 1
  • 1
  • 2
6
votes
2 answers

MySQL SELECT multiple rows based on specific field value and number of rows

I have three tables: author (columns: aut_id, aut_name) book (columns: book_id, book_title) authorbook (linking table, columns: aut_id, book_id) Each author can be associated with one or more books. Each book can be associated with one or more…
user1894374
  • 233
  • 3
  • 19
6
votes
3 answers

How do you construct the predicate for NSFetchRequest setHavingPredicate:?

For the life of me I can not seem to get this to work. Assume our entity is an managed object with a status field and an order field. How would I go about getting all orderedEntries having more than one order that are the same? Please no answers…
thewormsterror
  • 1,608
  • 14
  • 27
6
votes
1 answer

difference between where and having with respect to aliases

If I create an alias in the select clause then I cannot use it in the where clause because according to the order of execution of sql queries where comes before select. But I can create an alias in the select clause and use it in a having clause…
pooja
  • 73
  • 1
  • 6
5
votes
1 answer

LINQ TO SQL GROUP BY HAVING generated only as subquery

according to all samples, the SQL conversion to LINQ for HAVING clause as in this example: SELECT NAME FROM TABLES GROUP BY NAME HAVING COUNT(*) > 1 is: (vb.net) from t in tables group t by t.NAME into g = Group where g.count > 1 select g BUT…
holy
  • 51
  • 3
5
votes
4 answers

What's the difference between HAVING and WHERE in MySQL Query?

I have a view (viewX) based on joins of some tables: When I use WHERE, the query is delayed, processor usage goes to 50% and finally I need to close mysqld.exe service and restart to try to solve the problem again. When I use HAVING, the query…
CRISHK Corporation
  • 2,948
  • 6
  • 37
  • 52
5
votes
2 answers

How does the HAVING clause really work?

We able to use HAVING clause in SQL-query to filtrate groups of row. When we use GROUP BY clause it work directly in this way. But, let's look to this query: select 1 where 1!=1 having count(*)=0; (or append it with 'from dual' for Oracle). If…
potapuff
  • 1,839
  • 4
  • 18
  • 36
5
votes
1 answer

LINQ with Group By and Having Clause with Min(string)

There are examples with group by - having count or select minimum date with linq on the web but I couldn't find a particular solution for my question. And also I don't have an advanced linq understanding to combine these solutions that I found so…
Baz Guvenkaya
  • 1,482
  • 3
  • 17
  • 26
5
votes
5 answers

Oracle SQL - is there a standard HAVING EVERY workaround?

I am having trouble figuring out a way around Oracle's lack of support for the HAVING EVERY clause. I have two tables, Production and Movie, with the following schema: Production (pid, mid) Movie(mid, director) where 'pid' is in integer…
Dan
  • 2,952
  • 4
  • 23
  • 29
5
votes
1 answer

Multiple conditions in HAVING clause with NHibernate Criteria?

I'm trying to use NHibernate's Criteria API to write the equivalent of this: select foo_id from foo_history group by foo_id having sum(bar_in) > 0 or sum(baz_in) > 0; with this mapping:
wes
  • 1,577
  • 1
  • 14
  • 32
4
votes
2 answers

Difference between HAVING and WHERE clause in SQL

SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value What is the difference between having and where
user909058
  • 188
  • 1
  • 2
  • 11
4
votes
2 answers

SQL Select Count(person_id) > 3 From

Can someone convert this english to SQL I have tried several things but no luck. SELECT CASE WHEN COUNT(person_id) > 3 THEN person_id end FROM table I am trying to only get the person_id(s) that occur > 3 times in the table.
Daniel
  • 4,687
  • 2
  • 19
  • 9
4
votes
1 answer

Can I use HAVING instead of WHERE in SQL queries?

I always thought that I could not, but MSDN says otherwise. When GROUP BY is not used, HAVING behaves like a WHERE clause. I had checked and got the error: Msg 8121: Column '...' is invalid in the HAVING clause because it is not contained in…
Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29
3
votes
1 answer

doctrine: HAVING clause in DQL query

I am working on a simple forum in symfony2 and have the following DQL query to get the last posts in each forum: SELECT p, f.id FROM MyBundle:Post p JOIN p.forum f GROUP BY p.forum HAVING p.created_at >= MAX(p.created_at) Without the HAVING clause…
maiwald
  • 891
  • 12
  • 26
3
votes
1 answer

SQL Join Three Tables; Return Values from Table 1 where all instances in Table 2 have the same field value

I have three tables I need to join together. Specifically table 1 needs to join to table 2 and table 2 joins to table 3. I need to return values from table 1 where all instances of the values selected in table 1 in table 2 have a field of some value…
1
2
3
32 33