Existence judgment problem in structured data

1. Existence detection of foreign key mapping

In the two tables, find records based on the existence of the foreign key mapping.

[Example 1] Calculate the average score of a class of boys. The transcripts and student tables are as follows:

..

[Question-solving ideas]

When selecting data from the score table, it is judged whether there is a record that the name of the class is a class and the gender of the student is male, and if it exists, it is selected.

[SPL script]


A B
1 =connect("db") /Connect to the database
2 =A1.query("select * from Score") /Query student table
3 =A1.query("select * from   Student") /Query student transcript
4 =A3.select(Class=="Class 1"   && Gender=="Male") /Select a class of boys
5 =A2.join@i(Class:StudentID, A4:Class:ID) /Use function A.join@i() to connect and filter
6 =A5.groups(StudentID; avg(Score):Score) / Group and summarize the average score of each student

The results of A6 are as follows:

StudentID Score
1 76
3 74

 

When the amount of data in the foreign key table is large, you can use the orderly merge of the cursor to solve it.

[Example 2] Query the number of orders that did not use discounts in 2014. The order table and order detail table are as follows:

..

[Question-solving ideas]

When selecting data from the order table, judge whether there is an order with a discount of 0, and select it if it exists.

[SPL script]


A B
1 =connect("db") /Connect to the database
2 =A1.cursor("select * from Order where year(Date)=2014   order by ID") /Create the order table cursor, select the 2014 record
3 =A1.cursor("select * from Detail order   by ID") /Create an order list cursor
4 =A3.select(Discount==0) /Select records where discounts are not used
5 =joinx(A2:Order,ID;A4:Detail,ID) /Use the function joinx to orderly merge the cursors of the order table and the order detail table
6 =A5.groups(month(Order.Date):Month;   icount(Order.ID):OrderCount) /Group and summarize the number of orders per month

The results of A6 are as follows:

Month OrderCount
1 16
2 25

 

2. Existence detection of non-equivalent connection

In a table, look up data by detecting the existence of non-equivalent connections.

[Example 3] Query the sales of orders with the same order spanning more than one year. Part of the order form data is as follows:

ID NUMBER AMOUNT DELIVERDATE ARRIVALDATE
10814 1 408.0 2014/01/05 2014/04/18
10814 2 204.0 2014/02/21 2014/04/05
10814 3 102.0 2014/03/14 2014/04/06
10814 4 102.0 2014/04/09 2014/04/27
10814 5 102.0 2014/05/04 2014/07/04
10848 1 873.0 2014/01/06 2014/04/21

 

[Question-solving ideas]

When selecting data from the order table, it is judged whether there are records with an order span of more than one year, and if so, it is selected.

[SPL script]


A B
1 =connect("db") /Connect to the database
2 =A1.query("select * from Detail") /Query order details
3 =A2.group(ID) /Group by order date
4 =A3.select(interval(~.min(DELIVERDATE),   ~.max(ARRIVALDATE)) > 365) /Select the records where the time interval of the same order exceeds 365 days
5 =A4.new(ID, ~.sum(AMOUNT):Amount) /Create a data table to count the sales of each order

The results of A 5 are as follows:

ID Amount
10998 6800.0
11013 4560.0
11032 20615.0

 

3. Non-existence detection of foreign key mapping

In the two tables, find records based on the non-existence of foreign key mapping.

【例 4】查询所有科目均高于 80 分的学生。成绩表和学生表如下:

..

【解题思路】

从学生表选出数据时,判断学生是否存在任意科目低于 80 分的成绩,如果不存在则选出。

【SPL 脚本】


A B
1 =connect("db") /连接数据库
2 =A1.query("select * from   Student") /查询学生表
3 =A1.query("select * from Score") /查询成绩表
4 =A3.select(Score<=80) /选出成绩不高于 80 分的记录
5 =A4.id(StudentID) /按学生 ID 去重
6 =A2.join@d(ID, A5) /使用函数 A.join@d() 选出不匹配的记录

A6的执行结果如下:

ID Class Name
2 Class   1 Ashley
16 Class   2 Alexis

 

4. 双重否定的存在性检测

通过双重否定,查询能够匹配的记录。

【例 5】 查询选修了所有课程的学生。选课表、课程表和学生表如下:

..

【解题思路】

从学生表选出数据时,判断学生是否存在某一科目课程没有选出的记录,如果不存在则选出。处理双重否定的存在性检测时,我们也可以正向思考,只要选出选修科目数量与所有科目数量相同的记录即可。

【SPL 脚本】


A B
1 =connect("db") /连接数据库
2 =A1.query("select * from   Student") /查询学生表
3 =A1.query("select * from Course") /查询课程表
4 =A1.query("select * from   SelectCourse") /查询选课表
5 =A4.groups(StudentID;   icount(CourseID):CourseCount) /选课表按照学生 ID 分组汇总每个学生的选课数量
6 =A5.select(CourseCount==A3.len()) /选出选择了所有课程的学生 ID
7 =A2.join@i(ID, A6:StudentID) /使用函数 A.join@i() 连接过滤

A7的执行结果如下:

ID Name Class
4 Emily   Smith Class   1

 

5. 任意条件的存在性检测

在两个表中,根据任意条件的存在性检测查找记录。

【例 6】 查询两科分数差超过 30 分的学生。成绩表和学生表如下:

..

【SQL 语句】

从学生表选出数据时,判断是否存在有任意两个科目成绩相差 30 分的记录,存在则选出。

【SPL 脚本】


A B
1 =connect("db") /连接数据库
2 =A1.query("select * from   Student") /查询学生表
3 =A1.query("select * from Score") /查询成绩表
4 =A3.group(StudentID) /成绩表按学生 ID 分组
5 =A4.select(~.max(Score)-~.min(Score)>30) /选出最高分和最低分相差超过 30 分的学生
6 =A5.id(StudentID) /按学生 ID 去重
7 =A2.join@i(ID,A6) /使用函数 A.join@i() 连接过滤

A7的执行结果如下:

ID Name Class
4 Emily   Smith Class   1
8 Megan Class   1

 

6. 全部条件的存在性检测

根据一个表中数据,筛选出满足所有条件的记录。

【例 7】查询哪些员工的工资比所有销售部员工都要高。员工表部分数据如下:

ID NAME DEPT SALARY
1 Rebecca R&D 7000
2 Ashley Finance 11000
3 Rachel Sales 9000
4 Emily HR 7000
5 Ashley R&D 16000

 

【SQL 语句】

从员工表选出数据时,判断员工工资大于所有销售部员工工资的记录是否存在,存在则选出。

【SPL 脚本】


A B
1 =connect("db") /连接数据库
2 =A1.query("select * from   Employee") /查询员工表
3 =A2.select(DEPT:"Sales").max(SALARY) /选出所在城市包含在一线城市中的记录
4 =A2.select(SALARY>A3) /分组汇总各部门的平均工资

The results of A4 are as follows:

ID NAME DEPT SALARY
5 Ashley R&D 16000
20 Alexis Administration 16000
22 Jacob R&D 18000
47 Elizabeth Marketing 17000

 There are more examples of related calculations in "SPL CookBook".


Guess you like

Origin blog.51cto.com/12749034/2547132