You Asked
Tom,
Cant seem to frame this question in SQL. Did search through this site but didnt find a solution (the thread on stragg with ordered was the closest but the rollup of the null rows to the last non null for from and time is still presenting a problem). Any ideas?
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for Linux: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
5 rows selected.
SQL> create table cust
2 (custno number
3 ,cdate date
4 ,from_time varchar2(10)
5 ,to_time varchar2(10)
6 ,seq number
7 ,comments varchar2(100));
Table created.
SQL> insert into cust values (1,'01-JAN-2010','05:00','08:00',1,'5-8 comment line 1');
1 row created.
SQL> insert into cust values (1,'01-JAN-2010',null,null,2,'5-8 comment line 2');
1 row created.
SQL> insert into cust values (1,'01-JAN-2010',null,null,3,'5-8 comment line 3');
1 row created.
SQL> insert into cust values (1,'01-JAN-2010','08:00','10:00',4,'8-10 line 1');
1 row created.
SQL> insert into cust values (1,'01-JAN-2010',null,null,5,'8-10 line 2');
1 row created.
SQL> insert into cust values (1,'01-JAN-2010','10:00','12:00',6,'10-12 line 1');
1 row created.
SQL> insert into cust values (1,'01-JAN-2010','12:00','3:00',7,'12-3 line 1');
1 row created.
SQL> insert into cust values (1,'02-JAN-2010','08:00','09:00',1,'jan2 8-9 ine1');
1 row created.
SQL> insert into cust values (1,'02-JAN-2010','09:00','14:00',2,'jan2 9-14 line1');
1 row created.
SQL> insert into cust values (1,'02-JAN-2010',null,null,3,'jan2 9-14 line 2');
1 row created.
SQL> insert into cust values (1,'02-JAN-2010',null,null,4,'jan2 9-14 line 3');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from cust order by custno, cdate, seq;
CUSTNO CDATE FROM_TIME TO_TIME SEQ COMMENTS
--------------- --------- ---------- ---------- --------------- ------------------------------------
1 01-JAN-10 05:00 08:00 1 5-8 comment line 1
1 01-JAN-10 2 5-8 comment line 2
1 01-JAN-10 3 5-8 comment line 3
1 01-JAN-10 08:00 10:00 4 8-10 line 1
1 01-JAN-10 5 8-10 line 2
1 01-JAN-10 10:00 12:00 6 10-12 line 1
1 01-JAN-10 12:00 3:00 7 12-3 line 1
1 02-JAN-10 08:00 09:00 1 jan2 8-9 ine1
1 02-JAN-10 09:00 14:00 2 jan2 9-14 line1
1 02-JAN-10 3 jan2 9-14 line 2
1 02-JAN-10 4 jan2 9-14 line 3
11 rows selected.
The requirement is to concatenate the COMMENTS to the last not null value for from_time and to_time ordered by the seq column for each custno cdate and from
and to times. Tried various things with analytics and stragg but cannot quite nail the query needed. The ouput below is what is needed (added line spacing
for clarity)
CUSTNO CDATE FROM_TIME TO_TIME COMMENTS
--------------- --------- ---------- ---------- ------------------------------------
1 01-JAN-10 05:00 08:00 5-8 comment line 1, 5-8 comment line 2, 5-8 comment line 3
1 01-JAN-10 08:00 10:00 8-10 line 1, 8-10 line 2
1 01-JAN-10 10:00 12:00 10-12 line 1
1 01-JAN-10 12:00 3:00 12-3 line 1
1 02-JAN-10 08:00 09:00 jan2 8-9 ine1
1 02-JAN-10 09:00 14:00 jan2 9-14 line1, jan2 9-14 line 2, jan2 9-14 line 3
11 rows selected.
SQL> spool off
and Tom said...
ops$tkyte%ORA10GR2> select custno, cdate,
2 from_time,
3 last_value(from_time ignore nulls) over (partition by custno, cdate order by seq) new_from,
4 to_time,
5 last_value(to_time ignore nulls) over (partition by custno, cdate order by seq) new_to,
6 seq, comments
7 from cust
8 order by custno, cdate, seq;
CUSTNO CDATE FROM_TIME NEW_FROM TO_TIME NEW_TO SEQ COMMENTS
---------- --------- ---------- ---------- ---------- ---------- ---------- ----------------------------------------
1 01-JAN-10 05:00 05:00 08:00 08:00 1 5-8 comment line 1
1 01-JAN-10 05:00 08:00 2 5-8 comment line 2
1 01-JAN-10 05:00 08:00 3 5-8 comment line 3
1 01-JAN-10 08:00 08:00 10:00 10:00 4 8-10 line 1
1 01-JAN-10 08:00 10:00 5 8-10 line 2
1 01-JAN-10 10:00 10:00 12:00 12:00 6 10-12 line 1
1 01-JAN-10 12:00 12:00 3:00 3:00 7 12-3 line 1
1 02-JAN-10 08:00 08:00 09:00 09:00 1 jan2 8-9 ine1
1 02-JAN-10 09:00 09:00 14:00 14:00 2 jan2 9-14 line1
1 02-JAN-10 09:00 14:00 3 jan2 9-14 line 2
1 02-JAN-10 09:00 14:00 4 jan2 9-14 line 3
11 rows selected.
ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> column comm format a40
ops$tkyte%ORA10GR2> select custno, cdate, new_from, new_to, stragg( comments ) comm
2 from (
3 select custno, cdate,
4 from_time,
5 last_value(from_time ignore nulls) over (partition by custno, cdate order by seq) new_from,
6 to_time,
7 last_value(to_time ignore nulls) over (partition by custno, cdate order by seq) new_to,
8 seq, comments
9 from cust
10 )
11 group by custno, cdate, new_from, new_to
12 order by custno, cdate, new_from;
CUSTNO CDATE NEW_FROM NEW_TO COMM
---------- --------- ---------- ---------- ----------------------------------------
1 01-JAN-10 05:00 08:00 5-8 comment line 1,5-8 comment line 2,5-
8 comment line 3
1 01-JAN-10 08:00 10:00 8-10 line 1,8-10 line 2
1 01-JAN-10 10:00 12:00 10-12 line 1
1 01-JAN-10 12:00 3:00 12-3 line 1
1 02-JAN-10 08:00 09:00 jan2 8-9 ine1
1 02-JAN-10 09:00 14:00 jan2 9-14 line1,jan2 9-14 line 2,jan2 9-
14 line 3
6 rows selected.
Rating
(10 ratings)
Is this answer out of date? If it is, please let us know via a Comment