C
C
CityzenUNDEAD2020-07-10 15:32:07
SQL
CityzenUNDEAD, 2020-07-10 15:32:07

Why doesn't this select work?

Hello everyone!
Tell me why this select does not work?

SELECT field1, count(field1), field2
  FROM [Table] 
  group by field1
  having count(field1)>1
  order by count(field1) desc

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Vodakov, 2020-07-10
@CityzenUNDEAD

What a strange request you have. try this:

SELECT field1, count(field1) as countf1, field2
  FROM [Table] 
  group by field1, field2
  having countf1>1
  order by field1 desc

R
Rsa97, 2020-07-10
@Rsa97

Since you did not provide the text of the error, I will vangue.
Field2 is not included in either GROUP BY or aggregate function. This means that there is an ambiguous choice, from which row, which has fallen into the group, to take this field.
According to the ANSI SQL standard, this use of the field is prohibited.

K
Konstantin Tsvetkov, 2020-07-10
@tsklab

SELECT field1, COUNT( field1 ), STRING_AGG( field2, ',' )
  FROM [Table] 
  GROUP BY field1
  HAVING COUNT( field1 ) > 1
  ORDER BY COUNT( field1 ) DESC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question