2013-03-11 53 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