SELECT
*
FROM
(
SELECT
lev1,
lev2,
lev4,
ROW_NUMBER () over (PARTITION BY lev1 ORDER BY lev2 ASC) AS rw2
FROM
(
SELECT
lev1,
lev2,
lev4
FROM
(
SELECT
t.*, ROW_NUMBER () over (
PARTITION BY lev1,
lev2
ORDER BY
lev4 ASC
) AS rw
FROM
(
SELECT
a.m AS lev1,
a.n AS lev2,
b.m AS lev3,
b.n AS lev4
FROM
test a
LEFT JOIN test b ON a.m = b.m
AND a.n < b.n
WHERE
b.n IS NOT NULL
ORDER BY
a.m,
a.n
) t
) k
WHERE
rw = 1
) q
) j
WHERE
j.rw2 % 2 = 1
SQL还有优化的空间,自己发挥吧!