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
2
votes
2 answers

Count SQL Query Records

I have a problem that I need to identify how many companies participated in a given project number. For example, identify which companies participated in exactly 2 projects. Given the base below: id company project 1 company1 project1 2 company2…
Beto
  • 579
  • 1
  • 5
  • 15
2
votes
3 answers

Postgres SQL GROUP BY depending on a value from another column

I am trying to query set result set which returns something like the below. I need to return only 1 row per name and need to GROUP BY but only the name that have a value of '8' under the Grade column are desired. The below is a result from another…
Stackoverflow User
  • 161
  • 1
  • 4
  • 10
2
votes
4 answers

SQL aggregate rows with same id , specific value in secondary column

I'm looking to filter out rows in the database (PostgreSQL) if one of the values in the status column occurs. The idea is to sum the amount column if the unique reference only has a status equals to 1. The query should not SELECT the reference at…
2
votes
2 answers

How to get all records having maximum value in a SQL Server table

I've below data in MigratoryBirds table in SQL Server birdType 1 4 4 4 5 5 5 3 SQL script to create above table: /****** Object: Table [dbo].[migratoryBirds] Script Date: 20-Jul-17 8:01:02 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER…
RBT
  • 24,161
  • 21
  • 159
  • 240
2
votes
4 answers

SQL Server Query to find records with aggregate funct on one column but multiple columns in select clause

Here is the minimized version of the Customer table. There can be customers having same account number mapped to different Group . I am looking to find out customer numbers which are mapped to more than one group. As I was using sybase my query…
Suvasis
  • 1,451
  • 4
  • 24
  • 42
2
votes
1 answer

Django: How do I explicitly make a query with a HAVING clause?

I need to execute some SQL that looks like this: select approve_firm_id,approve_dt,approve_result from main_approve group by approve_firm_id having MAX(approve_dt) and approve_result=0; it runs (mysql-5.1), but if I try in the Django model like…
Vlad Bokov
  • 361
  • 2
  • 9
2
votes
2 answers

Querying on count value having ()

I'm having difficulty with a query which displays records according to their fill rate. For instance, a vacancy can have no bookings or some bookings. If a vacancy has bookings, they can be in the form of 'active [1]', 'pending [0]'. The query I…
user275074
2
votes
2 answers

HAVING more than 2 books

I have to print out only the publishers that published more than 2 books, but for my results, I always get PUB_NAME |TOTAL -------------------------------- Abatis Publishers |4 Core Dump Books |1 <------don't…
Jake
  • 313
  • 1
  • 7
  • 16
2
votes
0 answers

HAVING vs. WHERE use case

I have a column that its value is set from enum. I would like to check if there are lines that mistakely were set to a value nor from the enum, grouped by their value. The following queries retrive the same results: Using HAVING select COLUMN1,…
user3165438
  • 2,631
  • 7
  • 34
  • 54
2
votes
4 answers

Why do WHERE and HAVING exist as separate clauses in SQL?

I understand the distinction between WHERE and HAVING in a SQL query, but I don't see why they are separate clauses. Couldn't they be combined into a single clause that could handle both aggregated and non-aggregated data?
2
votes
2 answers

Is there a better way of filtering data that exists in every group?

I am trying to figure out how to filter a data set from SQL Server. DECLARE @OUTPUT AS TABLE (FILTER INT,PART INT,LOT INT) INSERT INTO @OUTPUT (FILTER,PART,LOT)…
jlimited
  • 685
  • 2
  • 11
  • 20
2
votes
5 answers

How can I make this query better?

I'm learning SQL through GALAXQL http://sol.gfxile.net/galaxql.html Im on lesson 17 - GROUP BY/HAVING Here is the scenario: Let's look at couple of SELECT operations we haven't covered yet, namely GROUP BY and HAVING. The syntax for these…
David Folksman
  • 225
  • 3
  • 8
  • 24
2
votes
1 answer

Filter by many elements in a many to many relationship without using HAVING?

Is there a way to write a parametric SQL query that matches notes linked to multiple projects without using HAVING ? SQLfiddle Say I have a "notes" table, a "projects" table and a "notes_projects" table linking these two together. I want to find the…
Anna B
  • 5,997
  • 5
  • 40
  • 52
2
votes
0 answers

QueryDsl-JPA SUM on Integer field - Parameter value did not match expected type

I'm having a unexpected problem to perform a query using QueryDsl/Hibernate (2.8.2). My field is generated like this, as you can see, it's an Integer. public final NumberPath nbPotatoes= createNumber("nbPotatoes", Integer.class); I'm trying to add…
Arthur H
  • 46
  • 5
2
votes
1 answer

Multiple n:m clauses

I am trying to find products that have certain specs. Because of the complex combinations of specs I have another table that is a n:m relation to another table. product id | other data ... ---+--------------- 1 | bike 2 | bus 3 |…
akirk
  • 6,757
  • 2
  • 34
  • 57