[英]getting the difference between two rows and perform mathematical operation on the difference mysql
例如,我的數據集如下所示
ID|Value
1|10
1|3
2|9
2|10
2|15
我需要找到相同 ID 的兩個連續元素之間的差異並存儲該值,然后再次找到差異並將存儲的值與具有相同 ID 的所有值的新差異相乘。 這必須一直持續到有相同 ID 的元素。 因此我的最終數據集應該看起來像
ID|Value
1|-7
2|5
有沒有人對我應該如何使用 sql 有任何建議
計算連續行之間的差異 -
MariaDB [sandbox]> SELECT T.*,
-> IF(T.TID <> @P ,@RN:=1,@RN:=@RN+1) RN,
-> IF(T.TID <> @P ,0, @PVAL) PVAL,
-> IF(T.TID <> @P ,0, T.VALUE * 1.00 - @PVAL) DIFF,
-> @P:=T.TID,
-> @PVAL:=T.VALUE
-> FROM(SELECT @RN:=0,@P:=0,@PVAL:=0.00,@DIFF:=0.00) RN,T
-> ORDER BY T.TID,T.dt;
+------------+------+-------+------+------+------+-----------+----------------+
| dt | TID | Value | RN | PVAL | DIFF | @P:=T.TID | @PVAL:=T.VALUE |
+------------+------+-------+------+------+------+-----------+----------------+
| 2017-01-01 | 1 | 10 | 1 | 0 | 0 | 1 | 10 |
| 2017-01-02 | 1 | 3 | 2 | 10 | -7 | 1 | 3 |
| 2017-01-01 | 2 | 9 | 1 | 0 | 0 | 2 | 9 |
| 2017-01-02 | 2 | 10 | 2 | 9 | 1 | 2 | 10 |
| 2017-01-03 | 2 | 15 | 3 | 10 | 5 | 2 | 15 |
+------------+------+-------+------+------+------+-----------+----------------+
5 rows in set (0.00 sec)
但是在 sql(據我所知的任何版本)中沒有聚合函數可以計算差異的乘積(如果這是正確的詞)。 更重要的是,您似乎想為每個 tid 丟棄第一行。 您可以通過 group_concatenating 差異到一個表中,然后調用一個存儲過程來啟動對動態 sql 的調用(順便說一下,您不能在存儲函數中使用動態 sql)。
這個存儲過程
drop procedure if exists f;
delimiter //
CREATE DEFINER=`root`@`localhost` procedure `F`(
instring varchar(200)
)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
begin
declare tempstring varchar(100);
declare outstring varchar(100);
declare tempid int;
declare checkit int;
declare maxrecs int;
set checkit = 0;
set maxrecs = (select count(*) from temp);
looper: while checkit < maxrecs do
select tid,calc into tempid,tempstring from temp limit checkit,1;
set checkit = checkit + 1;
if instr(tempstring,',') = 0 then
update temp
set rsult = tempstring;
else
set outstring = replace(tempstring,',','*');
set outstring = concat('update temp set rsult = (Select ',outstring,') where tid = ', tempid, ';');
set @sqlstmt = outstring;
#select checkit, maxrecs, outstring;
prepare sqlstmt from @sqlstmt;
execute sqlstmt;
deallocate prepare sqlstmt;
end if;
end while;
end //
delimiter ;
在此腳本中調用時會產生您想要的結果
*ariaDB [sandbox]> drop table if exists temp;
Query OK, 0 rows affected (0.09 sec)
MariaDB [sandbox]> create table temp as
-> select s.tid,group_concat(s.diff) calc, 1.00 as rsult
-> from
-> (
-> SELECT T.*,
-> IF(T.TID <> @P ,@RN:=1,@RN:=@RN+1) RN,
-> IF(T.TID <> @P ,0, @PVAL) PVAL,
-> IF(T.TID <> @P ,0, T.VALUE * 1.00 - @PVAL) DIFF,
-> @P:=T.TID,
-> @PVAL:=T.VALUE
-> FROM(SELECT @RN:=0,@P:=0,@PVAL:=0.00,@DIFF:=0.00) RN,T
-> ORDER BY T.TID,T.dt
-> ) s
-> where s.rn <> 1
-> group by s.tid;
Query OK, 2 rows affected (0.23 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [sandbox]>
MariaDB [sandbox]>
MariaDB [sandbox]> call f('1');
Query OK, 0 rows affected (0.07 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> select * from temp;
+------+-----------+-------+
| tid | calc | rsult |
+------+-----------+-------+
| 1 | -7.00 | -7.00 |
| 2 | 1.00,5.00 | 5.00 |
+------+-----------+-------+
2 rows in set (0.00 sec)*
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.