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

Usage and principles of provide and inject in Vue3

Preface: When passing data between parent and chi...

How to explain TypeScript generics in a simple way

Table of contents Overview What are Generics Buil...

WEB standard web page structure

Whether it is the background image or the text siz...

Vue+thinkphp5.1+axios to realize file upload

This article shares with you how to use thinkphp5...

W3C Tutorial (14): W3C RDF and OWL Activities

RDF and OWL are two important semantic web techno...

MySQL installation tutorial under Windows with pictures and text

MySQL installation instructions MySQL is a relati...

Css3 realizes seamless scrolling and anti-shake

question The seamless scrolling of pictures and t...

How to use Docker-compose to build an ELK cluster

All the orchestration files and configuration fil...

Example of implementing text wrapping in html (mixed text and images in html)

1. Text around the image If we use the normal one...

How to understand SELinux under Linux

Table of contents 1. Introduction to SELinux 2. B...

Detailed explanation of the solution to Ubuntu dual system stuck when starting

Solution to Ubuntu dual system stuck when startin...

Object-Oriented Programming with XHTML and CSS

<br />If only XHTML and CSS were object-orie...

Let's learn about MySQL database

Table of contents 1. What is a database? 2. Class...

CentOS7 64-bit installation mysql graphic tutorial

Prerequisites for installing MySQL: Install CentO...