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
4 answers

Having and Where

We use HAVING for groups of rows and WHERE for individual rows. But the following is valid: SELECT Salesperson, SUM(TotalSale) FROM SALES GROUP BY Salesperson HAVING Salesperson <> 'Georgio'; I.e. use HAVING without an…
Jim
  • 18,826
  • 34
  • 135
  • 254
0
votes
3 answers

SQL: Issue with finding unique customers of shops

I am new to SQL, I want to write a query to find the unique customers to any particular shop. For example: Shop SH1 is having 10 customers registered, out of 10 - 3 customers also registered to some other shops. I want a query which will return the…
Mayur Jadhav
  • 125
  • 12
0
votes
2 answers

Excluding items if they have values during a particular time period

I'm trying to return a list of accounts that are selling a particular product (SourceCustomerProductCode = 102) and want to see accounts that have sold this product in year 2013, but if they sold it in 2012, they I want them excluded from my query…
Dale
  • 25
  • 2
0
votes
2 answers

SQL with Multiple Where Clauses

First -- I have read about 7 pages of posts with similar titles but couldn't find the right insight for my challenge My SQL: SELECT name, address, lat, lng, city, state, phone, zip, info , ( 3959 * acos( cos( radians('37.4969') ) * cos( radians(…
0
votes
1 answer

Selecting requests a user hasn't responded to yet

I'm using heroku/ rails/ pgsql 9.1.6 and banging my head against the wall around what feels like it should be a really simple query. I want to surface recent "Requests" that a user hasn't responded to yet Attempt 1 (works before I push to heroku/…
0
votes
2 answers

How to use HAVING clause using RAND() alias

Trying to generate a unique code to prevent duplicates i use the following query using HAVING clause so i can use the alias but i get duplicate key errors: SELECT FLOOR(100 + RAND() * 899) AS random_code FROM product_codes HAVING random_code NOT…
user1765062
0
votes
1 answer

mysql: selecting persons who have a certain amount of relational rows

I have two relational tables, person and event_registration, and I would like to select all persons who have registered to 10 or less events. I have found a query which seems to be correct. It can find these persons with the following query: …
Robin Manoli
  • 2,162
  • 2
  • 25
  • 30
0
votes
1 answer

Count number of rows based on a column value from a left join

For a website (travel agency) I'm working on performance boosting. At the moment I'm optimizing the search-module. Amongst other things, I want to optimize a select-box which holds a destination. In the system there are several destinations, but not…
Ben Fransen
  • 10,884
  • 18
  • 76
  • 129
0
votes
1 answer

Floating point comparison in Oracle

I have a query like SELECT some_field, SUM(float_field) FROM some_table GROUP BY some_field HAVING SUM(float_field) <> 0 Is it safe to use such way of comparison in Oracle or it is better to use something…
Demarsch
  • 1,419
  • 19
  • 39
0
votes
1 answer

WHERE clause in sub-query

Following SQL query: select * from er_101 where cd_relnaam IN ( select cd_relnaam from er_101 group by cd_relnaam having count(*) > 1) AND ld_relopdrachtgever = '1' Though I need to have that sub query also limits on…
Patrick Rennings
  • 191
  • 1
  • 5
  • 19
0
votes
2 answers

Cross Join with always the same string message

I am using Microsoft SQL Server 2005. I would like to have a table in output with a column regarding duplicate values in cross join with a table containing only a column with the same string message. I think the cross join with a "string message"…
TPPZ
  • 4,447
  • 10
  • 61
  • 106
0
votes
3 answers

Using GROUP BY for an non-uniqueness-warning column

I have a query (a view actually) the normally should define a unique mapping from key to value2. select key, value1, value2 from tbl -- joins are left out here where (1=1) -- left out here order by key ; and usually the result is something like key…
towi
  • 21,587
  • 28
  • 106
  • 187
0
votes
1 answer

SQL that will decorate each member of a repeating group with a sequential number

I want to decorate each member of each repeating group in a table with a sequential number. So for: id fk scheme code 1 23 2 1234 2 23 2 6666 3 23 2 9876 4 24 2 3421 5 24 5 …
Olddave
  • 397
  • 1
  • 2
  • 12
0
votes
1 answer

Case When + Join + Group By + Having SQL Query

Please click here for sample tables and description. I have three tables ‘PROJECTS’, 'PROJECTMANAGER' and ‘MANAGERS' as per attached images. I need a query which can list PROJECTS managed by “PRODUCT” type managers grouped with PROJECT STATUS. If…
0
votes
1 answer

sql have function

Hi I have following sql statement that gives me the the first_name and total number of jobs all together each employee has done but now I want to have the first_name and job_id of the jobs that an employee has being doing with another worker I have…
suryll
  • 103
  • 2
  • 6