For one of my ETL-Jobs I need a SQL-Query that converts rows to columns. The difficulty is that I want to parameterize how many rows getting converted into one column as shown in following example:
ATM my scenario looks like this:
Oracle 11g
One Column Table:
PARAMETER
AB
AE
CF
GH
5G
H3
7P
…..
SQL-Query:
SELECT listagg (''''
||PARAMETER
||'''', ',') WITHIN GROUP (
ORDER BY ROWNUMBER) AS PARAMETER,
FROM
(SELECT
CASE
WHEN ROWNUM <= 5 THEN 5
WHEN ROWNUM <= 10 THEN 10
WHEN ROWNUM <= 15 THEN 15
WHEN ROWNUM <= 20 THEN 20
WHEN ROWNUM <= 25 THEN 25
END AS ROWNUMBER
PARAMETER
FROM SR0_CRTL_SL_OL_PSM_PARAMETER
)
GROUP BY ROWNUMBER
Result would look like this:
PARAMETER
AB, BG, RT, ZH, JK
AE, HL, GH, DZ, KL
CF, GH, NM, SD, WE …..
What I would like to have is a Query that takes a number eg. 5 with the following result:
PARAMETER
AB, BG, RT, ZH, JK
AE, HL, GH, DZ, KL
CF, GH, NM, SD, WE …..
Or it takes eg. 8 and the result would be like:
PARAMETER
AB, BG, RT, ZH, JK, AE, HL, GH
DZ, KL, CF, GH, NM, SD, WE, DE
FG, JK, KL, UZ, IT, FG, HR, TZ …..
I hope you get what I mean. Maybe someone could provide me some help.
Thanks
PS: I know that my English is damn good “: }
You can calculate the group using arithmetic -- do a division and take the integer from it. The following uses "8" as the parameter:
SELECT listagg (''''||PARAMETER||'''', ',') WITHIN GROUP (ORDER BY RowNumber) AS PARAMETER,
FROM (SELECT trunc((rownum - 1) / 8) as grp, t.*,
rownum as RowNumber
FROM SR0_CRTL_SL_OL_PSM_PARAMETER t
) t
GROUP BY grp;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.