2016-01-13 33 views
-2

的比較值創建Oracle視圖我要創建一個基於表Oracle視圖,類似的以下內容:如何基於表

<table border="1"> 
 
    <tr> 
 
    <td>Id</td><td>store-id</td><td>prod-id</td><td>s-date</td> 
 
    </tr> 
 
    <tr> 
 
    <td>1</td><td>21</td><td>2</td><td>NULL</td> 
 
    </tr> 
 
    <tr> 
 
    <td>2</td><td>21</td><td>3</td><td>12-12-16</td> 
 
    </tr> 
 
    <tr> 
 
    <td>3</td><td>22</td><td>2</td><td>NULL</td> 
 
    </tr> 
 
    <tr> 
 
    <td>4</td><td>22</td><td>3</td><td>NULL</td> 
 
    </tr> 
 
    <tr> 
 
    <td>5</td><td>23</td><td>4</td><td>12-12-16</td> 
 
    </tr> 
 
    <tr> 
 
    <td>6</td><td>23</td><td>5</td><td>12-12-16</td> 
 
    </tr> 
 
</table>

現在鑑於我要補充新叫狀態列,這是用於商店中的ID S-日期值的鹼,如果一個存儲有所有s-日期空值,則狀態= 0 如果商店有所有s-日期不是null然後starus = 2,並且如果某個商店有不所有空S-日期,那麼狀態= 1,所以查看看起來像以下:

<table border="1"> 
 
    <tr> 
 
    <td>Id</td><td>store-id</td><td>prod-id</td><td>s-date</td><td>status</td> 
 
    </tr> 
 
    <tr> 
 
    <td>1</td><td>21</td><td>2</td><td>NULL</td><td>1</td> 
 
    </tr> 
 
    <tr> 
 
    <td>2</td><td>21</td><td>3</td><td>12-12-16</td><td>1</td> 
 
    </tr> 
 
    <tr> 
 
    <td>3</td><td>22</td><td>2</td><td>NULL</td><td>0</td> 
 
    </tr> 
 
    <tr> 
 
    <td>4</td><td>22</td><td>3</td><td>NULL</td><td>0</td> 
 
    </tr> 
 
    <tr> 
 
    <td>5</td><td>23</td><td>4</td><td>12-12-16</td><td>2</td> 
 
    </tr> 
 
    <tr> 
 
    <td>6</td><td>23</td><td>5</td><td>12-12-16</td><td>2</td> 
 
    </tr> 
 
</table>

+0

你究竟需要什麼?該HTML表是您從前端應用程序接收到的對象(BLOB/CLOB)嗎?你想創建一個解析該HTML表格的視圖嗎?既然你標記了'plsql',也許你想創建一個分析表的過程/函數? – AndrewMcCoist

+0

我認爲HTML部分僅用於向我們展示用戶如何獲取數據。 – Nitish

+0

是的你的是正確的Nitish,HTML表格用於顯示用戶想要的數據。 – p27

回答

0

我已插入前四列中的數據到一個表STORE_DATA

SELECT A.*, TAB1.STATUS 
    FROM STORE_DATA A, 
     (SELECT T.STORE_ID, COUNT(T.S_DATE) STATUS 
      FROM STORE_DATA T 
     GROUP BY T.STORE_ID) TAB1 
WHERE A.STORE_ID = TAB1.STORE_ID; 

Count只會返回非空值。

0

我已經試過這對您的數據。

CREATE TABLE stores 
( 
    ID NUMBER(2), 
    store_id NUMBER(3), 
    proc_id NUMBER(3), 
    s_date DATE 
); 

INSERT INTO stores VALUES(1,21,2,NULL); 
INSERT INTO stores VALUES(2,21,3,TRUNC(SYSDATE)); 
INSERT INTO stores VALUES(3,22,2,NULL); 
INSERT INTO stores VALUES(4,22,3,NULL); 
INSERT INTO stores VALUES(5,23,4,TRUNC(SYSDATE)); 
INSERT INTO stores VALUES(6,23,5,TRUNC(SYSDATE)); 
COMMIT; 

您可以使用下面的創建視圖。

CREATE VIEW stores_view 
AS 
SELECT s.id, 
     s.store_id, 
     s.proc_id, 
     s.s_date, 
     (CASE 
     WHEN (SELECT COUNT(1)  
       FROM stores s1  
       WHERE s1.store_id = s.store_id  
        AND s1.s_date IS NOT NULL) = 0 THEN 
       0 
     WHEN (SELECT COUNT(1)  
       FROM stores s1  
       WHERE s1.store_id = s.store_id  
        AND s1.s_date IS NULL) = 0 THEN  
       2  
     ELSE  
       1  
     END) status 
    FROM stores s;  
0

感謝所有的回覆:

我必須解決這個問題類似以下內容:

首先我創建了一個函數來計算像以下狀態:

FUNCTION CALC_STATUS(s_date_count IN NUMBER, store_count IN NUMBER)RETURN NUMBER 
AS 
    n_status NUMBER; 
BEGIN 
    IF s_date_count = 0 THEN 
    n_status  := 0; 
    RETURN n_status; 
    END IF; 
    IF s_date_count < store_count THEN 
    n_status  := 1; 
    RETURN n_status; 
    END IF; 
    IF s_date_count = store_count THEN 
    n_status  := 2; 
    RETURN n_status; 
    END IF; 
END CALC_STATUS; 

然後用戶以上函數創建一個視圖:

CREATE OR REPLACE VIEW V_SRORE (ID, STORE_ID, PROD_ID, S_DATE, STATUS) 
         AS 
    SELECT A.ID   AS ID, 
    A.STORE_ID  AS STORE_ID, 
    A.PROD_ID   AS PROD_ID, 
    A.S_DATE   AS S_DATE, 
    TEMP_TABLE.STATUS AS STATUS 
    FROM T_STORE A, 
    (SELECT B.STORE_ID, 
     CALC_STATUS(COUNT(B.S_DATE), COUNT(B.STORE_ID)) AS STATUS 
    FROM T_STORE B 
    GROUP BY B.STORE_ID 
    ) TEMP_TABLE 
    WHERE A.STORE_ID=TEMP_TABLE.STORE_ID 
    ORDER BY A.STORE_ID DESC;