Referencing aliased column in where clause

  • This is probably a really dumb question but I have never understood why this doesn't work.

    SELECT COUNT(*) AS total

    ,last_name

    ,first_name

    FROM account_contact

    WHERE account_id = '11997'

    AND active = 'Y'

    AND total > 1

    GROUP BY last_name

    ,first_name

    ORDER BY total DESC

    Why does referring to the aliased column in the where clause generate this error?

    "Invalid column name 'total'."

    Is there any way to add a where condition to an aggregate, aliased column?

    Thanks!

    Chris

  • The clauses of SQL have an order of evaluation, specified by the standards as follows:

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    column list (SELECT clause)

    This means that although it appears first, the column list is actually evaluated last and thus, its aliasing would not normally be visible to the earlier clauses.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • It is also worth noting that a column list alias can have the same name as any of the source columns. This works because it is an output attribute and is not seen by the references inside the SELECT statement.

    However, if the ORDER BY clause could see your [Total] alias, it might confuse it with a Total column from one of your source tables.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Yes, it's called the HAVING clause...

    SELECT COUNT(*) AS total

    ,last_name

    ,first_name

    FROM account_contact

    WHERE account_id = '11997'

    AND active = 'Y'

    HAVING count(*) > 1

    GROUP BY last_name

    ,first_name

    ORDER BY total DESC

    DAB

  • For what it's worth I agree that it's dumb that SQL operates like this...


    Cheers,

    Ben Sullins
    bensullins.com
    Beer is my primary key...

  • Thanks guys! This really is a great site.

  • SQLServerLifer (3/13/2008)


    Yes, it's called the HAVING clause...

    SELECT COUNT(*) AS total

    ,last_name

    ,first_name

    FROM account_contact

    WHERE account_id = '11997'

    AND active = 'Y'

    HAVING count(*) > 1

    GROUP BY last_name

    ,first_name

    ORDER BY total DESC

    DAB

    HAVING would have to move AFTER group by or this won't work..... As in:

    SELECT COUNT(*) AS total

    ,last_name

    ,first_name

    FROM account_contact

    WHERE account_id = '11997'

    AND active = 'Y'

    GROUP BY last_name

    ,first_name

    HAVING count(*) > 1

    ORDER BY total DESC

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • You can do this in SQL 2005. Where, Having and Order By can all use the column alias from the Select clause.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply