--测试表与测试数据 CREATE TABLE test_main ( id INT, value VARCHAR(10), PRIMARY KEY(id) ); -- 创建测试子表. CREATE TABLE test_sub ( id INT, main_id INT, value VARCHAR(10), PRIMARY KEY(id) ); -- 插入测试主表数据. INSERT INTO test_main(id, value) VALUES (1, 'ONE'); INSERT INTO test_main(id, value) VALUES (2, 'TWO'); -- 插入测试子表数据. INSERT INTO test_sub(id, main_id, value) VALUES (1, 1, 'A'); INSERT INTO test_sub(id, main_id, value) VALUES (2, 1, 'B'); INSERT INTO test_sub(id, main_id, value) VALUES (3, 1, 'C'); INSERT INTO test_sub(id, main_id, value) VALUES (4, 2, 'D'); INSERT INTO test_sub(id, main_id, value) VALUES (5, 2, 'E'); INSERT INTO test_sub(id, main_id, value) VALUES (6, 2, 'F');
要求:主从表关联的时候,主表仅仅第一条记录显示,后面相同的情况下,不显示
默认情况下
---------- ----------
ONE A
ONE B
ONE C
TWO D
TWO E
TWO F
希望查询结果能变为
---------- ----------
ONE A
B
C
TWO D
E
F
思路:
首先, 根据主表的数据 分组显示 ROW_NUMBER
然后,仅仅显示 ROW_NUMBER = 1 的主表数据, 其他的主表数据不显示
实现
第一步 根据主表的数据 分组显示 ROW_NUMBER
SELECT
test_main.value,
test_sub.value,
ROW_NUMBER() OVER (PARTITION BY test_main.value ORDER BY test_sub.value)
FROM
test_main,
test_sub
WHERE
test_main.id = test_sub.main_id
value value
---------- ---------- --------------------
ONE A 1
ONE B 2
ONE C 3
TWO D 1
TWO E 2
TWO F 3
第二步 仅仅显示 ROW_NUMBER = 1 的主表数据, 其他的主表数据不显示
SELECT
CASE WHEN
ROW_NUMBER() OVER (PARTITION BY test_main.value ORDER BY test_sub.value) = 1 THEN test_main.value
ELSE ''
END AS Main_Value,
test_sub.value
FROM
test_main,
test_sub
WHERE
test_main.id = test_sub.main_id
执行结果
Main_Value value
---------- ----------
ONE A
B
C
TWO D
E
F