sno mappingtype username
1 A xx
2 B yy
3 A aa
4 B bb
上面是我的表...我想要oracle查询以下结果
sno mappingtype username
1 A xx
aa
2 B yy
bb
答案 0 :(得分:2)
这是一个SQL版本。 (也许你想要一个sqlplus技巧,但你没有要求它)
with a as (
select 1 sno, 'A' mappingtype, 'xx' username from dual union all
select 2 sno, 'B' mappingtype, 'yy' username from dual union all
select 3 sno, 'A' mappingtype, 'aa' username from dual union all
select 4 sno, 'B' mappingtype, 'bb' username from dual
)
select
case when rnk=1 then sno end as sno,
case when rnk=1 then mappingtype end as mappingtype,
username
from(
select
sno,
mappingtype,
username,
row_number()over (partition by mappingtype order by sno) rnk
from a
);
答案 1 :(得分:1)
我认为会是这样的:
select min(sno) , mappingtype, username from yourtable Group by mappingtype, username
答案 2 :(得分:0)
告诉florin ghita代码,输出是这样的:
所以,对代码进行一些更改可能是这样的:
WITH a AS (
SELECT 1 sno, 'A' mappingtype, 'xx' username FROM dual
WHERE TYPE = 'A' AND username = 'xx'
UNION ALL
SELECT 2 sno, 'B' mappingtype, 'yy' username FROM dual
WHERE TYPE = 'B' AND username = 'yy'
UNION ALL
SELECT 3 sno, 'A' mappingtype, 'aa' username FROM dual
WHERE TYPE = 'A' AND username = 'aa'
UNION ALL
SELECT 4 sno, 'B' mappingtype, 'bb' username FROM dual
WHERE TYPE = 'B' AND username = 'bb')
SELECT CASE WHEN rnk = 1 THEN sno END AS sno,
CASE WHEN rnk = 1 THEN mappingtype END AS mappingtype,
username
FROM (SELECT sno,
mappingtype,
username,
ROW_NUMBER () OVER (PARTITION BY mappingtype ORDER BY sno) rnk
FROM a);
并且在Shalim问题中输出将是相同的: