Oracle Connect by 單行變多行
create table my_t1(
rowno number,
wareid number,
maxqty number,
wareqty number
)
insert into my_t1 values(1, 100, 400, 500);
insert into my_t1 values(2, 110, 240, 600);
SELECT wareqty,
maxqty,
wareid,
rowno,
CASE
WHEN leijia > wareqty THEN
wareqty -
nvl(lag(leijia) over(PARTITION BY wareid, rowno ORDER BY lv), 0)
ELSE
maxqty
END allocqty
FROM (SELECT x.*,
LEVEL lv,
SUM(maxqty) over(PARTITION BY wareid, rowno ORDER BY LEVEL) leijia
FROM (SELECT ceil(wareqty / maxqty) num_rows,
wareqty,
maxqty,
wareid,
rowno,
SUM(ceil(wareqty / maxqty)) over(ORDER BY rownum) - rownum + 1 psum
FROM my_t1) x
CONNECT BY rownum <= psum
ORDER BY wareid, lv) t
SQL 2:
SELECT wareqty,
maxqty,
wareid,
rowno,
CASE
WHEN leijia > wareqty THEN
wareqty -
nvl(lag(leijia) over(PARTITION BY wareid, rowno ORDER BY lv), 0)
ELSE
maxqty
END allocqty
FROM (SELECT x.*,
LEVEL lv,
SUM(maxqty) over(PARTITION BY wareid, rowno ORDER BY LEVEL) leijia
FROM (SELECT ceil(wareqty / maxqty) num_rows,
wareqty,
maxqty,
wareid,
rowno,
SUM(ceil(wareqty / maxqty)) over(ORDER BY rownum) - rownum + 1 psum
FROM my_t1) x
CONNECT BY rownum <= psum
ORDER BY wareid, lv) t
rowno number,
wareid number,
maxqty number,
wareqty number
)
insert into my_t1 values(1, 100, 400, 500);
insert into my_t1 values(2, 110, 240, 600);
maxqty,
wareid,
rowno,
CASE
WHEN leijia > wareqty THEN
wareqty -
nvl(lag(leijia) over(PARTITION BY wareid, rowno ORDER BY lv), 0)
ELSE
maxqty
END allocqty
FROM (SELECT x.*,
LEVEL lv,
SUM(maxqty) over(PARTITION BY wareid, rowno ORDER BY LEVEL) leijia
FROM (SELECT ceil(wareqty / maxqty) num_rows,
wareqty,
maxqty,
wareid,
rowno,
SUM(ceil(wareqty / maxqty)) over(ORDER BY rownum) - rownum + 1 psum
FROM my_t1) x
CONNECT BY rownum <= psum
ORDER BY wareid, lv) t
SQL 2:
SELECT wareqty,
maxqty,
wareid,
rowno,
CASE
WHEN leijia > wareqty THEN
wareqty -
nvl(lag(leijia) over(PARTITION BY wareid, rowno ORDER BY lv), 0)
ELSE
maxqty
END allocqty
FROM (SELECT x.*,
LEVEL lv,
SUM(maxqty) over(PARTITION BY wareid, rowno ORDER BY LEVEL) leijia
FROM (SELECT ceil(wareqty / maxqty) num_rows,
wareqty,
maxqty,
wareid,
rowno,
SUM(ceil(wareqty / maxqty)) over(ORDER BY rownum) - rownum + 1 psum
FROM my_t1) x
CONNECT BY rownum <= psum
ORDER BY wareid, lv) t
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8520577/viewspace-2126845/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- sql、oracle資料多行轉單行SQLOracle
- ORACLE單行函式與多行函式之一Oracle函式
- Oracle多行轉一行Oracle
- ORACLE單行函式與多行函式之七:多行函式之分組函式示例Oracle函式
- Oracle之多行記錄變一行記錄,行變列,並排序(wmsys.wm_concat)Oracle排序
- ORACLE單行函式與多行函式之二:字元函式示例Oracle函式字元
- ORACLE單行函式與多行函式之三:數值函式Oracle函式
- ORACLE單行函式與多行函式之四:日期函式示例Oracle函式
- ORACLE單行函式與多行函式之六:通用函式示例Oracle函式
- Oracle 合併行/多行合併成一行Oracle
- ORACLE單行函式與多行函式之五:轉換函式示例Oracle函式
- 利用Oracle分析函式row_number和sys_connect_by_path實現多行資料合併為一行Oracle函式
- SQL多行變一列SQL
- JS單行、多行文字字元去重和行去重JS字元
- 在oracle中將一行字串拆分成多行Oracle字串
- Oracle connect byOracle
- MySQL 變數 、rownum 、 一行拆多行..小知識點MySql變數
- Oracle 多行分多列Oracle
- extjs 裡執行多行選中,和單行選中的操作JS
- oracle 行變列Oracle
- Laravel connect oracleLaravelOracle
- Oracle “CONNECT BY” 使用Oracle
- oracle connect by用法Oracle
- Oracle Start with ....Connect ByOracle
- 如何在 Bash 指令碼中使用單行和多行註釋指令碼
- 正規表示式單行、多行模式簡介(使用說明)模式
- JavaScript註釋:單行註釋和多行註釋詳解JavaScript
- 用Table變數返回多行資料變數
- CSS實現單行、多行文字溢位顯示省略號CSS
- CSS實現單行、多行文字溢位顯示省略號(…)CSS
- START WITH and CONNECT BY in Oracle SQLOracleSQL
- oracle表對錶的多行更新Oracle
- 返回部分結果後,才報單行子查詢返回多行。
- 如何將多行單元格資料轉變成一行?這個Excel技巧你一定不知道!Excel
- oracle中start with connect by priorOracle
- oracle的connect by語句Oracle
- 【函式】Oracle “CONNECT BY” 使用函式Oracle
- Oracle Connect By Prior 詳解Oracle