Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8183515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:11:04+00:00 2026-06-07T01:11:04+00:00

I have a query like select user, if(purchase_date=’2012-06-10′ – interval 1 day, revenue, 0),

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:11:05+00:00Added an answer on June 7, 2026 at 1:11 am

    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

    select user,
    SUM(if(purchase_date=DATE(NOW()) - interval 1 day, revenue, 0)) sum1,
    SUM(if(purchase_date=DATE(NOW()) - interval 2 day, revenue, 0)) sum2,  
    SUM(if(purchase_date=DATE(NOW()) - interval 3 day, revenue, 0)) sum3
    from purchases
    GROUP BY user;
    

    from this one

    select * from 
    (select dt,DATE_FORMAT(dt,'%a %c-%e') dh FROM
    (select distinct purchase_date dt from purchases) dthdrs) DateHeaders;
    

    First, let’s make some sample data:

    drop database if exists idris;
    create database idris;
    use idris
    create table purchases
    (
        id int not null auto_increment,
        user int,
        purchase_date date,
        revenue int,
        primary key (id)
    ) ENGINE=MyISAM;
    create table dummydata like purchases;
    alter table dummydata drop column id;
    insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
    update dummydata set purchase_date = purchase_date - interval 1 day;
    insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
    update dummydata set purchase_date = purchase_date - interval 1 day;
    insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
    update dummydata set purchase_date = purchase_date - interval 1 day;
    insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
    update dummydata set user=4;
    insert into purchases (user,purchase_date,revenue) select user,purchase_date,revenue from dummydata;
    delete from dummydata;
    insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
    update dummydata set purchase_date = purchase_date - interval 1 day;
    insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
    update dummydata set purchase_date = purchase_date - interval 1 day;
    insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
    update dummydata set purchase_date = purchase_date - interval 1 day;
    insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
    update dummydata set user=11;
    insert into purchases (user,purchase_date,revenue) select user,purchase_date,revenue from dummydata;
    delete from dummydata;
    insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
    update dummydata set purchase_date = purchase_date - interval 1 day;
    insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
    update dummydata set purchase_date = purchase_date - interval 1 day;
    insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
    update dummydata set purchase_date = purchase_date - interval 1 day;
    insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
    update dummydata set user=1;
    insert into purchases (user,purchase_date,revenue) select user,purchase_date,revenue from dummydata;
    delete from dummydata;
    select * from purchases;
    

    After running this, I made this data:

    mysql> select * from purchases;
    +----+------+---------------+---------+
    | id | user | purchase_date | revenue |
    +----+------+---------------+---------+
    |  1 |    4 | 2012-06-23    |      45 |
    |  2 |    4 | 2012-06-24    |      18 |
    |  3 |    4 | 2012-06-25    |      55 |
    |  4 |    4 | 2012-06-26    |      20 |
    |  5 |   11 | 2012-06-23    |      39 |
    |  6 |   11 | 2012-06-24    |      32 |
    |  7 |   11 | 2012-06-25    |      44 |
    |  8 |   11 | 2012-06-26    |      26 |
    |  9 |    1 | 2012-06-23    |      99 |
    | 10 |    1 | 2012-06-24    |      17 |
    | 11 |    1 | 2012-06-25    |      88 |
    | 12 |    1 | 2012-06-26    |      91 |
    +----+------+---------------+---------+
    12 rows in set (0.00 sec)
    
    mysql>
    

    Let’s run the first query I posted

    mysql>  select user,
        -> SUM(if(purchase_date=DATE(NOW()) - interval 1 day, revenue, 0)) sum1,
        -> SUM(if(purchase_date=DATE(NOW()) - interval 2 day, revenue, 0)) sum2,
        -> SUM(if(purchase_date=DATE(NOW()) - interval 3 day, revenue, 0)) sum3
        -> from purchases
        -> GROUP BY user;
    +------+------+------+------+
    | user | sum1 | sum2 | sum3 |
    +------+------+------+------+
    |    1 |   88 |   17 |   99 |
    |    4 |   55 |   18 |   45 |
    |   11 |   44 |   32 |   39 |
    +------+------+------+------+
    3 rows in set (0.00 sec)
    
    mysql>
    

    Here is the second query I posted that will construct the headers

    mysql> select * from
        -> (select dt,DATE_FORMAT(dt,'%a %c-%e') dh FROM
        -> (select distinct purchase_date dt from purchases) dthdrs) DateHeaders;
    +------------+----------+
    | dt         | dh       |
    +------------+----------+
    | 2012-06-23 | Sat 6-23 |
    | 2012-06-24 | Sun 6-24 |
    | 2012-06-25 | Mon 6-25 |
    | 2012-06-26 | Tue 6-26 |
    +------------+----------+
    4 rows in set (0.00 sec)
    
    mysql>
    

    Now let’s form VOLTRON

    Here are the two queries combined form the headers you need:

    mysql> select CONCAT('select user,',rv,' from purchases GROUP BY user')
        -> INTO @voltron from
        -> (select GROUP_CONCAT(CONCAT('SUM(if(purchase_date=''',dt,''',revenue,0)) as "',dh,'"')) rv
        -> from (select dt,DATE_FORMAT(dt,'%a %c-%e') dh FROM
        -> (select distinct purchase_date dt from purchases) dthdrs) DateHeaders) A;
    Query OK, 1 row affected (0.01 sec)
    mysql> select @voltron\G
    *************************** 1. row ***************************
    @voltron: select user,SUM(if(purchase_date='2012-06-23',revenue,0)) as "Sat 6-23",SUM(if(purchase_date='2012-06-24',revenue,0)) as "Sun 6-24",SUM(if(purchase_date='2012-06-25',revenue,0)) as "Mon 6-25",SUM(if(purchase_date='2012-06-26',revenue,0)) as "Tue 6-26" from purchases GROUP BY user
    1 row in set (0.00 sec)
    
    mysql>
    

    OK, whoppie I formed the query. But does it work ???

    BEHOLD

    mysql> PREPARE s1 FROM @voltron;
    Query OK, 0 rows affected (0.00 sec)
    Statement prepared
    
    mysql> EXECUTE s1;
    +------+----------+----------+----------+----------+
    | user | Sat 6-23 | Sun 6-24 | Mon 6-25 | Tue 6-26 |
    +------+----------+----------+----------+----------+
    |    1 |       99 |       17 |       88 |       91 |
    |    4 |       45 |       18 |       55 |       20 |
    |   11 |       39 |       32 |       44 |       26 |
    +------+----------+----------+----------+----------+
    3 rows in set (0.00 sec)
    
    mysql> DEALLOCATE PREPARE s1;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql>
    

    So, it is possible. You just have use your imagination to make MySQL construct the query for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query like this SELECT user_id FROM user WHERE user_id NOT IN
I have a query like so: SELECT User.id, 10*10 as distance FROM USERS INNER
In T-SQL you could have a query like: SELECT * FROM Users WHERE User_Rights
I have query like this : SELECT EXTRACT(MONTH FROM d.mydate) AS synmonth, SUM(apcp) AS
I have a query like: SELECT ... FROM ... WHERE .. AND SomeID =
I have an query like: SELECT id as OfferId FROM offers WHERE concat(partycode, connectioncode)
I have a query like SELECT * FROM myTable WHERE key LIKE 'XYZ' The
I have a query like this: select empno,name from emp where job = 'CLERK'
I have a query like so : SELECT * FROM table WHERE premium =
I have a query like this: SELECT COUNT(*) AS amount FROM daily_individual_tracking WHERE sales

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.