NEKIBUR RAHMAN
NEKIBUR RAHMAN

Reputation: 212

having clause is not working in sql

I dont understand whats wrong in this statment.

  Select customername,LEN(address) 
  FROM customers group by customername having LEN(address) = 13;

This is the error message HAVING clause (LEN(address)=13) without grouping or aggregation.

Upvotes: 1

Views: 3449

Answers (2)

developer_hatch
developer_hatch

Reputation: 16214

Try it with the where clause

  Select customername,LEN(address) as lenadressnamecolumn
  FROM customers where LEN(address) = 13;

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1270993

Neither address nor LEN(address) is in the GROUP BY. So, you either need to add them or wrap the expressions in an aggregation function:

SELECT customername, MAX(LEN(address))
FROM customers 
GROUP BY customername 
HAVING MAX(LEN(address)) = 13;

Or if you just want customers with a length of 13, perhaps no aggregation is needed at all:

SELECT customername
FROM customers 
WHERE LEN(address) = 13;

Upvotes: 4

Related Questions