List:General Discussion« Previous MessageNext Message »
From:Jay Blanchard Date:April 18 2006 2:41am
Subject:RE: How can I use a value computed in my SQL query for further computations?
View as plain text  
[snip]
Here is a paired down version of a query I want to make. How can I get
the
"grandtotal" column? I know about the "HAVING" clause, but that's only
going
to be good for weeding out rows I don't want. I just want to do some
basic
math here.

SELECT  a.*, 
	DATE_FORMAT(a.created_on,'%m/%d/%y %h:%i:%s %p') AS
created_on_format, 
	DATE_FORMAT(a.timestamp,'%m/%d/%y %h:%i:%s %p') AS
timestamp_format,

	(views * ppview) AS totalviews, 
	(clicks * ppclick) AS totalclicks,
	totalviews + totalclicks AS grandtotal
FROM advertisements a;

There has got to be a better way than this (which would be a colossal
waste
of computing power to recalculate something that was just done!):

SELECT  a.*, 
	DATE_FORMAT(a.created_on,'%m/%d/%y %h:%i:%s %p') AS
created_on_format, 
	DATE_FORMAT(a.timestamp,'%m/%d/%y %h:%i:%s %p') AS
timestamp_format,

	(views * ppview) AS totalviews, 
	(clicks * ppclick) AS totalclicks,
	((views * ppview) + (clicks * ppclick)) AS grandtotal
FROM advertisements a;
[/snip]

Use variables;

http://dev.mysql.com/doc/refman/5.1/en/set-statement.html
Thread
How can I use a value computed in my SQL query for further computations?Daevid Vincent18 Apr
RE: How can I use a value computed in my SQL query for further computations?Daevid Vincent18 Apr
RE: How can I use a value computed in my SQL query for further computations?Jay Blanchard18 Apr
RE: How can I use a value computed in my SQL query for further computations?Jay Blanchard18 Apr