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
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…
spheroidic
  • 199
  • 7
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
  • 572
  • 5
  • 17
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,412
  • 5
  • 21
  • 51
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
  • 1,113
  • 1
  • 23
  • 42
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
  • 379
  • 5
  • 15
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
  • 75
  • 9
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
  • 1
  • 15
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
  • 436
  • 8
  • 21
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
  • 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
  • 18,954
  • 30
  • 85
  • 131
2
votes
3 answers

Error with ORDER BY used with HAVING Clause

I am trying to use some basic SQL functions. I need to get an average of some data and order it in descending order. The error I get is "group function is not allowed" Table: STUDENTS ----------- ID CLASS GRADE ROOM SQL: SELECT ID, class,…
user953640
  • 23
  • 2
2
votes
1 answer

Need validation that interpretation for a Grouping Query is correct

I am running the following query and at first it appears to give the sub totals for customers and shows by date each customers payment amounts only if that total for all payments is greater than $90,000. SELECT Customername, …
Chon A
  • 33
  • 1
1 2
3
32 33