tags:

views:

39

answers:

2

hi, is it possible to alias an aggregate function in a select clause as AliasTable.AliasColumn? The reason is because I want minimum modifications in my arrays (I'm using PHP). Thanks!

P.S. I'm using SQl Server 2008

P.S.#2 Example: SELECT MAX(Person.name) name, MAX(Person.address) address, Farm.id, FROM Farm LEFT JOIN Person on Person.farm_id = Farm.id GROUP BY Person.name

could I alias name as Person.name and address as Person.address (it doesn't have to be "Person")

A: 

If you are saying something like select t.a, t.b, sum(a) as c from table1 t group by t.a, t.b then it is possible. Otherwise, you can submit your SQL query here.

Kangkan
+1  A: 

You can create an SQL View containing the select statement with the aggregate function as an alias. That way, you won't have any problems with it in your PHP code, because you can use the SQL View as a table.

Sample SQL statement for creating the view:

CREATE VIEW FarmView
AS
SELECT MAX(Person.name) name, MAX(Person.address) address, Farm.id, 
FROM Farm LEFT JOIN Person ON Person.farm_id = Farm.id 
GROUP BY Person.name

Sample SQL statement for using the view:

SELECT name, address, id 
FROM FarmView
ORDER BY name
Prutswonder
could you please cite an example on how to do this? Thanks!
bakerjr
Added some samples for you based on your query.
Prutswonder