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

Detailed explanation of JSON.parse and JSON.stringify usage

Table of contents JSON.parse JSON.parse Syntax re...

Summary of constructor and super knowledge points in react components

1. Some tips on classes declared with class in re...

JS implements the rock-paper-scissors game

This article example shares the specific code of ...

Detailed analysis of MySQL optimization of like and = performance

introduction Most people who have used databases ...

React Hooks Common Use Scenarios (Summary)

Table of contents 1. State Hook 1. Basic usage 2....

Summary of the application of decorative elements in web design

<br />Preface: Before reading this tutorial,...

SQL serial number acquisition code example

This article mainly introduces the sql serial num...

MySQL 5.6.33 installation and configuration tutorial under Linux

This tutorial shares the installation and configu...

Solution to 404 Problem of Tomcat Installation in Docker

Find the containerID of tomcat and enter the toma...

Methods to enhance access control security in Linux kernel

background Some time ago, our project team was he...

MySQL scheduled task example tutorial

Preface Since MySQL 5.1.6, a very unique feature ...

Example code for implementing card waterfall layout with css3 column

This article introduces the sample code of CSS3 c...

Solution to the problem of passing values ​​between html pages

The first time I used the essay, I felt quite awkw...

Understanding MySQL precompilation in one article

1. Benefits of precompilation We have all used th...