SQL daily exercises (15)

These two weeks have really been frightened by customers. I am a data analyst, doing more complicated tasks than programmers, holding the salary of a clerk, seeing the punch time of my daily work, I feel that I have no desire to survive. I really do n’t know what the picture is. Quickly understand the business, immediately build the database table, write the background sql, and then do the front-end web board, and also write small programs for the business, help them deal with Excel, and crawl data for them .. . Does Nima want me to be versatile? Especially spitting waves, data collection, RPA they use, automated ERP without interfaces, I think it is very powerful, but just operate the browser, crawl the data, and mess around It ’s really annoying. I still have to write according to the process to get online ... I do n’t appreciate it. I feel a little low. I have a feeling that it ’s like I ’ve graduated from university for 2 years, and then suddenly told me that I ’m not qualified, Read it from the beginning of the freshman year ... Isn't this a retrograde step? I certainly don't accept retrogression, I would rather stop, and never retrogress.

Data collection-> Data cleaning-> Database table creation-> Business logic sql-> Business front-end web board-> Additional programming support ...

Although I can do it, I just do n’t want to do it. There is a saying that strongly agrees, Where there is a will, there is a way . It ’s just a person, just me. If I want to do something, I will always try to find a way; if I do n’t want to do it, I will do everything possible to find an excuse. I need an excuse now, no, many excuses ...

After vomiting, I still continue to practice sql. Although I wrote much more complicated than these notes in my work, but look carefully, the principles are similar. Just practice, just like typing, proficient until you forget how to type

Table relationship

Requirement 01

Query all students' courses and scores

analysis

Case when to turn rows into columns. SQL I have written routines, first is to join the table involved, and finally come to the field, what is there.

select
from score as a
-- 匹配上课程名称, 学生信息
inner join course as b 
  on a.c_id = b.c_id 
inner join student as c 
  on a.s_id = c.s_i

Then, after being assembled into a large table, let's process the fields, including field filtering, field cleaning (function), condition filtering, grouping aggregation, sorting and other operations.

This is to use the student number and course number to group by, score also come to group by

mysql> select
    ->    s_id,
    ->    c_id,
    ->    score
    -> from score
    -> group by s_id, c_id, score;
+------+------+-------+
| s_id | c_id | score |
+------+------+-------+
| 0001 | 0001 |    80 |
| 0001 | 0002 |    90 |
| 0001 | 0003 |    99 |
| 0002 | 0002 |    60 |
| 0002 | 0003 |    80 |
| 0003 | 0001 |    80 |
| 0003 | 0002 |    80 |
| 0003 | 0003 |    80 |
+------+------+-------+
8 rows in set (0.00 sec)

Still repeating, group by is the most critical point. The fields in select must appear in group by, or aggregate functions, otherwise it will cause ambiguity. Then, in fact, the results we want to see should be It is the kind of expansion, it is necessary to display the courses corresponding to c_id from row to column field to show. That is, case when c_id can be.

select
  a.s_id as 学号,
  a.c_id as 课程号,
  c.s_name as 姓名,

  -- 将课程的行转为列,用 case when 的方式
  max(case when b.c_name="语文" then a.score else null end) as 语文,
  max(case when b.c_name="数学" then a.score else null end) as 数学,
  max(case when b.c_name="英语" then a.score else null end) as 英语

from score as a
-- 匹配上课程名称, 学生信息
inner join course as b 
  on a.c_id = b.c_id 
inner join student as c 
  on a.s_id = c.s_id

group by c.s_id, c.s_name

+--------+-----------+-----------+--------+--------+--------+
| 学号   | 课程号    | 姓名      | 语文   | 数学   | 英语   |
+--------+-----------+-----------+--------+--------+--------+
| 0001   | 0001      | 王二      |     80 |     90 |     99 |
| 0002   | 0002      | 星落      |   NULL |     60 |     80 |
| 0003   | 0001      | 胡小适    |     80 |     80 |     80 |
+--------+-----------+-----------+--------+--------+--------+
3 rows in set (0.00 sec)

I can easily write it now, because I have followed it on the Internet before. Which one can make a coincidence? Sure enough, remember again. As Ebbinghaus said, the best way to remember is repeat.

I do n’t want to practice, I think this case when is particularly powerful. And I still use a regular function and replace today, and I gradually feel a little bit about SQL, just like programming. I always feel that programming is simple, SQL is difficult , But the little friends don't agree. No matter what, it will do. Just practice more, I think.

Guess you like

Origin www.cnblogs.com/chenjieyouge/p/12757774.html