sql >> Database >  >> RDS >> Mysql

Hoe maak ik een inverse geordende variabele in mijn sql

Uw volledige code is

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`;

Het idee hierachter is

db<>fiddle hier




  1. gebruik \set variabele binnen plpgsql declare block

  2. MySQL selecteer willekeurige rij met JOIN uit twee tabellen

  3. GROUP PER maand in het veld DATETIME

  4. Qt - Hoe Mysql-stuurprogramma te krijgen|compileren