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

How to write SQLite code to show a specified code along with its associated codes from group_concat function?

I am trying to create a table where the code N09 is included, where a student was assigned a set of codes that contains N09, and "Status Complete" was yes. I wanted to use group_concat to see if each set contains N09. I saw a similar question to…
mq19745422
  • 73
  • 5
3
votes
1 answer

Expand a GROUP BY and HAVING resultset

Is there a way to expand/get all the records of a GROUP BY and HAVING query? For example, SELECT Column1, Column2, Column3, Count(*) as Count FROM table_name GROUP BY Column1, Column2, Column3 HAVING Count > '2' Is there an easier way to get all…
theking963
  • 2,223
  • 7
  • 28
  • 42
3
votes
2 answers

Can LAG be used with HAVING?

I distinctly recall that T-SQL will never let you mix LAG and WHERE. For example, SELECT FOO WHERE LAG(BAR) OVER (ORDER BY DATE) > 7 will never work. T-SQL will not run it no matter what you do. But does T-SQL ever let you mix LAG with…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
3
votes
2 answers

Selecting fields that are not in GROUP BY when nested SELECTS aren't allowed

I have the tables: Product(code (PK), pname, (....), sid (FK)), Supplier(sid(PK), sname, (....)) The assignment is: Find Suppliers that supply only one product. Display their name (sname) and product name (pname). It seem to me like a GROUP BY…
avivgood2
  • 227
  • 3
  • 19
3
votes
1 answer

Two extra columns with using HAVING in QoQ Coldfusion

When I use HAVING in my QoQ coldfusion, the query returned will have two extra column: "Column_7" and "Column_8" The original resultat And the resultat with two extra columns Here is my code var qEffectifTemp = queryExecute(" SELECT…
Li xiaoyu
  • 53
  • 5
3
votes
1 answer

MySql: Find nearest date for each product subtype within a type

I apologize if this has been asked before, but I couldn't find an identical problem in StackOverflow. I have a table named prices, like this, where type, subtype and date are primary keys: type subtype date price 18 | DFY | 2019-06-27…
dtyfet
  • 33
  • 4
3
votes
4 answers

Select rows where ColA=ColB and also add a new column that tells whether the original data in ColA is repeated

I am trying to figure out how to query a table (the table is actually a result set, so it will be a subquery), group it by ColA=ColB (see below), and create a calculated field all in one step. So, if my test data looks like ColA ColB ColC 1…
Shereif
  • 31
  • 1
3
votes
1 answer

Unknown column in 'having clause' in mysql 5.5

i try to run a query with having: SELECT `doctors`.*, ( SELECT GROUP_CONCAT(`areas`.`areaName` SEPARATOR ', ') FROM `areas_has_doctors` INNER JOIN `areas` ON…
kfir
  • 732
  • 10
  • 22
3
votes
2 answers

MySQL Group by a field based on the maximum value of another field IN THE GROUP (not in the table)

Consider the following table: un_id avl_id avl_date avl_status 1738 6377398 2011-03-10 unavailable 1738 6377399 2011-03-11 unavailable 1738 6377400 2011-03-12 unavailable 1738 6719067 2011-03-12 unavailable 1738 6719351…
Adam
  • 1,932
  • 2
  • 32
  • 57
3
votes
3 answers

SQL Query find users with only one product type

I solemnly swear I did my best to find an existing question, may I'm not sure how to phrase it correctly. I would like to return records for users that have quota for only one product type. | user_id | product | | 1 | A | | 1 | …
3
votes
2 answers

SQL - How to retrieve the result set containing all items of another element?

l'll explain the use-case simply with following scenario. Basically,i want to find all the pts/s from pts_table which contains all the orders of the pl001 pl_table ======== pl_id | order_id pl001 order001 pts_table …
3
votes
3 answers

SQL select rows containing empty field using GROUP BY, HAVING COUNT

What I'm trying to do is very similar to Select first row in each GROUP BY group? but other than select first row after ORDER BY number DESC, I want to select a row shows up multiple times in database (has changed name in past) AND empty field…
huza
  • 107
  • 6
3
votes
2 answers

Why AVG() give no result?

I try to select only dog with weight superior to the average weight of all dogs. I have this SQL query: SELECT nameAni, weigth FROM Animal WHERE idTpAni IN (SELECT idTpAni FROM TypeAnimal WHERE libTpAni = 'DOG') GROUP BY…
Zac
  • 31
  • 4
3
votes
1 answer

PostgreSQL HAVING clause

I posted on here last night looking for help with some homework. I'm down to my last question. Here's the relevant piece of the schema I am working with: CREATE TABLE votesOnPoll( user_id int, poll_id int, option_id int, voteDate date, …
Ganon11
  • 151
  • 6
3
votes
1 answer

How to use SELECT with GROUP BY and HAVING in a specific SQL case

I have an SQL database with one table: Product (maker, type, model) For each row we have a different maker (brand), a type of product(pc, tablet, laptop, etc.) and a model (1.1 / 2.0 / 3.4.5 etc) Q: How do I get the makers who produce only one…
Victor A
  • 63
  • 5