我必须根据如下表格创建一个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>
现在在视图中我必须添加一个名为status的新列,它基于store-id的s-date值,如果一个商店的所有s-date为null,则status = 0 如果商店的所有s-date都不为null,那么starus = 2并且如果商店没有所有null s-date然后status = 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 :(得分: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
将仅返回非空值。
答案 1 :(得分: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;
答案 2 :(得分: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;