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

Sequelize.js : Sum with condition and group by

I'm a biginner using Sequelize. I spend hours trying to get the following code runs but I can't I quit some columns from the table for better understanding. Finance table I want to get this result: [ { 'month': 11, 'year': 2021, …
AleOjeda
  • 38
  • 5
2
votes
1 answer

Filtering multiple items in json array with sqlite json_each

I have an sqlite table with the following schema and data: CREATE TABLE Feeds ( id INTEGER PRIMARY KEY AUTOINCREMENT, groups JSON NOT NULL DEFAULT ('[]') ); INSERT INTO Feeds(groups) VALUES ('["fav1",…
Redark
  • 173
  • 1
  • 11
2
votes
1 answer

How to GROUP BY multiple columns with multiple HAVING values in MySQL?

I am trying to create a mysql query that looks at a table that stores search queries. The query is trying to get all rows that: >= to a date Only returning rows where the search query has 3 or more occurrences Also only returning rows where 2 or…
Adam B
  • 95
  • 11
2
votes
1 answer

MySQL error #1111 - Invalid use of group function

Yes, this is an assignment. So the task was to output two columns of 'first name' and 'last name' with conditions: -A u (B ∩ -C ∩ -(A ∩ -( B u D))) A: All consumers that didn't shop on Monday and Friday (time_by_day.the_day) B: All consumers who…
Skyblue
  • 23
  • 4
2
votes
1 answer

POSTGRESQL group by for interval

I have a query in which I have the time of light switches turned on in a flat, with switch on (start) time and switch off (end) time. I would to know how many light switches are on, does anyone know how can I do? Example query: Select Flat,…
2
votes
2 answers

SQL Server - select column using in having count()

This is my first question (and sorry for my English) I have this table in SQL Server: id_patient | date | id_drug ---------------------------------------------------- 1 20200101 A 1 20200102 …
Made
  • 21
  • 1
2
votes
2 answers

Condition in HAVING faster than same condition in WHERE?

I came across a query that had a condition in the HAVING section of the query that I would have typically placed in the WHERE section. I preceded to move the condition to the WHERE section and to my surprise the code runs about 200%+ longer using…
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
2
votes
2 answers

SQL filter query results based on analytic function

I'd like to find an efficient way to filter my RANK() OVER function in SQL. I have the following query: SELECT base.ITEM_SKU_NBR, RANK() OVER (ORDER BY SUM(base.NET_SLS_AMT) DESC) AS SLS_rank, RANK() OVER (ORDER BY COUNT(DISTINCT…
Muriel
  • 449
  • 4
  • 20
2
votes
1 answer

How to query mysql to select rows with non unique combination of two columns

I have the following table: | id | user_id | game_id | |----|---------|---------| | 83 | 1 | 1 | | 84 | 1 | 1 | | 85 | 1 | 1 | | 86 | 2 | 2 | | 87 | 2 | 2 | | 88 | 3 | 2 | | 89…
jwarz
  • 481
  • 2
  • 9
2
votes
3 answers

How do I filter a group-by query on the value of a coalesced column?

Here's my Query that does not work because it apparently violates the rules of the HAVING clause: SELECT COALESCE(Publisher.name, Affiliate.name) AS Publisher ,dbo.SumKeys(Item.id) AS ItemIDs FROM Item INNER JOIN…
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
2
votes
1 answer

How to filter a SQL Query using ROLLUP

This query: SELECT 1, 2, count(*) FROM t GROUP BY ROLLUP (1, 2) ORDER BY 1, 2 Shows: 1, 2 A Null 3 A Blue 2 A Neon 1 B NULL 2 B Navy 2 C NULL 4 C Neon 2 C Blue 2 You see the sums A = 3, B = 2, and C = 4? I want to filter to only show if the SUM…
Aron Schor
  • 27
  • 4
2
votes
2 answers

Alias in Group By clause - invalid identifier

I tried many times by many ways but I cannot resolve this... I'm performing an Oracle SQL query: SELECT TRUNC(months_between(sysdate, DateofBirth) / 12) AS "age" FROM players group by age HAVING COUNT (TRUNC(months_between(sysdate,…
2
votes
2 answers

HAVING clause properties

I need help with this exercise where I have to translate properties from English into sql. Questions: length has at least one 0 Answer: MIN(ABS(length)) = 0 length has more that one value Answer: MIN(length) < MAX(length) there are…
user640072
  • 161
  • 2
  • 2
  • 8
2
votes
2 answers

HAVING and GROUP BY

The problem I am trying to solve is as follows For each continent show the continent and number of countries with populations of at least 10 million The world table looks like this: World (name, continent, area, population, gdp) My query (which…
sql_learner
  • 537
  • 2
  • 8
2
votes
1 answer

2 count(*)+group by+having+join

I have tables: CREATE TABLE IF NOT EXISTS `un_pl` ( `coo` double DEFAULT NULL, `pl` varchar(255) DEFAULT NULL, `gal` varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `un_pl` (`coo`, `pl`, `gal`) VALUES …
Oleg Shevtsov
  • 73
  • 2
  • 12