1

私は次のようなクエリを持っています

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;

そして、列の日付形式を「Mon 6-10」のように変更したい

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')

しかし、エラー 1064 (42000): You have an error in your SQL syntax; という SQL エラーが表示されます。near...' を使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルを確認してください。

これを行うことは可能ですか?

出力を次のようにしたい

user Mon 6-9 Sun 6-8 Sat 6-7
---- ------- ------- -------
   1      23     34       65
   4      26     21       65
  11      21     65        0
4

1 に答える 1

2

私に解決策があると思いますか?

私は本当にこれに私の想像力を使わなければなりませんでした。クエリを少し変更して、1日あたりのSUMを実行しました。従ってください:

基本的に、このクエリを作成する必要があります

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;

これから

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

まず、いくつかのサンプルデータを作成しましょう。

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;

これを実行した後、私はこのデータを作成しました:

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>

投稿した最初のクエリを実行してみましょう

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>

これが私が投稿した2番目のクエリで、ヘッダーを作成します

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>

それでは、VOLTRONを作成しましょう

必要なヘッダーを組み合わせた2つのクエリは次のとおりです。

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クエリを作成しました。しかし、それは機能しますか?

見よ

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>

だから、それは可能です。想像力を駆使して、MySQLにクエリを作成させます。

于 2012-06-26T21:51:35.387 に答える