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!开发者_JAVA百科
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")
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
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.
精彩评论