Questions tagged [having]

About the HAVING keyword in SQL.

(Should really be: )
The HAVING clause is the equivalent of WHERE after applying aggregate functions.

Reference

Related tags

1156 questions
5
votes
3 answers

Mysql: rows having minimum value and prioritize where condition based on case

I have two tables, first table is: docs and another table: doc_val with doc_id as foreign key from table docs I need to get list of docs (including val, type and criteria from doc_val ) which matches certain conditions, say for example:…
ro ko
  • 2,906
  • 3
  • 37
  • 58
5
votes
3 answers

PostgreSQL: How to select 2 different counts from table when x=a and x=b

Dear fellow Stackoverflowers, How may one select 2 different counts from the same table for scenarios: x=a and x=b specifically, (WHEN type = subdomain) AND (WHEN subtype = subdomain)? to add them together to create a 'totalcount'? My attempt (for…
user5527252
  • 73
  • 1
  • 7
5
votes
2 answers

SQL Count where clause

I have the the following SQL statement: SELECT [l.LeagueId] AS LeagueId, [l.LeagueName] AS NAME, [lp.PositionId] FROM (Leagues l INNER JOIN Lineups lp ON l.LeagueId = lp.LeagueId) WHERE (lp.PositionId…
Kris B
  • 3,436
  • 9
  • 64
  • 106
5
votes
2 answers

SQL: Select two columns by single column in group by with only having condition

I'm a bit new to SQL; I have a single table Products: maker model type A 1232 PC A 1233 PC A 1276 Printer A 1298 Laptop A 1401 Printer A 1408 Printer A 1752 Laptop B 1121 PC B 1750 Laptop C 1321 …
ashka
  • 53
  • 2
5
votes
1 answer

elasticsearch like select ... where ... group by ... having

In elasticsearch, how to achieve the function like this SQL: SELECT a, sum(b), sum(c), d FROM TableA WHERE a IN (1,2,3,4) AND d = 88 GROUP BY a HAVING sum(b) > 10 ORDER BY sum(b) desc, sum(c) asc LIMIT 10 OFFSET 5; So far I have only done this…
flowyi
  • 81
  • 1
  • 6
5
votes
1 answer

oracle sql select syntax with GROUP BY and HAVING clause

I been going thru some of the sql syntax to study for the oracle sql exam, I found something rather confusing based on the official references, the select syntax is as follow : SELECT [ hint ] [ { { DISTINCT | UNIQUE } | ALL } ] …
user2081775
  • 63
  • 1
  • 1
  • 3
5
votes
1 answer

How to construct a having statement in linq with a max value?

I have a collection of objects containing transactional data. This dataset has a 3 level parent-child relationship with the levels being: Project > Stage > Transaction. For each project and stage there will be multiple transactions. class…
codemonkeh
  • 2,054
  • 1
  • 20
  • 36
5
votes
5 answers

Group by two fields, and having count() on first field

I have a table that stored users play list, a video can be viewed by multiple users for multiple times. A records goes like this: videoid, userid, time 123, abc , 2013-09-11 It means user(abc) has watched video(123) on 2013-09-11 Now I want…
蒋艾伦
  • 464
  • 1
  • 5
  • 16
5
votes
3 answers

MySQL : Make HAVING select also "null"

I have two tables : dilemme and like. The first one contains articles and the second one contains votes. The script SELECT randomly one article to show and the user can vote (it's like and dislike). I want to make a query that shows only the…
rachids
  • 350
  • 1
  • 3
  • 11
5
votes
2 answers

SQL query mixing aggregated results and single values

I have a table with transactions. Each transaction has a transaction ID, and accounting period (AP), and a posting value (PV), as well as other fields. Some of the IDs are duplicated, usually because the transaction was done in error. To give an…
5
votes
2 answers

mysql group by having min

Below is the table data (a small piece) basically I'm looking to query just the rows with the minimum original_date_ctr when grouped by the accoutn number. I've tried using HAVING(MIN()), and where = Min() and other ways with no luck. The correct…
bbutle01
  • 521
  • 8
  • 18
4
votes
2 answers

SELECT From MySQL View With HAVING Clause Returns Empty Result Set

My business partner and I are having issues selecting from a MySQL view that has a HAVING clause. The query simply selects a few fields from the view, determines a distance dynamically with a few calculations, and aliases it as 'distance' - then…
4
votes
2 answers

Filter table to leave out specific rows

I have the table | WorkerID | ProjectName | Role | |----------|-------------|--------| | 1 | Test | Leader | |----------|-------------|--------| | 4 | Test | Worker | |----------|-------------|--------| | 2 | …
Luxman
  • 59
  • 4
4
votes
2 answers

Still confused by having and subqueries in MySQL

I'm trying to select products based on facets for a product category page. The basic structure is: Product_Facets -------------------------------- UID ProductID FacetID FacetOptionID 1 1 1 1 2…
Nick
  • 10,904
  • 10
  • 49
  • 78
4
votes
3 answers

Does MySQL use indexes on Having?

A portion of my query looks like: HAVING date > '2011-04-13 04:28:03' The date variable is indexed, does this have any effect on the query? EXPLAIN EXTENDED doesn't seem to be using the index, but I don't know if that's only because I have 4 rows…
Tony
  • 55
  • 2
  • 7