Reputation: 43
I am trying to find the count of obsv_dt which has less than million records
select obsv_dt,count(*) as c from table
group by obsv_dt
having c<1000000
order by c
is giving unable to resolve column 'c'. I get that 'c' is alias and this error is expected How can i get this working?
Upvotes: 0
Views: 43
Reputation: 175
Below is the precedence of sql clauses:
- FROM clause
- ON clause
- OUTER clause
- WHERE clause
- GROUP BY clause
- HAVING clause
- SELECT clause
- DISTINCT clause
- ORDER BY clause
- TOP clause
Since HAVING clause is evaluated prior to the SELECT clause it is unware of the aliases. Similarly for all clauses except for ORDER BY where we can include the alias for sorting the result set.
Upvotes: 0
Reputation: 18950
You cannot use the alias before it has been calculated; try:
select
obsv_dt,
count(obsv_dt) as c
from
table
group by obsv_dt
having count(obsv_dt) < 1000000
order by count(obsv_dt)
There is a subtle difference in using count(*) vs count(col). But often it does not matter. count(*) vs count(column-name) - which is more correct?
Upvotes: 1
Reputation: 12169
select obsv_dt,count(*) as c from table
group by obsv_dt
having count(*) <1000000
order by count(*)
Upvotes: 1