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

489 questions
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…
3
votes
1 answer

Why is this code able to use an alias from SELECT in HAVING?

I found this solution online to a HackerRank problem: SELECT c.hacker_id, h.name, COUNT(c.challenge_id) AS cnt FROM Hackers AS h JOIN Challenges AS c ON h.hacker_id = c.hacker_id GROUP BY c.hacker_id, h.name HAVING cnt = (SELECT…
user8629729
  • 167
  • 6
3
votes
3 answers

Cities with more customer than average. Subquery

I have three tables country_table: id int, country_name string city_table: id int, city_name string, postal_code int, country_id int customer_table: id int, customer_name string, city_id id, customer_address string I am looking for an answer which…
Berk Yünür
  • 33
  • 1
  • 5
3
votes
1 answer

how to take only last transaction to count timediff mysql 5.7

this is the continue question from find out time difference for every user in condition mysql 5.7 this is my fiddle https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=31b3be9d1e2444eb0b32c262176aa4b4 i have this table CREATE TABLE test ( ID INT, …
18Man
  • 540
  • 2
  • 13
3
votes
4 answers

Trying to output only reservations totaling over $2000

SELECT reservationWH.RNUM AS Reservation, packageWH.PNAME AS "Package Name", (COUNT(guestWH.CELL_PHONE) * packageWH.COSTPERSON) AS "Total Cost" FROM packageWH INNER JOIN reservationWH ON packageWH.P_ID = reservationWH.P_ID…
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,221
  • 5
  • 20
  • 49
3
votes
0 answers

Statement couldn't be produced with sql in Zend\Db\Sql\Select

I'm trying to fetch the data with HAVING clause. Here is my code, //Main Table $select->from(array('cre' => 'credit')); //company Table $select->join(array('com' => 'company), 'cre.account_id = com.account_id'); $select->join(array('u' => 'user'),…
Keyur
  • 980
  • 1
  • 20
  • 38
3
votes
4 answers

MySQL Having Clause and Count()

I've got the following table and I'm trying to retrieve every: name, descr, stock, address, postalcode & the city where stock of item1 is > 10 AND stock of item2 is > 10 AND stock of item3 > 5 so I don't want the rows that only contain item1 and…
b101
  • 279
  • 5
  • 14
3
votes
1 answer

MySQL Multiple Conditions on Group By / Having Clause

I have three tables that are all inter-related with the following structure. ModuleCategory Table: +------------------+----------------+------------+ | ModuleCategoryID | ModuleCategory | RequireAll…
celik
  • 55
  • 7
3
votes
2 answers

Group if COUNT(*) > X, otherwise don't group items

I'm trying to return a list of map items from my DB grouping the items ONLY if the group count is greater than 4 otherwise I don't want the items to be grouped. My project is built to return all entries within a set area and I'll be using grouping…
Brett
  • 33
  • 2
3
votes
3 answers

SQL GROUP BY and HAVING

So I don't get this error that I keep getting. select distinct substr(CUSTZIP, 1,5), AVG(CUSTBAL), custcity, custstate from customer group by CUSTCITY, custstate having CUSTSTATE = 'wa' AND avg(CUSTBAL) >100; The error says "not a GROUP BY…
Restnom
  • 124
  • 13
3
votes
3 answers

Select row where SUM becomes greater than X

I have this table ---------------- ID | Duration ---------------- 1 10 2 10 3 10 I want to select the id where sum(duration) becomes greater than 15. In other words... ------------------------- ID |…
user1274113
  • 422
  • 6
  • 17
3
votes
1 answer

Select with Inner Join with Multipart Identifier and Having Clause

I need to select certain fields for a multipart identifier that has duplicates. I have the query to grab the duplicates correct. SELECT b.MemEmpID, b.LastName, b.FirstName FROM table1 As b GROUP BY b.MemEmpID,…
user2136054
  • 33
  • 1
  • 3
3
votes
2 answers

Same conditions, different result?

From reading the MySQL documentation, I can't explain the difference between these two queries in phpMyAdmin: SELECT * FROM f_ean GROUP BY ean HAVING type = 'media' --> gives me 57059 results SELECT ean, type FROM f_ean GROUP BY ean HAVING type =…
puyol5
  • 69
  • 5
3
votes
2 answers

Select query with group by clause and multiple inner joins in Postresql

I have 3 tabels with the following definitions people ------ - wid - name types ----- - guid - type mapping ------- - guid - wid The people table has the list of people The types table the type information for each row present in the people…
Sudar
  • 16,394
  • 28
  • 76
  • 123
1 2
3
32 33