Link to home
Start Free TrialLog in
Avatar of Eddie Shipman
Eddie ShipmanFlag for United States of America

asked on

Oracle and DateTime math

I'm writing a query to select some records. I have this data (Table name Sessions):
StartDate   SessionDay StartTime
11/1/2016   1          1000
11/1/2016   1          1400
11/1/2016   8          1000
11/1/2016   8          1400
11/1/2016   15         1000
11/1/2016   15         1400

Open in new window

What I need is some way to match records from another table where I have this data (Table name WAL):
StartTime               Key
11/1/2016 10:00:00 AM   6341371019318098180
11/1/2016 2:00:00 PM    7561779448126279684
11/8/2016 10:00:00 AM   6904435321948802820
11/8/2016 2:00:00 PM    7998296559469684996
11/15/2016 10:00:00 AM  4690144247933554180
11/15/2016 2:00:00 PM   7931460546152111876

Open in new window

How could I write my where clause to match the WAL records to the session records and return the correct Key for the correct Session? This would be result:
StartDate   SessionDay StartTime  Key                
11/1/2016   1          1000       6341371019318098180
11/1/2016   1          1400       7561779448126279684
11/1/2016   8          1000       6904435321948802820
11/1/2016   8          1400       7998296559469684996
11/1/2016   15         1000       4690144247933554180
11/1/2016   15         1400       7931460546152111876

Open in new window

Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Please provide the data types for all the columns involved.
Assuming WAL's startyime is a varchar2, I would convert it to a date then back into the individual values and do a simple join to sessions.

Here is the conversion of WAL.  Then just add the join.

with cte as (
select to_date(starttime,'MM/DD/YYYY HH:MI:SS AM') starttime_as_date from wal
)
select to_char(starttime_as_date,'fmMM/"1"/YYYY') startdate, to_char(starttime_as_date,'fmDD'), to_char(starttime_as_date,'HH24MI') starttime
from cte
/
Try this as a complete example:
with cte as (
select to_date(starttime,'MM/DD/YYYY HH:MI:SS AM') starttime_as_date, key from wal
)
select startdate, sessionday, s.starttime, key
from sessions s
	join cte c on s.startdate=to_char(starttime_as_date,'fmMM/"1"/YYYY')
		and s.sessionday=to_char(starttime_as_date,'fmDD')
		and s.starttime= to_char(starttime_as_date,'HH24MI')
/

Open in new window

Avatar of Eddie Shipman

ASKER

I'm writing a query to select some records. I have this data:
Event Table
------------------
Definition
    EVE_RID         NUMBER(10)
    EVE_START_DATE  Date

Data:
    EVE_RID     EVE_START_DATE
    156891      11/1/2016

Agenda Table
------------------
Definition:
    AGD_EVE_RID     NUMBER(10)
    AGD_DAY         NUMBER(2)
    AGD_START_TIME  NUMBER(4)

Data:
AGD_EVE_RID     AGD_DAY     AGD_START_TIME
156891          1           1000     
156891          1           1400     
156891          8           1000     
156891          8           1400     
156891          15          1000     
156891          15          1400     

WAList Table
------------------
Definition:
    WAL_STARTTIME   DATE
    WAL_KEY         VARCHAR2(50)

Data:
WAL_STARTTIME           WAL_KEY                
11/1/2016 10:00:00 AM   6341371019318098180
11/1/2016 2:00:00 PM    7561779448126279684
11/8/2016 10:00:00 AM   6904435321948802820
11/8/2016 2:00:00 PM    7998296559469684996
11/15/2016 10:00:00 AM  4690144247933554180
11/15/2016 2:00:00 PM   7931460546152111876

Open in new window


What I need is some way to match records from the WAList table from the Agenda table.
How could I write my where clause to match the WALList records to the Event.EVE_START_DATE + Agenda.AGD_DAY records and return the correct Key for the correct day? This would be result:
EVE_START_DATE   AGD_DAY    AGD_START_TIME  WAL_KEY                
11/1/2016       1           1000            6341371019318098180
11/1/2016       1           1400            7561779448126279684
11/1/2016       8           1000            6904435321948802820
11/1/2016       8           1400            7998296559469684996
11/1/2016       15          1000            4690144247933554180
11/1/2016       15          1400            7931460546152111876

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.