Example of complex statistics using only SQL

    Just a while ago, I wanted to write an article about how I conduct statistics, not only exchanging data statistics techniques with my friends. In our general informatization projects, there are several large technical points:

    1. Background J2EE technical architecture. The actual change is not large. Most of the time, an architecture can be unchanged for many years, plus scaffolding code, so there is usually no difficulty, and it is impossible to further improve work efficiency.
    2. Front-end technology. The ones used are complex, mostly based on the jquery framework. Now try to use Bootstrap as much as possible, easyUi is also used, and ordinary jsp is also used. In most cases, it is best to use a set of technologies. Efficiency cannot be improved further.
    3. Business development and design, since each project is different in this aspect, in most cases it depends on personal ability, the key is to be able to use process processing technology well, analyze the main object table, from the comparison table, business basic table, business Data table for these relationships. These two aspects have been written analysis articles before.
    4. Statistics is an important function that customers usually use. Generally, I don't think this is technically important, so I don't pay much attention to it. Later, I saw that many statistics were very complicated, all of which were simple queries, and then returned complex objects to be processed in JVM. I spent a lot of time on it, and I felt it necessary to write about this aspect to help developers improve work efficiency. . Features: Almost all use pure SQL to implement complex statistical functions. . Commonly used sql functions are: group by, case when, group_concat(mysql)/listagg(oralce), decode, distinct, having, etc. 1. Introduce with a specific example table. The relationship between the tables is as follows:     1. A student table     2. A student's reward and punishment table

  

  






    3. Relationship description The student table is the master table, the reward and punishment situation is the slave table, and there is a FK that is the PK of the master table, which is STUDENT_ID. Simple. The slave table can also be multiple tables, such as the passing of each student's exam, the situation of each student's participation in class activities, and so on. A lot of statistical tables are a mix of these.

    Second, the first type of statistics, the characteristics of each student's classification statistics summary
    is that a row of the table is an object of the main table.
1.


2.


3.


4.


5. Multi-column statistics from


a table 6.


7. Multi -column tables A summary of one of the categories of the table object. 1. 2. 3. Use left join for statistics 4. 5. Process the statistics of multiple slave tables from multiple left joins 6. Without left join, I personally tried the statistical method. After the fields of the main table are combined with the INSTR function, they are used for statistics from the table.     Finally, let me mention the recently encountered demand, which is to randomly select from a group. It is said that there is a random function after the group. But no, my method is still group_concat or LISTAGG() after taking out this field, the external program uses ''' to divide it into an array, and then randomly takes a value from it. 4. Summary Some statistical classifications are obtained from the dictionary table, or directly use the dictionary table to splicing out long sql by looping in the jvm, and the sql that has been generated has more than 2000 characters. Try to use SQL as much as possible and make full use of the capabilities of the database itself. Although there are many, it is not messy, and it is very simple. Can save a lot of development time.



   
   






















   
   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326246008&siteId=291194637