Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic alias 2

Status
Not open for further replies.

mcowen

Programmer
Oct 21, 2001
134
0
0
GB
Hi

I'm trying to output the previous 12 months and have the month name as the column heading. It doesnt want to let me use a function as the alias.

Code:
SELECT Department, 
SUM(case when Month=MONTH((CURDATE() - INTERVAL 11 MONTH)) then Total else '0' end) AS DATE_FORMAT((CURDATE() - INTERVAL 11 MONTH), '%M-%Y'),

Does anyone have any ideas how I can do this please?

Thanks

Matt
 
Are you trying to do something like this?:
[tt]
SELECT
Department,
FORMAT(month,'%M%Y') m,
SUM(
case
when Month=MONTH((CURDATE() - INTERVAL 11 MONTH))
then Total
else 0
end
)
FROM tblname
GROUP BY month
[/tt]
 
Yes thats right but I'm also trying to get the alias to be the month i.e. a table with August 2005 to July 2006 in the column headings. It throws an error when I try and use the

AS DATE_FORMAT((CURDATE() - INTERVAL 11 MONTH), '%M-%Y')



Matt
 
mcowen:
The only way you can achieve that is to assemble your query using program code, so that the alias is already defined before passing the query on to MySQL.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top