-3
sno mappingtype username

1      A            xx   
2      B            yy   
3      A            aa    
4      B            bb    

以上是我的表...我想要 oracle 查询以下结果

sno 映射类型用户名

1     A           xx    
                  aa    

2     B           yy
                  bb
4

3 回答 3

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
        );
于 2013-06-10T07:15:30.440 回答
1

我认为它会是这样的:

select min(sno) , mappingtype, username from yourtable Group by mappingtype, username
于 2013-06-10T08:13:11.943 回答
0

Triing 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 问题中的输出将是相同的:

编码后

于 2013-06-10T10:11:15.870 回答