Let me start my question by setting up my scenario.
I have a test2 table which contains only 2 fields: productid and productlife, I would like to explicitly list all the years along with the products
for example,
With product A, I would like to list
Y15|Y16|Y17|Y18|Y19, A
and for product B, using the same rule, I should get
Y18|Y19, B
My sql does not produce the result I am looking for:
SELECT
(
SELECT listagg("Year",'|') within GROUP (
ORDER BY "Year") "Prefix"
FROM
(
SELECT 'Y'
||(TO_CHAR(SYSDATE,'yy')-LEVEL) "Year"
FROM dual
CONNECT BY level<=r.productlife
)
) "Prefix", productid
FROM TEST2 r
How should it be corrected? I would think the field productlife in each record will control the level in the statement but it does not seem to do so..
Please advise.
Below is the script to create my example for your convenience.
CREATE TABLE "TEST2"
( productid VARCHAR2(20 BYTE),
productlife NUMBER
) ;
Insert into TEST2 (productid,productlife) values ('A',5);
Insert into TEST2 (productid,productlife) values ('B',2);
Thanks!
