Home » SQL & PL/SQL » SQL & PL/SQL » Need a query to get the ouput in an expected format
Need a query to get the ouput in an expected format [message #640510] Wed, 29 July 2015 21:26 Go to next message
nyaduguru
Messages: 1
Registered: July 2015
Location: devon,pa
Junior Member
create table sample(USUBJID varchar2(20),LBDTC varchar2(20),VISIT varchar2(50),VISITNUM number(4));

insert into sample values('114-001','2014-07-10T09:30','Screening',1);
insert into sample values('114-001','2014-07-24T08:20','Unscheduled',9999);
insert into sample values('114-001','2014-07-24T09:10','Unscheduled',9999);
insert into sample values('114-001','2014-07-29T09:10','Unscheduled',9999);
insert into sample values('114-001','2014-08-07T08:16','Cycle 1 Day 1',101);
insert into sample values('114-001','2014-08-14T08:25','Cycle 1 Day 8',108);
insert into sample values('114-001','2014-08-28T08:42','Cycle 2 Day 1',201);
insert into sample values('114-001','2014-09-11T08:12','Cycle 2 Day 15',215);
insert into sample values('114-001','2014-09-25T08:34','Cycle 3 Day 1',301);
insert into sample values('114-001','2014-10-12T06:04','Unscheduled',9999);
insert into sample values('114-001','2014-10-13T05:37','Unscheduled',9999);
insert into sample values('114-001','2014-10-20T06:01','Unscheduled',9999);
insert into sample values('114-001','2015-01-15T08:55','Cycle 7 Day 1',701);

select * from sample;


The above is the data I have in SAMPLE table.

I need a query to get the output as follows. I have also atatched the file with the expected output.Any help is greatly appreciated.

USUBJID	LBDTC	                VISIT                            VISITNUM
114-001	2014-07-10T09:30	Screening	                   1
114-001	2014-07-24T08:20	Screening Unscheduled 1	           1.1
114-001	2014-07-24T09:10	Screening Unscheduled 1	           1.1
114-001	2014-07-29T09:10	Screening Unscheduled 2	   	   1.2
114-001	2014-08-07T08:16	Cycle 1 Day 1	   	           101
114-001	2014-08-14T08:25	Cycle 1 Day 8	   	           108
114-001	2014-08-28T08:42	Cycle 2 Day 1	   	           201
114-001	2014-09-11T08:12	Cycle 2 Day 15	   	           215
114-001	2014-09-25T08:34	Cycle 3 Day 1	   	           301
114-001	2014-10-12T06:04	Cycle 3 Day 1 Unscheduled 1	   301.1
114-001	2014-10-13T05:37	Cycle 3 Day 1 Unscheduled 2	   301.2
114-001	2014-10-20T06:01	Cycle 3 Day 1 Unscheduled 3	   301.3
114-001	2015-01-15T08:55	Cycle 7 Day 1	                   701



Thanks


Lalit : Added code tags. Please do it yourself in future.
  • Attachment: output.PNG
    (Size: 23.54KB, Downloaded 940 times)

[Updated on: Thu, 30 July 2015 00:20] by Moderator

Report message to a moderator

Re: Need a query to get the ouput in an expected format [message #640511 is a reply to message #640510] Wed, 29 July 2015 22:24 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum

Please read and follow the forum guidelines, to enable us to help you:
OraFAQ Forum Guide
How to use {code} tags and make your code easier to read

>I need a query to get the output as follows.

what are the written requirements that the SQL must satisfy?
Re: Need a query to get the ouput in an expected format [message #640513 is a reply to message #640510] Thu, 30 July 2015 00:33 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
>2014-07-10T09:30

Seems like a bad design. What does the above value signify? Why do you store it like that? Consider normalizing your data first.
Re: Need a query to get the ouput in an expected format [message #640514 is a reply to message #640510] Thu, 30 July 2015 01:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Something like:
SQL> col original_visit format a20
SQL> col new_visit format a30
SQL> with
  2    data as (
  3      select s.*,
  4             row_number() over (partition by USUBJID order by LBDTC) rn
  5      from sample s
  6    )
  7  select LBDTC, visit original_visit,
  8         case
  9           when visit = 'Unscheduled'
 10             then last_value(decode(visit, 'Unscheduled','', visit) ignore nulls)
 11                        over (partition by USUBJID order by LBDTC)
 12                  || ' ' || visit || ' ' ||
 13                  to_char(rn
 14                          - last_value(decode(visit, 'Unscheduled','', rn) ignore nulls)
 15                              over (partition by USUBJID order by LBDTC))
 16           else visit
 17         end new_visit,
 18         visitnum,
 19         case
 20           when visit = 'Unscheduled'
 21             then to_number(
 22                  to_char(last_value(decode(visit, 'Unscheduled','', visitnum) ignore nulls)
 23                            over (partition by USUBJID order by LBDTC))
 24                  || '.' ||
 25                  to_char(rn
 26                          - last_value(decode(visit, 'Unscheduled','', rn) ignore nulls)
 27                              over (partition by USUBJID order by LBDTC)))
 28           else visitnum
 29         end new_visitnum
 30  from data
 31  order by USUBJID, to_date(LBDTC,'YYYY-MM-DD"T"HH24:MI')
 32  /
LBDTC                ORIGINAL_VISIT       NEW_VISIT                        VISITNUM NEW_VISITNUM
-------------------- -------------------- ------------------------------ ---------- ------------
2014-07-10T09:30     Screening            Screening                               1            1
2014-07-24T08:20     Unscheduled          Screening Unscheduled 1              9999          1.1
2014-07-24T09:10     Unscheduled          Screening Unscheduled 2              9999          1.2
2014-07-29T09:10     Unscheduled          Screening Unscheduled 3              9999          1.3
2014-08-07T08:16     Cycle 1 Day 1        Cycle 1 Day 1                         101          101
2014-08-14T08:25     Cycle 1 Day 8        Cycle 1 Day 8                         108          108
2014-08-28T08:42     Cycle 2 Day 1        Cycle 2 Day 1                         201          201
2014-09-11T08:12     Cycle 2 Day 15       Cycle 2 Day 15                        215          215
2014-09-25T08:34     Cycle 3 Day 1        Cycle 3 Day 1                         301          301
2014-10-12T06:04     Unscheduled          Cycle 3 Day 1 Unscheduled 1          9999        301.1
2014-10-13T05:37     Unscheduled          Cycle 3 Day 1 Unscheduled 2          9999        301.2
2014-10-20T06:01     Unscheduled          Cycle 3 Day 1 Unscheduled 3          9999        301.3
2015-01-15T08:55     Cycle 7 Day 1        Cycle 7 Day 1                         701          701

(Assuming that:
114-001	2014-07-24T08:20	Screening Unscheduled 1	           1.1
114-001	2014-07-24T09:10	Screening Unscheduled 1	           1.1
is an error in your output.)
Re: Need a query to get the ouput in an expected format [message #640515 is a reply to message #640513] Thu, 30 July 2015 01:09 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Lalit Kumar B wrote on Thu, 30 July 2015 07:33
>2014-07-10T09:30

...What does the above value signify? ..


This is (almost - seconds are missing) ISO 8601 standard time format (which of course does not benefit of DATE data type value checks).

Re: Need a query to get the ouput in an expected format [message #640520 is a reply to message #640515] Thu, 30 July 2015 02:11 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
My bad eyes. When I saw the data type as varchar2 and not timestamp, I saw the character literal "T" as "TO". While that is just "T", and that is not character "O" but zero which is part of the "hour".
Previous Topic: Joining
Next Topic: NUMERIC DENOMINATION
Goto Forum:
  


Current Time: Mon Jul 27 21:26:54 CDT 2026