-1
有人可以讓我們知道一個更簡單的方法來獲得結果,如下面的查詢所示嗎?我們需要找到一種方式,可以向我們展示了相同的結果,下面的查詢:更簡單的方法來獲得如下所示的查詢結果(oracle數據庫)
SELECT DISTINCT A.bio_id ,
DECODE(A.wk_units2 - B.wk_units1,0,NULL,A.wk_units2) prev,
DECODE(A.wk_units2 - B.wk_units1,0,NULL,B.wk_units1) curr,
DECODE(A.wk_units3 -B.wk_units2,0,NULL,A.wk_units3) prev,
DECODE(A.wk_units3 -B.wk_units2,0,NULL,B.wk_units2) curr,
DECODE(A.wk_units4 -B.wk_units3,0,NULL,A.wk_units4) prev,
DECODE(A.wk_units4 -B.wk_units3,0,NULL,B.wk_units3) curr,
DECODE(A.wk_units5 -B.wk_units4,0,NULL,A.wk_units5) prev,
DECODE(A.wk_units5 -B.wk_units4,0,NULL,B.wk_units4) curr,
DECODE(A.wk_units6 -B.wk_units5,0,NULL,A.wk_units6) prev,
DECODE(A.wk_units6 -B.wk_units5,0,NULL,B.wk_units5) curr
. .
. .
. .
DECODE(A.wk_units105 -B.wk_units104,0,NULL,A.wk_units105) prev,
DECODE(A.wk_units105 -B.wk_units105,0,NULL,B.wk_units104) curr
FROM
cnt_sls_dm.fct_sales_summary A,
cnt_sls_dm.fct_sales_summary B
WHERE
A.bio_id=B.bio_id AND A.bio_id<>0
AND ROWNUM<25
AND (A.wk_units2<> B.wk_units1
OR A.wk_units3<> B.wk_units2
OR A.wk_units4<> B.wk_units3
OR A.wk_units5<> B.wk_units4
OR A.wk_units6<> B.wk_units5
. .
. .
. .
OR A.wk_units105<>B.wk_units104)
輸出是這樣的:
BIO_ID PREV CURR PREV_1 CURR_1 PREV_2 CURR_2 PREV_3 CURR_3
5,032,130.00 -1 0 0 1 0 1
5,032,130.00 1 0 0 1 1 0 0 1
5,032,130.00 0 1 0 1
5,032,130.00 10 1 10 0 0 1
5,032,328.00 53 0 36 0 20 0
5,032,328.00 1 0
5,175,147.00 2 0
5,175,147.00 2 0
5,175,147.00
21,073,129.00 17 0 2 0
21,073,129.00 18 0 6 0 2 0
21,073,129.00 1 0
21,073,129.00 3 0
21,073,129.00
您使用的是哪個版本的Oracle? – David 2009-08-31 13:30:57
@Iveveen;如果建議的解決方案正常工作/回答其他問題,您應提供反饋意見。 – tuergeist 2009-08-31 20:44:15
@Iveveen:「更容易」是什麼意思?如果您希望我們重寫查詢,您應該描述生成結果的規則,併爲nasratic的非規格化表提供解釋。 – APC 2009-09-01 06:50:53