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
0
votes
1 answer

How to get total number of rows within subquery for multiple conditions

This query return number of rows where Sum of Product's Quantity is equal to 0 and Product Name is 'XYZ'. But I want same result for each products. For example if I remove product name from where clause so it will bring SUM of every product where…
Affan Shahab
  • 838
  • 3
  • 17
  • 39
0
votes
3 answers

Confusion between HAVING and WHERE, SQL Server

I am trying to fetch all vendors whose product's quantity (SUM) is equal to zero. This is what I have done so far but it doesn't return any row nor it gives any error. I am making sure there are products whose quantity is zero and script is running…
Affan Shahab
  • 838
  • 3
  • 17
  • 39
0
votes
3 answers

How can I access columns by name in an anonymous data frame object for filtering in R?

I've got this one-liner: aggregate(. ~ gear,mtcars[mtcars$mpg>20,c('gear','mpg')],mean) Now I want to filter by mpg>25 like his: aggregate(. ~ gear,mtcars[mtcars$mpg>20,c('gear','mpg')],mean)[mpg>25,] But now I get some 1743 lines of NA…
Romeo Kienzler
  • 3,373
  • 3
  • 36
  • 58
0
votes
2 answers

MYSQL GROUP BY HAVING clause unsing MIN and MAX for a range

The query: SELECT product_cd, SUM(avail_balance) prod_balance FROM account WHERE status = 'ACTIVE' GROUP BY product_cd HAVING MIN(avail_balance) >= 1000 AND MAX(avail_balance) <= 10000; The…
Robert
  • 10,126
  • 19
  • 78
  • 130
0
votes
1 answer

Column ambiguously defined error oracle sql

I have a problem in the following sql code.I have a table like this: Student(st_id,st_fname,st_lname) and Visitings(visit_id,st_id,visit_cost). What I want to do is to select average visit_cost for per student where it is more than 500, then…
Programmer
  • 37
  • 6
0
votes
2 answers

How to select a distinct list where a single reference has 2 sub-references but also has more than 1 of each sub-reference value

I am trying to select a distinct list of TRMANE, TRATSA where there is more than one TRASTA of different values associated to a TRMANE. Here is an extract from the table; WHTRANP TRMANE TRASTA 56819 5 56819 5 56819 4 56833 …
user5085719
0
votes
2 answers

argument of HAVING must be type boolean, not type integer

I am having a table course and columns as place and cost with more than two rows. I want my query to display the course which has the highest cost. I tried with the below query select splace from studies group by splace having max(ccost); and…
zeewagon
  • 1,835
  • 4
  • 18
  • 22
0
votes
1 answer

"Having" in SQL

I have a SQL like this below SELECT A.UNIT, A.PO_NO, A.LINE, A.QUNATITY, SUM(B.RECEIVED_QUNATITY) AS "RECEIVED QUANTITY" FROM PO_TBL A, RECEIVER_TBL B WHERE A.UNIT = B.UNIT AND A.PO_NO = B.PO_NO AND A.LINE = B.LINE GROUP BY A.UNIT,…
Spine
  • 11
0
votes
2 answers

How to use CASE and HAVING clauses efficiently to optimize SQL Server Query

I have a SQL Query i.e. SELECT A2P.aid, COUNT(*) [PaperCount], [2010] = SUM(CASE WHEN A2P.p_year = 2010 THEN 1 ELSE 0 END), [2011] = SUM(CASE WHEN A2P.p_year = 2011 THEN 1 ELSE 0 END), [2012] = SUM(CASE WHEN A2P.p_year = 2012…
maliks
  • 1,102
  • 3
  • 18
  • 42
0
votes
3 answers

I'm having problem with SUM() in MySQL

I want to fetch the BalanceAmt of a particular Customer attended by a particular attender. The criteria is that a customer may buy more than one items. Each bought items has been recorded individually. At some cases 'attender' value would be NULL.…
Venkat
  • 2,604
  • 6
  • 26
  • 36
0
votes
6 answers

select data having same id

id idtest result 1 1 2 1 2 1 1 3 2 2 1 2 2 2 1 2 3 1 3 1 1 3 2 2 3 3 1 Would like to get all the rows with the same IDs that matches the…
Jon Etxeza
  • 11
  • 1
0
votes
3 answers

return column data not used in SQL HAVING query

I think this should be straightforward, but I'm at a loss. I want to return data in the columns that weren't used in the SQL statement HAVING clause without having to do a 2nd query. I'm using PostgreSQL, but this should be a fairly generic…
TPM
  • 834
  • 4
  • 9
  • 20
0
votes
3 answers

HAVING COUNT() not giving out the expected result

Attempting to get duplicate values of produce.prod_id using distinct, having clauses.but not giving out the expected result. Here's my data: PRODUCE --------- ----------- PROD_ID PROD_NAME --------- ----------- 1 APPLES 2 …
wkk91193
  • 1
  • 3
0
votes
1 answer

SQL Query - Having Clause

Here are my database relations: shows(showID, title, premiere_year, network, creator, category) episode(showID, episodeID, airdate, title) showID is a foreign key to shows actor(actID, fname, lname) main_cast(showID, actorID, role) showID is a…
0
votes
1 answer

Using GROUP_CONCAT, GROUP, and HAVING in the same statement in MySQL

In an application a have tables projects, categories, and a table, that handles the n:m relationship between of them (project_category_info): Now I want to get all projects of a category (I'm solving this with HAVING). I also needs the information…
automatix
  • 14,018
  • 26
  • 105
  • 230