SQL statements are used to accumulate and sort fields in a table.
The table is as follows:
| Id |
In |
Out |
Index |
| 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 the integer type.
The requirements are as follows:
1. Search by index = 2
2. Combine the same and all IDs, and add the data together.
3. The final presented data should be sorted by in + out, from large to small
To meet the preceding requirements: the SQL statement is as follows (assuming the table name is tab)
Copy codeThe 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;
The above is all the content of the SQL statement that achieves the accumulative sorting of fields in the table. I hope to give you a reference, and I hope you can provide more support to the customer's house.