Как создать обратную упорядоченную переменную в моем sql - PullRequest
0 голосов
/ 28 мая 2020

У меня есть столбец с текущей суммой, но я хотел бы изменить порядок в новом или таком же столбце

Мой запрос

SELECT date,
ant, 
num_room,
(@csum:= @csum + num_room) as cumulative_room
from xxx_xxxx_xxxx
WHERE date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
AND(select @csum := 0) = 0
order by date

Таблица Mi:

 date         | ant  | num_room  | cumulative_room
    28-04-2020       6       1              1  
    28-04-2020       3       1              2
    28-04-2020       4       1              3
    28-04-2020       7       1              4
    28-04-2020       4       1              5
    ....

Как я могу сделать другую переменную с обратным порядком из cumulative_room?

 date         | ant  | num_room  | cumulative_room |reverse_cumulative room
28-04-2020       6       1              1                5
28-04-2020       3       1              2                4
28-04-2020       4       1              3                3
28-04-2020       7       1              4                2
28-04-2020       4       1              5                1
    ....

1 Ответ

0 голосов
/ 28 мая 2020

Ваш полный код:

SELECT
t1.*
, t2.max_room - `cumulative_room` 'reverse_cumulative room'
FROm (SELECT date,
                ant, 
                num_room,
                (@csum:= @csum + num_room) as cumulative_room
                from xxx_xxxx_xxxx
                WHERE date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
                AND(select @csum := 0) = 0
                order by date) t1
INNER JOIN (SELECT MAX(`cumulative_room`) +1 max_room,  `date` FROM (SELECT date,
                                                                            ant, 
                                                                            num_room,
                                                                            (@csum:= @csum + num_room) as cumulative_room
                                                                            from xxx_xxxx_xxxx
                                                                            WHERE date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
                                                                            AND(select @csum := 0) = 0
                                                                            order by date) t3 GROUP BY  `date`) t2
ON t1.`date` = t2.`date`;

Идея:

CREATE TABLE xxx_xxxx_xxxx (
  `date` VARCHAR(10),
  `ant` INTEGER,
  `num_room` INTEGER,
  `cumulative_room` INTEGER
);

INSERT INTO xxx_xxxx_xxxx
  (`date`, `ant`, `num_room`, `cumulative_room`)
VALUES
  ('28-04-2020', '6', '1', '1'),
  ('28-04-2020', '3', '1', '2'),
  ('28-04-2020', '4', '1', '3'),
  ('28-04-2020', '7', '1', '4'),
  ('28-04-2020', '4', '1', '5');
✓

✓
SELECT
t1.*
,t2.max_room - `cumulative_room` 'reverse_cumulative room'
FROm xxx_xxxx_xxxx t1
INNER JOIN (SELECT MAX(`cumulative_room`) +1 max_room,  `date` FROM xxx_xxxx_xxxx GROUP BY  `date`) t2
ON t1.`date` = t2.`date`;
date       | ant | num_room | cumulative_room | reverse_cumulative room
:--------- | --: | -------: | --------------: | ----------------------:
28-04-2020 |   6 |        1 |               1 |                       5
28-04-2020 |   3 |        1 |               2 |                       4
28-04-2020 |   4 |        1 |               3 |                       3
28-04-2020 |   7 |        1 |               4 |                       2
28-04-2020 |   4 |        1 |               5 |                       1

db <> fiddle здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...