我试图弄清楚如何根据一组值计算匹配值。下面是表结构的示例。我正在使用12c
Table 1 Table 2
id term course course_num course course_num equ_course equ_num
1 201710 MATH 1020 MATH 1020 MATH 2015
1 201510 MATH 2015 MATH 1020 MATH 101
1 201680 MATH 101 MATH 101 MATH 1020
2 201380 ENGL 1010
我需要的是学生重复课程的数量。在示例中,id为1的学生同时参加MATH 1020,101和2015.由于在表2(等值表)中,MATH 2015和MATH 101计为MATH 1020,结果应如下所示。重复列是他们重复课程的次数。
id term course course_num repeated
1 201710 MATH 1020 3
1 201510 MATH 2015 3
1 201680 MATH 101 3
答案 0 :(得分:0)
让我们改变样本数据,使其更复杂;而不是MATH/1020
等同于MATH/2015
和MATH/101
我们可以将等效项链接起来,以便MATH/1020
等同于MATH/2015
,而MATH/101
相当于Table2
{1}}。这使得简单地将CREATE TABLE Table1 ( id, term, course, course_num ) AS
SELECT 1, 201710, 'MATH', 1020 FROM DUAL UNION ALL
SELECT 1, 201510, 'MATH', 2015 FROM DUAL UNION ALL
SELECT 1, 201680, 'MATH', 101 FROM DUAL UNION ALL
SELECT 2, 201380, 'ENGL', 1010 FROM DUAL;
CREATE TABLE Table2 ( course, course_num, equ_course, equ_num) AS
SELECT 'MATH', 1020, 'MATH', 2015 FROM DUAL UNION ALL
SELECT 'MATH', 2015, 'MATH', 101 FROM DUAL;
加入到自身以合并等价物是不够的,因为可以存在多层深度的树。而是使用分层查询:
Oracle 11g R2架构设置:
WITH equivalent_courses ( course, course_num, equ_course, equ_num ) AS (
SELECT CONNECT_BY_ROOT( course ),
CONNECT_BY_ROOT( course_num ),
equ_course,
equ_num
FROM Table2
START WITH ( course, course_num )
NOT IN ( SELECT equ_course, equ_num FROM TABLE2 )
CONNECT BY PRIOR equ_course = course
AND PRIOR equ_num = course_num
)
SELECT t.*,
COUNT(*) OVER (
PARTITION BY COALESCE( e.course, t.course ),
COALESCE( e.course_num, t.course_num )
) AS repeated
FROM Table1 t
LEFT OUTER JOIN equivalent_courses e
ON ( t.course = e.equ_course AND t.course_num = e.equ_num )
查询1 :
| ID | TERM | COURSE | COURSE_NUM | REPEATED |
|----|--------|--------|------------|----------|
| 2 | 201380 | ENGL | 1010 | 1 |
| 1 | 201710 | MATH | 1020 | 3 |
| 1 | 201510 | MATH | 2015 | 3 |
| 1 | 201680 | MATH | 101 | 3 |
<强> Results 强>:
const fetch = require('jest-fetch-mock');
jest.setMock('portable-fetch', fetch);
const factory = require('./current-weather');
describe('current-weather', () => {
const config = { appid: 'appid' };
const currentWeather = factory(config);
test('currentWeather()', async () => {
const weather = { temperature: '25.17 °C' };
fetch.mockResponse(JSON.stringify(weather));
const current = await currentWeather({ city: 'London' });
expect(current).toEqual(weather);
});
});