Get the App
SLTechnology News&Howtos  ›  Database  › 

How to realize the combinatorial cumulative sorting of fields in the table in SQL

Shulou Source: shulou.com Published: 2022-05-31 19:33:37 10月02日 Update

This article introduces how to achieve the combination and cumulative sorting of the fields in the table in SQL, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

The table is as follows:

1 10 12 2 2 9 8 2 3 12 8 3 1 9 9 3 1 12 0 3 2 5 18 2 2 20 0 2

All three fields are of type integer.

The requirements are as follows:

1. Search by index=2 2. Add all the same id together, and add the data together. 3. The final presented data should be sorted by in+out, from large to small.

To meet the above requirements: the SQL statement is designed as follows (assuming the table name is tab)

The code is as follows:

Select id,sum (in) as in,sum (out) as out from tab where index=2 group by id order by in + out desc

SQL on how to achieve the combination of fields in the table cumulative sorting is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Fields sorting combinations content data more help good same three code interests guys buddies articles knowledge articles types statements references Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB NVidia vpn Redmi MySQL