2013-03-11 78 views
2

我有一个表如下,我使用的是Oracle 10g。根据优先级获取不同的行?

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

我需要获得不同的ID以及他们的状态。如果我查询不同的ID和他们的状态,我得到所有4行。 但我应该只得到2.每个ID一个。 此处ID为1有3种不同的状态。在这里,我应该只根据优先级获得一行。

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

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

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

我该怎么做?请帮帮我。

谢谢!

回答

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 
    ); 
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 
+0

这比我好,因为解码是在一个地方。我的答案有没有使用分析功能的“优势”,这是一个先进的事情。 – 2013-03-11 10:17:02

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)) 
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

+0

我的投票去这 - 包含一些独创性:) – 2013-03-11 13:26:42

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 
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用于设置“优先级”,然后找到MIN(即一个具有较高优先级)值,然后再次对其进行解码回去拿其“状态”

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