SQL implements addition, subtraction, multiplication and division operations on two adjacent rows of data after sorting the data in a certain order and grouping them by certain fields. Ideas: 1: First group and sort the table data and then label them with serial numbers 2: Add/subtract one to the tag field as needed Above code: select distinct a.phone,from_unixtime(cast(floor(a.ts/1000) as bigint),'yyyyMMdd HH:mm:ss'),cha from table a join ( select a.phone,a.ts,abs(a.ts-b.ts)/1000 cha from (select phone,ts,row_number() over (partition by phone order by ts ) rank from table) a left join ( select phone,ts,rank-1 as rank from (select phone,ts,row_number() over(partition by phone order by ts ) rank from table) a ) b on a.phone = b.phone and a.rank = b.rank ) b on a.phone = b.phone and a.ts = b.ts where a.phone is not null and a.phone<>''; The table data is as follows: The first column is phone, the second column is time ts, and you are required to calculate the time it takes for each two adjacent data of the same phone The result is as shown below: The third column is in seconds. Supplementary knowledge: SQL implements the current row equal to the sum of the previous two rows of data SQL implements a function similar to the Fibonacci sequence, that is, the current data is equal to the sum of the previous two data. See the example in this article for details. Original table: SQL statement (be familiar with the usage of JION ON here) result The above SQL to implement addition, subtraction, multiplication and division operations of two adjacent rows of data is all the content that the editor shares with you. I hope it can give you a reference, and I also hope that you will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed explanation of the latest IDEA process of quickly deploying and running Docker images
>>: Vue3 manual encapsulation pop-up box component message method
Table of contents JSON.parse JSON.parse Syntax re...
1. Some tips on classes declared with class in re...
This article example shares the specific code of ...
introduction Most people who have used databases ...
Table of contents 1. State Hook 1. Basic usage 2....
<br />Preface: Before reading this tutorial,...
This article mainly introduces the sql serial num...
Table of contents 1. Effect display 2. Enhanced v...
This tutorial shares the installation and configu...
Find the containerID of tomcat and enter the toma...
background Some time ago, our project team was he...
Preface Since MySQL 5.1.6, a very unique feature ...
This article introduces the sample code of CSS3 c...
The first time I used the essay, I felt quite awkw...
1. Benefits of precompilation We have all used th...