Questions tagged [having]

About the HAVING keyword in SQL.

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

Reference

Related tags

1156 questions
3
votes
2 answers

MySQL - Using COUNT() to return total results when HAVING is specified

Using the query below, I can search for properties within a given radius and results are returned. SELECT id, address, ( 3959 * acos( cos( radians( 53.184815 ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-3.025741) ) + sin(…
Reado
  • 1,412
  • 5
  • 21
  • 51
3
votes
1 answer

LINQ query using join and aggregate functions

Urgh...my LINQ has suddenly taken a turn for the worse!! I have two tables and want to join and perform aggregate functions upon the tables. Firstly, is this possible, I am assuming it is, and secondly, how do I handle the aggregate functions? As…
Ricardo Deano
  • 2,769
  • 8
  • 47
  • 70
3
votes
4 answers

'invalid column name' while using the HAVING

I am using Microsoft SQL Server 2014. The following is my query SELECT type, SUM(calories) AS total_calories FROM exercise_logs GROUP BY type HAVING total_calories > 150; and I get the error Msg 207, Level 16, State 1, Line 2 Invalid column name…
Newton
  • 418
  • 2
  • 9
  • 19
3
votes
4 answers

Using HAVING or WHERE on SUM Fields

Problem We have a Users table and a Addresses table.Each user can have multiple addresses. We have a bit field called IsDefault where a user can select their default address. This field currently isnt mandatory and possibly will be in the future, so…
garfbradaz
  • 3,424
  • 7
  • 43
  • 70
3
votes
3 answers

Oracle SQL: SQL join with group by (count) and having clauses

I have 3 tables Table 1.) Sale Table 2.) ItemsSale Table 3.) Items Table 1 and 2 have ID in common and table 2 and 3 have ITEMS in common. I'm having trouble with a query that I have made so far but can't seem to get it right. I'm trying to select…
RobertC
  • 123
  • 15
3
votes
1 answer

Magento unknown column in having clause

I need to use having clause in Magento custom collection grid, I try to do this: $store = Mage::app()->getStore($this->getStore()); $collection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToSelect('sku') …
dharth
  • 179
  • 4
  • 12
3
votes
3 answers

Check if two columns are equal for all rows per group

Assuming a DB like this: Date | Attribute1 | Attribute2 | ... | AttributeN ------------------------------------------------- 1 | A | C | ... | ... 1 | B | C | ... | ... 2 | A | A | ... |…
Dan
  • 45,079
  • 17
  • 88
  • 157
3
votes
2 answers

GROUP BY WITH HAVING ( DISTINCT ) : PHP , MYSQL

id | mid | pid | owgh | nwgh | 1 3 12 1.5 0.6 2 3 12 1.5 0.3 3 3 14 0.6 0.4 4 3 15 1.2 1.1 5 4 16 1.5 1.0 6 4 17 2.4 1.2 7 3 19 3.0 1.4 From Above i…
user3209031
  • 837
  • 1
  • 14
  • 38
3
votes
3 answers

SQL Group by Having > 1

Maybe the title was not the best I could use to discribe the issue an example of the table structure I am dealing with is in the image below. I need to write a query to pull all records for "Manufactures" that have more than one record. So the end…
scripter78
  • 1,117
  • 3
  • 22
  • 50
3
votes
3 answers

How to use GROUP By and HAVING in MySQL

The first two queries work fine, the 3rd one runs but brings back nothing when there should be results. How can I get the 3rd one to bring back results. It seems to me the GROUP BY and HAVING are not working together. The 2nd query returns 32 Active…
OahuInvestor
  • 121
  • 4
3
votes
3 answers

How to Use HAVING in a Subquery as Part of a Select Statement

I'm working on a query to return how many activists have volunteered this year by team more than once; I got it to work as a standalone query: SELECT Activists.team, COUNT(id) AS Activists FROM ( SELECT e.id, v.team, …
Ryan
  • 501
  • 1
  • 12
  • 26
3
votes
7 answers

SQL: HAVING clause

See the following SQL statement: SELECT datediff("d", MAX(invoice.date), Now) As Date_Diff , MAX(invoice.date) AS max_invoice_date , customer.number AS customer_number FROM invoice INNER JOIN customer ON…
CJ7
  • 22,579
  • 65
  • 193
  • 321
3
votes
2 answers

Laravel not executing queries with 'having' clause

I have manually built a large query using the various eloquent functions (i.e. $this->newQuery()->join....), but I can't get it to run. When I call echo $query->toSql(); It shows me the query. I can copy and paste it into my command line mysql…
Benubird
  • 18,551
  • 27
  • 90
  • 141
3
votes
2 answers

SQL - CASE WHEN count different values

I need to show how many different values every 'id' has. It should look like this: id | component_a | component_b | component_c -------------------------------------------------- KLS11 | none | one | none KLS12 | …
JennaHO88
  • 31
  • 1
  • 1
  • 2
3
votes
4 answers

Select Foo object without any specific Bar events

Consider two tables: Foo: id INT, name VARCHAR Bar: id INT, foo_id INT REFERENCES Foo(id), event_type VARCHAR DEFAULT NULL, event_duration INT DEFAULT NULL Each Foo item can have multiple Bar events. How to query for Foo items which do…
dotancohen
  • 30,064
  • 36
  • 138
  • 197