I have a query like
select user,
if(purchase_date='2012-06-10' - interval 1 day, revenue, 0),
if(purchase_date='2012-06-10' - interval 2 day, revenue, 0),
if(purchase_date='2012-06-10' - interval 3 day, revenue, 0)
from purchases
group by user;
And I want to change the date format of the columns to be like ‘Mon 6-10’ by doing something like
select user,
if(purchase_date='2012-06-10' - interval 1 day, revenue, 0) AS date_format('2012-06-10' - interval 1 day, '%M %D-%m')
if(purchase_date='2012-06-10' - interval 2 day, revenue, 0) AS date_format('2012-06-10' - interval 2 day, '%M %D-%m')
if(purchase_date='2012-06-10' - interval 3 day, revenue, 0) AS date_format('2012-06-10' - interval 3 day, '%M %D-%m')
But I get a SQL error saying
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near…’
Is it possible to do this?
I want the output to look like
user Mon 6-9 Sun 6-8 Sat 6-7
---- ------- ------- -------
1 23 34 65
4 26 21 65
11 21 65 0
Would you believe I have a solution ?
I really had to use my imagination on this one. I changed the query slightly to perform a SUM per day. Please follow along:
You essentially need to construct this query
from this one
First, let’s make some sample data:
After running this, I made this data:
Let’s run the first query I posted
Here is the second query I posted that will construct the headers
Now let’s form VOLTRON
Here are the two queries combined form the headers you need:
OK, whoppie I formed the query. But does it work ???
BEHOLD
So, it is possible. You just have use your imagination to make MySQL construct the query for you.