[SQL] according to two information, the integration of two tables of data

Two tables of data follows:

- 2017

College Score the above mentioned id
A001 Peking 670
A002 Renmin University of China 646
A003 Tsinghua 664
A003 Tsinghua University (directional)
A004 615 Beijing Jiaotong University,
A004 Beijing Jiaotong University (Sino-foreign cooperation in running schools)
A005 Beijing University of Technology
A005 Beijing University of Technology (Sino-foreign cooperation in running schools)
- - 2018

College Score the above mentioned id
A001 Peking 680
A002 Renmin University of China 662
A003 Tsinghua University 671
(Colleges specific requirements) A003 Tsinghua
A004 Beijing Jiaotong University, 634
A004 Beijing Jiaotong University (Sino-foreign cooperation in running schools)
A005 Beijing University of Technology
A005 Beijing University of Technology (Sino-foreign cooperation school)
A006 Beijing University of Aeronautics and Astronautics 640
A007 Beijing Institute of Technology 636
A007 Beijing Institute of Technology (Sino-foreign cooperative education)
A008 Beijing University of Science and Technology 632
Y007 Beijing Institute of Technology 621

Demand, the new table four, id college, s2017, s2018 two tables together, according to id, college

Related statements as follows:

. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
- Create 2017/2018 Table
create table score2017 (id VARCHAR2 (10), College VARCHAR2 (60), S2017 int);
Create table score2018 (ID VARCHAR2 (10), College VARCHAR2 (60), S2018 int);
- create a collection table
create table score1718 (id varchar2 (10 ), college varchar2 (60), s2017 int, s2018 int); Zhengzhou Women's Hospital: http://jbk.39.net/yiyuanzaixian/sysdfkyy/
- 2017 Chronology delete duplicate school and the above mentioned id
delete from score2017 where replace (college, '', '') = ' Guangxi University (professional and volunteer)'; --5 line
delete from score2017 where replace (college, '', '') = ' Hebei Normal University ( professional volunteer) '; --2 line
- the same data table 1138 is inserted into two rows
insert into score1718 select a.id, replace ( a.college,' ',' '), a.s2017, b.s2018 from score2017 a , B WHERE score2018 Replace (a.college, '', '') = Replace (b.college, '', '') and a.id = b.id
SELECT COUNT (College) from score1718; --1,138 repeated line data
- 2017 chronology insert 80 rows of data are not the same
insert into score1718 value (id, college , s2017) select a.id, replace (a.college, '', ''), a.s2017 from score2017 a where replace (a.college, '', '') not in (SELECT Replace (a.college, '', '') from score2017 A, B WHERE score2018 Replace (a.college, '', '') = Replace (b.college, '', ''))
- insert 2018 chronology data 134 is not the same row
insert into score1718 value (id, college , s2018) select b.id, replace (b.college, '', ''), b.s2018 from score2018 b where replace (b.college , '', '') not in (select replace (a.college, '', '') from score2017 a, score2018 b where replace (a.college, '', '') = replace (b.college, ' ',' '))
- 2018 is inserted into the same school chronology same id data of 8 lines are not
insert into score1718 value (id, college , s2018) select id, college, s2018 from score2018 b where b.college in (select college from score2018 group by college having count (*)> 1) and b.id not in (select a A score2017 from .id, score2018 B WHERE Replace (a.college, '', '') = Replace (b.college, '', '') and a.id = b.id)
- comparative data
select count ( college) from score2017; --1218 row
SELECT COUNT (College) from score2018; --1 280 + 134 + 1138 =. 8
- the total data collection table 1360 row
select count (college) from score1718; --1360 = 1138 + 134 + 80 + 8 lines
- column type is added, extraction field
ALTER Table score1718 the Add (CollegeType VARCHAR2 (40));
Update score1718 SET CollegeType = 'normal' college not like '% (% )%' where; --982 line
update score1718 set CollegeType = substr (college , instr (college, '(') + 1, instr (college, ')') - instr (college, '(') - 1) where college like '% (%)%' ; --378 line
the SELECT the above mentioned id, College, CollegeType from score1718;
- as shown in the following results:
the above mentioned id College S2017 S2018 CollegeType
A650 Sichuan International Studies University 582 608 ordinary
A651 southwestern University of Finance 619 638 ordinary
612 627 ordinary A652 Southwest University of Political
A652 southwest of Political Science University (Sino-foreign cooperation in running schools) 599 624 Sino-foreign cooperation in running schools
A653 Chengdu Institute of Physical education 540 ordinary
A655 Sichuan Academy of Fine Arts 549 570 ordinary
A656 Southwest University for Nationalities 556 582 ordinary
A657 Guizhou Normal University 586 601
A660 Medical University, Guizhou Normal

Guess you like

Origin blog.51cto.com/14337203/2403638