SQL implements addition, subtraction, multiplication and division operations on two adjacent rows of data

SQL implements addition, subtraction, multiplication and division operations on two adjacent rows of data

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:
  • MySQL data table partitioning strategy and advantages and disadvantages analysis
  • Mysql method to calculate the difference between two adjacent rows of a column
  • How to get adjacent data in MySql

<<:  Detailed explanation of the latest IDEA process of quickly deploying and running Docker images

>>:  Vue3 manual encapsulation pop-up box component message method

Recommend

Key points for writing content of HTML web page META tags

The META tag is an auxiliary tag in the head area...

VMware workstation 12 install Ubuntu 14.04 (64 bit)

1. Installation Environment Computer model: Lenov...

Four ways to modify the default CSS style of element-ui components in Vue

Table of contents Preface 1. Use global unified o...

How to set a fixed IP address for a VMware virtual machine (graphic tutorial)

1. Select Edit → Virtual Network Editor in the me...

Detailed explanation of the use of MySQL Online DDL

Table of contents text LOCK parameter ALGORITHM p...

MySQL transaction concepts and usage in-depth explanation

Table of contents The concept of affairs The stat...

Using CSS3 to achieve progress bar effect and dynamically add percentage

During the project, I started using the js reques...

Difference and principle analysis of Nginx forward and reverse proxy

1. The difference between forward proxy and rever...

MySQL advanced learning index advantages and disadvantages and rules of use

1. Advantages and Disadvantages of Indexes Advant...

Centos7 startup process and Nginx startup configuration in Systemd

Centos7 startup process: 1.post(Power-On-Self-Tes...

Example code of implementing starry sky animation with CSS3 advanced LESS

This article introduces the sample code of advanc...