# Select with an alias does not show alias column in "having"

**URL:** <https://www.ruby-forum.com/t/select-with-an-alias-does-not-show-alias-column-in-having/231872>\
**Category:** Rails\
**Created:** [September 3, 2014, 7:39pm UTC](https://www.ruby-forum.com/t/select-with-an-alias-does-not-show-alias-column-in-having/231872 "2014-09-03T19:39:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [September 3, 2014, 7:39pm UTC](https://www.ruby-forum.com/t/select-with-an-alias-does-not-show-alias-column-in-having/231872/1 "2014-09-03T19:39:38Z")

</div>

The Model Image has many Tags

When i run this query to get Images with tags that are named both bamboo  
in  
and Green i get an error that says the alias table is not present. What  
am  
I doing wrong?

Image.joins(:tags).select(“images.\*, count(tags.id) as  
‘tags\_count’”).where(tags:{name:[“bamboo”,“Green”]}).group(“images.id”).having(“tags\_count  
= 2”)

Error

_Image Load (0.4ms)_ SELECT images.\*, count(tags.id) as tags\_count FROM  
“images” INNER JOIN “image\_tags” ON “image\_tags”.“image\_id” =  
“images”.“id”  
INNER JOIN “tags” ON “tags”.“id” = “image\_tags”.“tag\_id” WHERE  
“tags”.“name” IN (‘bamboo’, ‘Green’) GROUP BY images.id HAVING  
tags\_count =  
2

PG::UndefinedColumn: ERROR: column “tags\_count” does not exist

LINE 1: … IN (‘bamboo’, ‘Green’) GROUP BY images.id HAVING  
tags\_count…

```
                                                         ^

```

: SELECT images.\*, count(tags.id) as tags\_count FROM “images” INNER JOIN  
“image\_tags” ON “image\_tags”.“image\_id” = “images”.“id” INNER JOIN  
“tags”  
ON “tags”.“id” = “image\_tags”.“tag\_id” WHERE “tags”.“name” IN (‘bamboo’,  
‘Green’) GROUP BY images.id HAVING tags\_count = 2

ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column  
“tags\_count” does not exist

LINE 1: … IN (‘bamboo’, ‘Green’) GROUP BY images.id HAVING  
tags\_count…

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [September 3, 2014, 8:16pm UTC](https://www.ruby-forum.com/t/select-with-an-alias-does-not-show-alias-column-in-having/231872/2 "2014-09-03T20:16:14Z")

</div>

Hi,

At Wed, 3 Sep 2014 10:38:13 -0700 (PDT), ruby user wrote:

> “images”.“id” INNER JOIN “tags” ON “tags”.“id” = “image\_tags”.“tag\_id” WHERE  
> “tags”.“name” IN (‘bamboo’, ‘Green’) GROUP BY images.id HAVING  
> tags\_count = 2
> 
> PG::UndefinedColumn: ERROR: column “tags\_count” does not exist
> 
> LINE 1: … IN (‘bamboo’, ‘Green’) GROUP BY images.id HAVING tags\_count…
> 
> ```
> ^
> 
> ```

Well, “doing it wrong” is not the phrase I would choose. In  
PostgreSQL, aliases are in a different scope than HAVING. HAVING may  
not access aliases, you have to repeat the aggregate (will be  
optimized to not be executed again). This is part of the SQL standard,  
AFAIK.

Best regards,
