根据优先级获取不同的行?

时间:2013-03-11 10:01:21

标签: sql oracle

我有一张表如下。我正在使用oracle 10g。

TableA
------
id  status
---------------
1   R
1   S
1   W
2   R

我需要获得不同的ID及其状态。如果我查询不同的ID及其状态,我将获得所有4行。 但是每个身份我只能得到一个。 这里id 1有3种不同的状态。在这里,我应该只根据优先级获得一行。

第一优先级为'S',第二优先级为'W',第三优先级为'R'。

在我的情况下,我应该得到两个记录如下。

id  status
--------------
1   S
2   R

我该怎么做?请帮帮我。

谢谢!

7 个答案:

答案 0 :(得分:4)

select
  id,
  max(status) keep (dense_rank first order by instr('SWR', status)) as status
from TableA
group by id
order by 1

fiddle

答案 1 :(得分:1)

select id , status  from (         
select TableA.*, ROW_NUMBER()
OVER (PARTITION BY TableA.id  ORDER BY DECODE(
         TableA.status,
         'S',1,
         'W',2,
         'R',3,
             4)) AS row_no
FROM TableA) 
where row_no = 1

答案 2 :(得分:0)

这是我要做的第一件事,但可能有更好的方法。

Select id, case when status=1 then 'S' 
                when status=2 then 'W'
                when status=3 then 'R' end as status
from(
    select id, max(case when status='S' then 3
                        when status='W' then 2
                        when status='R' then 1
                    end) status
    from tableA
    group by id
    );

答案 3 :(得分:0)

要完成它,您可以编写类似的查询:

     -- sample of data from your question 
SQL> with t1(id , status) as (
  2    select 1,   'R'  from dual union all
  3    select 1,   'S'  from dual union all
  4    select 1,   'W'  from dual union all
  5    select 2,   'R'  from dual
  6  )
  7  select id   -- actual query
  8       , status
  9    from ( select id
 10                , status
 11                , row_number() over(partition by id
 12                                        order by case
 13                                                   when upper(status) = 'S'
 14                                                   then 1
 15                                                   when upper(status) = 'W'
 16                                                   then 2
 17                                                   when upper(status) = 'R'
 18                                                   then 3
 19                                                 end
 20                                     ) as rn
 21            from t1
 22         ) q
 23  where q.rn = 1
 24  ;

        ID STATUS
---------- ------
         1 S
         2 R

答案 4 :(得分:0)

 select id,status from 
 (select id,status,decode(status,'S',1,'W',2,'R',3) st from table) where (id,st) in
 (select id,min(st) from (select id,status,decode(status,'S',1,'W',2,'R',3) st from table))

答案 5 :(得分:0)

像这样的东西???

SQL> with xx as(
  2      select 1 id, 'R' status from dual UNION ALL
  3      select 1, 'S' from dual UNION ALL
  4      select 1, 'W' from dual UNION ALL
  5      select 2, 'R' from dual
  6  )
  7  select
  8      id,
  9      DECODE(
 10          MIN(
 11              DECODE(status,'S',1,'W',2,'R',3)
 12           ),
 13      1,'S',2,'W',3,'R') "status"
 14  from xx
 15  group by id;

        ID s
---------- -
         1 S
         2 R

在这里,逻辑非常简单。 做一个DECODE来设置'Priority',然后找到MIN(即一个具有更高优先级的值)并再次解码它以获得它的'Status'

答案 6 :(得分:0)

使用带有附加值的MOD()示例:

SELECT id, val, distinct_val
  FROM
  (
  SELECT id, val
       , ROW_NUMBER() OVER (ORDER BY id) row_seq
       , MOD(ROW_NUMBER() OVER (ORDER BY id), 2) even_row
       , (CASE WHEN id = MOD(ROW_NUMBER() OVER (ORDER BY id), 2) THEN NULL ELSE val END) distinct_val
   FROM
   (
   SELECT 1 id, 'R' val FROM dual
    UNION
   SELECT 1 id, 'S' val FROM dual
    UNION
   SELECT 1 id, 'W' val FROM dual
    UNION
   SELECT 2 id, 'R' val FROM dual
    UNION                          -- comment below for orig data
   SELECT 3 id, 'K' val FROM dual
    UNION
   SELECT 4 id, 'G' val FROM dual
    UNION
   SELECT 1 id, 'W' val FROM dual
  ))
  WHERE distinct_val IS NOT NULL
 /

ID    VAL    DISTINCT_VAL
--------------------------
1      S      S
2      R      R
3      K      K
4      G      G