GP詭異的查詢轉換
原始資料:
edw=# select * from ods.my_test2;
i_a | i_b | i_c
-----+-----+--------
b | 5 | 201501
b | 5 | 201502
b | 6 | 201503
a | 1 | 201502
a | 1 | 201503
a | 1 | 201508
a | 2 | 201505
a | 2 | 201507
a | 2 | 201506
a | 1 | 201509
a | 1 | 201501
a | 1 | 201501
SELECT
m.*,
case
when lag(m.i_a) over(PARTITION by m.i_a ORDER BY m.i_c) = m.i_a and lag(m.i_b) over(PARTITION by m.i_a order by m.i_c)=m.i_b
then case
when m.id=min(m.id) over(PARTITION by m.i_a ORDER BY m.i_c) then id else NULL
end else id end flag
FROM (select i_a,i_b,i_c,row_number() over(ORDER BY i_c) as id from ods.my_test2 ) m
結果
i_a | i_b | i_c | id | flag
-----+-----+--------+----+------
a | 1 | 201501 | 3 | 3
a | 1 | 201501 | 2 | 2
a | 1 | 201502 | 4 |
a | 1 | 201503 | 7 |
a | 2 | 201505 | 8 | 8
a | 2 | 201506 | 9 |
a | 2 | 201507 | 10 |
a | 1 | 201508 | 11 | 11
a | 1 | 201509 | 12 |
b | 5 | 201501 | 1 | 1
b | 5 | 201502 | 5 |
b | 6 | 201503 | 6 | 6
從語句上來說得出這個結果是有問題的,第一行資料的flag應該是空的,不應該是3
將子查詢變成實表
create table ods.my_test as select i_a,i_b,i_c,row_number() over(ORDER BY i_c) as id from ods.my_test2;
再從實表中查詢
SELECT
m.*,
case
when lag(m.i_a) over(PARTITION by m.i_a ORDER BY m.i_c) = m.i_a and lag(m.i_b) over(PARTITION by m.i_a order by m.i_c)=m.i_b
then case
when m.id=min(m.id) over(PARTITION by m.i_a ORDER BY m.i_c) then id else NULL
end else id end flag
FROM ods.my_test m
結果:
i_a | i_b | i_c | id | flag
-----+-----+--------+----+------
a | 1 | 201501 | 2 | 2
a | 1 | 201501 | 3 |
a | 1 | 201502 | 4 |
a | 1 | 201503 | 7 |
a | 2 | 201505 | 8 | 8
a | 2 | 201506 | 9 |
a | 2 | 201507 | 10 |
a | 1 | 201508 | 11 | 11
a | 1 | 201509 | 12 |
b | 5 | 201501 | 1 | 1
b | 5 | 201502 | 5 |
b | 6 | 201503 | 6 | 6
這個結果和上面的結果有差異,這種情況和oracle的查詢轉換很相似。但在GP中沒有找到oracle的/*+NO_MERGE(m)*/功能,只能在儲存過程中將這個子查詢用臨時表去解決了!
edw=# select * from ods.my_test2;
i_a | i_b | i_c
-----+-----+--------
b | 5 | 201501
b | 5 | 201502
b | 6 | 201503
a | 1 | 201502
a | 1 | 201503
a | 1 | 201508
a | 2 | 201505
a | 2 | 201507
a | 2 | 201506
a | 1 | 201509
a | 1 | 201501
a | 1 | 201501
SELECT
m.*,
case
when lag(m.i_a) over(PARTITION by m.i_a ORDER BY m.i_c) = m.i_a and lag(m.i_b) over(PARTITION by m.i_a order by m.i_c)=m.i_b
then case
when m.id=min(m.id) over(PARTITION by m.i_a ORDER BY m.i_c) then id else NULL
end else id end flag
FROM (select i_a,i_b,i_c,row_number() over(ORDER BY i_c) as id from ods.my_test2 ) m
結果
i_a | i_b | i_c | id | flag
-----+-----+--------+----+------
a | 1 | 201501 | 3 | 3
a | 1 | 201501 | 2 | 2
a | 1 | 201502 | 4 |
a | 1 | 201503 | 7 |
a | 2 | 201505 | 8 | 8
a | 2 | 201506 | 9 |
a | 2 | 201507 | 10 |
a | 1 | 201508 | 11 | 11
a | 1 | 201509 | 12 |
b | 5 | 201501 | 1 | 1
b | 5 | 201502 | 5 |
b | 6 | 201503 | 6 | 6
從語句上來說得出這個結果是有問題的,第一行資料的flag應該是空的,不應該是3
將子查詢變成實表
create table ods.my_test as select i_a,i_b,i_c,row_number() over(ORDER BY i_c) as id from ods.my_test2;
再從實表中查詢
SELECT
m.*,
case
when lag(m.i_a) over(PARTITION by m.i_a ORDER BY m.i_c) = m.i_a and lag(m.i_b) over(PARTITION by m.i_a order by m.i_c)=m.i_b
then case
when m.id=min(m.id) over(PARTITION by m.i_a ORDER BY m.i_c) then id else NULL
end else id end flag
FROM ods.my_test m
結果:
i_a | i_b | i_c | id | flag
-----+-----+--------+----+------
a | 1 | 201501 | 2 | 2
a | 1 | 201501 | 3 |
a | 1 | 201502 | 4 |
a | 1 | 201503 | 7 |
a | 2 | 201505 | 8 | 8
a | 2 | 201506 | 9 |
a | 2 | 201507 | 10 |
a | 1 | 201508 | 11 | 11
a | 1 | 201509 | 12 |
b | 5 | 201501 | 1 | 1
b | 5 | 201502 | 5 |
b | 6 | 201503 | 6 | 6
這個結果和上面的結果有差異,這種情況和oracle的查詢轉換很相似。但在GP中沒有找到oracle的/*+NO_MERGE(m)*/功能,只能在儲存過程中將這個子查詢用臨時表去解決了!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29989552/viewspace-2125249/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 記一次詭異的Oracle查詢轉換Oracle
- 詭異的”慢查詢“
- 查詢轉換
- Oracle 查詢轉換Oracle
- GP查詢外部表報錯
- Oracle 查詢轉換初探Oracle
- Oracle查詢轉換(五)子查詢展開Oracle
- Oracle 查詢轉換-01 or expansionOracle
- Mysql 查詢時間轉換MySql
- JavaScript 詭異的0.01JavaScript
- 【SQL】Oracle查詢轉換之 OR用法SQLOracle
- 一個詭異的 Pulsar InterruptedException 異常Exception
- Oracle 12CR2查詢轉換之星型轉換Oracle
- 一個詭異的MySQL查詢超時問題,居然隱藏著存在了兩年的BUGMySql
- 【SQL】Oracle查詢轉換之物化檢視查詢重寫SQLOracle
- 一條SQL語句查詢塊分解及查詢轉換SQL
- Oracle 查詢轉換-04 Subquery UnnestingOracle
- Oracle XQuery查詢、構建和轉換XML(1)(轉)OracleXML
- CBO的查詢轉換(謂詞推入與子查詢展開(Subquery Unnesting))
- Oracle 12CR2查詢轉換之臨時錶轉換Oracle
- 關於查詢轉換的一些總結
- (轉)SQL查詢案例:多行轉換為一行SQL
- Oracle 查詢轉換-02 View MergingOracleView
- Oracle 查詢轉換-03 Predicate PushingOracle
- 【SQL】Oracle查詢轉換之謂詞推送SQLOracle
- D4.玩轉查詢與替換
- 使用 Oracle XQuery 查詢、構建和轉換 XMLOracleXML
- 詭異的無線網路卡Down
- C語言之詭異字串C語言字串
- mysql慢查詢的奇異事件MySql事件
- SQL解析過程中的查詢轉換 - Transforming QueriesSQLORM
- 【八】查詢變換
- vim查詢替換
- 【SQL】Oracle查詢轉換之檢視合併SQLOracle
- Oracle查詢轉換(四)連線謂詞推入Oracle
- 怎樣把Oracle查詢轉換為SQL ServerOracleSQLServer
- API 路由中介軟體的詭異API路由
- 介面詭異的404問題記錄