Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
DOlivastro
Starting Member
41 Posts |
Posted - 2006-03-24 : 16:31:49
|
| It seems that I can't use an alias name in a WHERE clause, but I can in a ORDER BY clause. For example, this is wrongSelect min (Name) as First_Namefrom T_Employeesgroup by Namewhere First_Name = 'Dominic'order by First_NameI have to change the where clause towhere min (Name) = 'Dominic'Is there any reason for this? It is especially provoking whent the where clause needs to repeat a full "CASE WHEN" statement.Dom |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-03-24 : 21:22:58
|
| Just something you have to remember (should be having not where).You can think of it as executing the having clause before generating the resultset so the alias isn't available whereas the order by is executed on the result so the alias is available.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2006-03-27 : 07:33:46
|
| alternative version....(I think this should work)Select min (Name) as First_Namefrom T_Employeeswhere First_Name = 'Dominic' <---moved heregroup by Nameorder by First_Name |
 |
|
|
pootle_flump
1064 Posts |
Posted - 2006-03-27 : 08:00:56
|
quote: Originally posted by DOlivastroIt is especially provoking whent the where clause needs to repeat a full "CASE WHEN" statement.
Hi DomIf you have loads of logic you don't fancy repeating in several clauses you can select from a derived table instead.For example imagine this with one of your long case statements instead of an aggregate:SELECT First_NameFROM (Select min (Name) as First_Namefrom T_Employeesgroup by Name) AS DerivedTWHERE First_Name = 'Dominic' HTHBTW Andrew - I'm afraid your query wouldn't work. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-03-27 : 08:44:57
|
quote: Originally posted by AndrewMurphy alternative version....(I think this should work)Select min (Name) as First_Namefrom T_Employeeswhere First_Name = 'Dominic' <---moved heregroup by Nameorder by First_Name
That wont work until you use Derived tableMadhivananFailing to plan is Planning to fail |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-27 : 09:01:01
|
[code]select 'Dominic' as First_Name[/code] KHChoice is an illusion, created between those with power, and those without.Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant |
 |
|
|
DOlivastro
Starting Member
41 Posts |
Posted - 2006-03-27 : 10:55:34
|
| Pootle_Flump had the best answer for my purposes. Thanks for that.Khtan, I hope you understand that I was just using a quick example. Perhaps not a very good one (as you noted), but my point can be made by other queries.I guess I'm still not sure WHY we have to go through derived tables. But I guess NR made the correct point -- you can only use an alias in clauses that are interpreted after the result set returns.Again, thanks to everyone.Dom |
 |
|
|
|
|
|
|
|