ORACLE table query result transpose (fixed column)

 

1. Application scenario description


 For an editable grid, there are two design methods for such a grid database. Method 1: Correspond each column in the database table with each column in the grid; Method 2: Make an editable part for each cell The rows of the database table not only store data in the form of a vertical table, but also transpose the results of the vertical table by 90 degrees when querying.

Two ways to compare:

Method 1: It is consistent with the correspondence in GRID. It is more intuitive for database design. The rows in the grid correspond to the rows in the table one by one, and the background code is also easier to operate on the table; but one disadvantage is that the table structure is relatively rigid. If The requirements in the grid change, to add more editable cell columns, some reserved column fields need to be designed in advance in the table;

Method 2: It is not intuitive to correspond to the rows of the database table and the grid, but there is no problem with the expansion of the dynamic columns of the grid. When it is necessary to add fields to the grid, the database table does not need to be changed, only the front-end code and a small amount of changes are required. code behind.

 

2. Database Design



 Table TB0 is used as a template record table, TB1 is used as a classification table, TB2 is used as an editable part of the cell table, and each editable cell corresponds to a row.

WEIGHTsNAME corresponds to the GRID column name, and weightsval corresponds to the grid cell value.

Query all cell values ​​SQL:



 Transpose the above query result by 90 degrees, which is consistent with the row in GRID:



 The principle of conversion is to use group by to first group the id, and then filter the weightsname by the decode method. For the sum function, it is just to cooperate with the group by. It does not matter if it is replaced by max and min. After all, it is only for sum(1) , max(1), and min(1) are all 1.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326921832&siteId=291194637