2014-12-26 66 views
0

我有一張包含不同主題標記的表格「標記」。如果標記符合一個特定的範圍,那麼我應該選擇相應的等級並插入標記表本身的列'rank_sub_1'中。你能幫我嗎?我怎麼能在桌子上查找並插入列中。以下是我的表格結構。如何比較查詢表的值

**Marks** 
Subject1_Marks Subject2_Marks 
------------------------------ 
71    22 
10    40 

**LookupTable**  
Rank range1 range2 
---------------------- 
9   10 20 
8   21 30 
7   31 40 
6   41 50 
5   51 60 
4   61 70 
3   71 80 
2   81 90 
1   91 100 

現在我想用查找表檢查每個主題的標記,其中包含獲取的不同標記的範圍和排名。

**Marks**   
Subject1_Marks Subject2_Marks Rank_Sub_1 Rank_Sub_2 
------------------------------------------------------ 
71    22  
10    40 

如果標記符合一個特定的範圍,那麼我應該選擇相應的等級並插入標記表本身的列'rank_sub_1'中。你能幫我嗎?我怎麼能在桌子上查找並插入列中。

+0

如果在Subject1_Marks適合在兩個不同的區域?你在查找表中的等級重疊(例如20屬於等級9和8)。另外,你使用的是什麼數據庫? – jpw

+0

我正在使用oracle數據庫。 – Pratsam

+0

如果標記> rang1和<= range2,那麼值將不會重疊,那麼它應該適合。我會爲你改變我的數據 – Pratsam

回答

0

(考慮範圍值沒有重疊)

取兩個可查找的實例,首先加入subject1_marks,然後加入subject2_marks。在這裏,我沒有使用左連接,因爲我假設你的主題標記肯定會落在1範圍內。如果你不知道這一點,請使用左連接和處理空值按您的要求爲列RANK_SUB_1和RANK_SUB_2

WITH LOOKUPTABLE_TMP AS (SELECT * FROM LOOKUPTABLE) 
SELECT M.*, L1.RANK AS RANK_SUB_1, L2.RANK AS RANK_SUB_2 
FROM MARKS M , LOOKUPTABLE_TMP L1, LOOKUPTABLE_TMP L2 
WHERE M.SUBJECT1_MARKS BETWEEN L1.RANGE1 AND L1.RANGE2 
AND M.SUBJECT2_MARKS BETWEEN L2.RANGE1 AND L2.RANGE2 

然後將數據合併到表標記。

解決方案:

MERGE INTO MARKS MS 
USING 
(
SELECT M.SUBJECT1_MARKS, M.SUBJECT2_MARKS, L1.RNK AS RANK_SUB_1, L2.RNK AS RANK_SUB_2 
FROM MARKS M , LOOKUPTABLE L1, LOOKUPTABLE L2 
WHERE M.SUBJECT1_MARKS BETWEEN L1.RANGE1 AND L1.RANGE2 
AND M.SUBJECT2_MARKS BETWEEN L2.RANGE1 AND L2.RANGE2 
GROUP BY M.SUBJECT1_MARKS, M.SUBJECT2_MARKS, L1.RNK, L2.RNK 
) SUB 
ON (MS.SUBJECT1_MARKS=SUB.SUBJECT1_MARKS AND MS.SUBJECT2_MARKS =SUB.SUBJECT2_MARKS) 
WHEN MATCHED THEN UPDATE 
SET MS.RANK_SUB_1=SUB.RANK_SUB_1, MS.RANK_SUB_2=SUB.RANK_SUB_2; 

測試下面的架構和數據按您的問題的詳細信息。

CREATE TABLE MARKS (SUBJECT1_MARKS NUMBER, SUBJECT2_MARKS NUMBER , RANK_SUB_1 NUMBER, RANK_SUB_2 NUMBER) 

INSERT INTO MARKS (SUBJECT1_MARKS , SUBJECT2_MARKS) VALUES (71, 22); 

INSERT INTO MARKS (SUBJECT1_MARKS , SUBJECT2_MARKS) VALUES (10, 40); 

CREATE TABLE LOOKUPTABLE (RNK NUMBER, RANGE1 NUMBER , RANGE2 NUMBER) 

INSERT INTO LOOKUPTABLE VALUES (9, 10, 20); 

INSERT INTO LOOKUPTABLE VALUES (8, 21, 30); 

INSERT INTO LOOKUPTABLE VALUES (7, 31, 40); 

INSERT INTO LOOKUPTABLE VALUES (6, 41, 50); 

INSERT INTO LOOKUPTABLE VALUES (5, 51, 60); 

INSERT INTO LOOKUPTABLE VALUES (4, 61, 70); 

INSERT INTO LOOKUPTABLE VALUES (3, 71, 80); 

INSERT INTO LOOKUPTABLE VALUES (2, 81, 90); 

INSERT INTO LOOKUPTABLE VALUES (1, 91, 100); 

謝謝!!

0

我覺得這update說法應該做你想要什麼:

UPDATE Marks m 
    SET Rank_Sub_1 = (SELECT l.Rank 
        FROM LookupTable l 
        WHERE m.Subject1_Marks BETWEEN l.range1 AND l.range2) 
WHERE EXISTS (
    SELECT 1 
    FROM LookupTable l 
    WHERE m.Subject1_Marks BETWEEN l.range1 AND l.range2 
); 

Sample SQL Fiddle

如果要更新的同時對Rank_Sub_2值,你可以這樣做:

UPDATE Marks m 
    SET Rank_Sub_1 = (SELECT l.Rank 
        FROM LookupTable l 
        WHERE m.Subject1_Marks BETWEEN l.range1 AND l.range2) 
     ,Rank_Sub_2 = (SELECT l.Rank 
        FROM LookupTable l 
        WHERE m.Subject2_Marks BETWEEN l.range1 AND l.range2) 

Sample SQL Fiddle

0

考慮下面的設計,消除重疊或間隙的可能性。儘管我通常在日期中使用這種技術,但定義完整序列的任何數據都將以相同的方式工作。這個想法是,你只能定義範圍的起始位置。可以理解的是,範圍在可能小於下一個更高範圍的最後值處停止。但是,請注意我添加了第十個等級,以防萬一小於10的值是可能的。任何值大於100,當然,顯示爲排名1

with 
Lookup(Rank, Cutoff)as(
    select 1, 91 union all 
    select 2, 81 union all 
    select 3, 71 union all 
    select 4, 61 union all 
    select 5, 51 union all 
    select 6, 41 union all 
    select 7, 31 union all 
    select 8, 21 union all 
    select 9, 10 union all 
    select 10, 0 
), 
Marks(Mark1, Mark2)as(
    select 71, 22 union all 
    select 10, 40 union all 
    select 21, 101 
) 
select Mark1, l1.Rank as Rank1, Mark2, l2.Rank as Rank2 
from Marks m 
join Lookup l1 
    on l1.Cutoff =(
     select Max(Cutoff) 
     from Lookup 
     where Cutoff <= m.Mark1) 
join Lookup l2 
    on l2.Cutoff =(
     select Max(Cutoff) 
     from Lookup 
     where Cutoff <= m.Mark2); 

輸出:

Mark1  Rank1  Mark2  Rank2 
----------- ----------- ----------- ----------- 
71   3   22   8 
10   9   40   7 
21   8   101   1