Full Outer Join Subqueries works until last part then ORA-013113 - sql
I've been task to implement a query that will identify and provide datetime marks using a specific date and time range for each user. Basically first scan last scan throughout the entire shift at specific intervals.
I set a UI that prompts user for date time that will be used within the query and then smaller subqueries to parse out the data I need. Individually every works as I expect but when I get to the last set it breaks and I encounter the following error:
ORA-03113: end-of-file on communication channel
I can't seem to work around this or think of an alternative way to structure the query to what I need.
I need help w/ either a better method of achieving the same results or a fix to what I already have in place.
This is for an Oracle database 12c
With
UI As
( Select
-- Deactivate UI While testing
#('Start of Shift', #DATETIME) sDATE,
#('End of Shift', #DATETIME) EDate
#Oracle(From Dual) -- Oracle don't allow "Select from Nothing"
)
Select s90.uname, Trim(s90.fname)||' '||(s90.lname) "_Full Name", MIN(FRST.datetimecol) "_Day Start", Max(FRST.datetimecol) "_Before 1st break"
, MIN(SCND.datetimecol) "_After 1st Break", Max(SCND.datetimecol) "_Before Lunch"
, MIN(THRD.datetimecol) "_After Lunch", MAX(THRD.datetimecol) "_Before Last Break"
, MIN(FRTH.datetimecol) "_After Last Break", MAX(FRTH.datetimecol) "_End of Shift"
From
(Select T1.Username, t1.datetimecol
From
syslogtable T1
where datetimecol > (Select sdate from UI) and datetimecol < (Select edate from UI) and to_char(datetimecol, 'hh24:mi:ss') >= '05:00:00' and to_char(datetimecol, 'hh24:mi:ss') <= '08:00:59') FRST
Full Outer Join
(Select T2.Username, t2.datetimecol
From
syslogtable T2
where datetimecol > (Select sdate from UI) and datetimecol < (Select edate from UI) and to_char(datetimecol, 'hh24:mi:ss') >= '08:00:00' and to_char(datetimecol, 'hh24:mi:ss') <= '10:00:59') SCND
On FRST.Username = SCND.Username
-- code continues in same format then I close out the fourth part w/ below
-- from frst to thrd - no issues but when add frth it stops
,
usermastertable s90
Where FRST.Username = s90.uname
Group By s90.uname, Trim(s90.fname)||' '||(s90.lname)
Order by s90.uname
I expect to receive usernamecol, full name then the date/time stamps in the applicable columns
Related
Group Timestamps into intervals of 5 minutes, take value that's closest to timestamp and always give out a value
I'm new to SQL coding and would heavily appreciate help for a problem I'm facing. I have the following SQL script, that gives me the following output (see picture 1): WITH speicher as( select a.node as NODE_ID, d.name_0 as NODE_NAME, d.parent as PARENT_ID, c.time_stamp as ZEITSTEMPEL, c.value_num as WERT, b.DESCRIPTION_0 as Beschreibung, TO_CHAR(c.time_stamp, 'HH24:MI:SS') as Uhrzeit from p_value_relations a, l_nodes d, p_values b, p_value_archive c where a.node in (select sub_node from l_node_relations r where r.node in ( 50028, 49989, 49848 )) and a.node = d.id and (b."DESCRIPTION_0" like 'Name1' OR b."DESCRIPTION_0" like 'Name2') and c.time_stamp between SYSDATE-30 AND SYSDATE-1 and a.value = b.id and b.id = c.value) SELECT WERT as Value, NODE_NAME, ZEITSTEMPEL as Timestamp, Uhrzeit as Time, Beschreibung as Category FROM speicher I would like to create time intervals of 5 minutes to output the value. It should always choose the value closest above one on the defined time interval time stamps. If there is no value inside a set 5 minute intervall it should still give out the last value it finds, since the value has not changed in that case. To see what I mean please see the following picture. Any help wold be greatly appreciated. This data is from an oracle database. Result until now [ Result I would like
Since I do not understand your data, and can't test with it, I present something I could test with. My data has a table which tracks when folks login to a system. This is not intended as a complete answer, but as something to potentially point you in the right direction; with time_range as ( select rownum, sysdate - (1/288)*rownum time_stamp from dual connect By Rownum <= 288*30 ) select time_stamp, min(LOGIN_TIME) from time_range left outer join WEB_SECURITY_LOGGED_IN on LOGIN_TIME >= time_stamp group by time_stamp order by 1; Good luck... Edit: The with part of the query builds a time_stamp column which has one row for every 5 minutes for the last 30 days. The query portion joins to my login log table which I get the login which is the smallest date/time greater than the time_stamp.
tdate issue I'm facing in SQL query
While fetching count from table by using following query Select count(*) from tab where tdate = '17-05-19' ---> output 0 or Select count(*) from tab where trunc(tdate) = '17-05-19' ---->output 0 If I use: Select count(*) from tab where tdate >sysdate - 1 ---> it returns some count(yesterday+some of the today txn) But here I want only yesterday txn whenever I fire this query.
But here I want only yesterday txn whenever I fire this query. You may use this. Select count (*) from tab where tdate >= TRUNC(SYSDATE) - 1 AND tdate < TRUNC(SYSDATE) The advantage of this over using TRUNC on the date column is that it will utilize an index if it exists over tdate
If you tried by using Select count(*) from tab where trunc(tdate) = date'2019-05-17' (or, you could use Select count(*) from tab where to_char(tdate,'dd-mm-yy') = '17-05-19' by formatting through to_char function or, you could use Select count(*) from tab where trunc(tdate) = trunc(sysdate)-1 to get only the data for the day before ) you'd get some results provided you have data for the date 17th May. So, you need to provide a formatting for your literal as date'2019-05-17'(known as date literal) especially for Oracle DB, it might be used as '2019-05-17' without date part in MySQL as an example. Btw, trunc function is used to extract the date portion, and remove the time part of a date type column value. If your table is populated with huge data, therefore performance may matter, then you can even create functional index on trunc(tdate). Demo
Query to find out entries where dates don't overlap
Can anyone help me create a query which will populate a list of DJs who are not already booked in. My user will select a start date (and time), and an end date (and time) - and then click a button to select a DJ. I only want those DJs which are available between those time slots to appear in the list. Here are the two tables which are involved all I need in the listbox is the DJ Number, and the DJ Name So far I have this... but it isn't working: SELECT tblDJ.DJ_No AS [DJ ID], tblDJ.DJ_Name AS Name FROM tblDJ WHERE (((tblDJ.[DJ_No]) Not In (SELECT tblBooking.[FK_DJ_No] FROM tblBooking WHERE ( (tblBooking.End_Date) >= 01-04-2020 19:30:00 )))) ....etc.... I'm just entering a date in here for now, but obviously it will be stored in a variable once implemented. Thanks
Implementing OVERLAPS of two intervals would look like: 1st_start_date <= 2nd_end_date and 1st_end_date >= 2nd_start_date where 1st and 2nd values are markers of different events. You could use that logic in combination with NOT EXISTS to discard those djs that are unavailable at a given time: select dj_no, dj_name from tbldj d where not exists ( select 1 from tblbooking b where b.fk_dj_no = d.dj_no and b.start_date <= #END DATE# and b.end_date >= #START DATE# ) You just need to replace #START DATE# and #END DATE# with your values. This does work because there are following assumptions: Start date of the first event is prior to end date of that event Start date of the second event is prior to end date of that event Which seems logical, right?
The date in the SQL needs to be wrapped between two # in order for MS-Access to recognize it as a date: select * from tblDJ where DJ_No not in ( select FK_DJ_No from tblBooking where End_Date >= #2020-04-01 19:30:00# ) Other than that you query will work.
counting date and time for historical reporting
I am currently working on a query that will be used in junction with share-point to run reports. I have a query that I know will work with Oracle, but the company I am working for is running SQL Server 2005. What the report will do is give the person the ability to select any date and time, and give the count for that specific operation. The problem is that there are large gaps in the time stamps (because it takes a little while for the product to get to the next operation). The date type is varchar, so i used substrings to parse out the year, month, day, and time. I have sample data available. The people looking at the reports want the ability to say at this time and day how many units went through this operation. I know this is is confusing, let me know if you need any clarification. Here is the oracle syntax SELECT T3.PAYMENT_DATE, T3."Hr", T3."Min", (SELECT COUNT(*) FROM INVOICE_ARCHIVE T4 WHERE TO_NUMBER(TO_CHAR(T4.PAYMENT_DATE, 'MM')) <= T3."Hr" AND TO_NUMBER(TO_CHAR(T4.PAYMENT_DATE, 'DD')) <= T3."Min") AS "NUM" FROM(SELECT T1.PAYMENT_DATE, T2."Hr", T2."Min" FROM (SELECT (FLOOR((LEVEL + 359)/60)) AS "Hr", MOD((LEVEL + 359), 60) AS "Min" FROM dual CONNECT BY LEVEL <= 961) T2, INVOICE_ARCHIVE T1 ORDER BY T1.PAYMENT_DATE, T2."Hr", T2."Min") T3
The answer to your question is the datepart() function in SQL Server. This will allow you to extract minutes and hours from dates. The harder part is the "connect by level" portion. How is this being used? You might need to use recursive CTEs to handle this. With the little hint from spencer, the following may suffice for your query: SELECT T3.PAYMENT_DATE, T3."Hr", T3."Min", (SELECT COUNT(*) FROM INVOICE_ARCHIVE T4 WHERE datepart(month, T4.PAYMENT_DATE) <= T3."Hr" AND datepart(day, T4.PAYMENT_DATE, 'DD') <= T3."Min" ) AS "NUM" FROM (SELECT T1.PAYMENT_DATE, T2."Hr", T2."Min" FROM (SELECT top 961 (FLOOR((LEVEL + 359)/60)) AS "Hr", MOD((LEVEL + 359), 60) AS "Min" FROM (select top 961 row_number() over (order by (select NULL)) as level from invoice_archive ) t ) T2 cross join INVOICE_ARCHIVE T1 ) T3 ORDER BY T3.PAYMENT_DATE, T3."Hr", T3."Min" I made the following changes: Changed the date arithmetic to use datepart() instead of to_char() . Replaced the method for getting a list of numbers, by using row_number() instead of connect by level Made the cross join explicit Moved the order by to the outer query, since neither SQL Server nor Oracle guarantee the results of an order by in a subquery (and SQL Server does not allow it unless you have a "TOP" query)
Calculating working days including holidays between dates without a calendar table in oracle SQL
Okay, so I've done quite a lot of reading on the possibility of emulating the networkdays function of excel in sql, and have come to the conclusion that by far the easiest solution is to have a calendar table which will flag working days or non working days. However, due to circumstances out of my control, we don't have access to such a luxury and it's unlikely that we will any time in the near future. Currently I have managed to bodge together what is undoubtedly a horrible ineffecient query in SQL that does work - the catch is, it will only work for a single client record at a time. SELECT O_ASSESSMENTS.ASM_ID, O_ASSESSMENTS.ASM_START_DATE, O_ASSESSMENTS.ASM_END_DATE, sum(CASE When TO_CHAR(O_ASSESSMENTS.ASM_START_DATE + rownum -1,'Day') = 'Sunday ' THEN 0 When TO_CHAR(O_ASSESSMENTS.ASM_START_DATE + rownum -1,'Day') = 'Saturday ' THEN 0 WHEN O_ASSESSMENTS.ASM_START_DATE + rownum - 1 IN ('03-01-2000','21-04-2000','24-04-2000','01-05-2000','29-05-2000','28-08-2000','25-12-2000','26-12-2000','01-01-2001','13-04-2001','16-04-2001','07-05-2001','28-05-2001','27-08-2001','25-12-2001','26-12-2001','01-01-2002','29-03-2002','01-04-2002','06-04-2002','03-06-2002','04-06-2002','26-08-2002','25-12-2002','26-12-2002','01-01-2003','18-04-2003','21-04-2003','05-05-2003','26-05-2003','25-08-2003','25-12-2003','26-12-2003','01-01-2004','09-04-2004','12-04-2004','03-05-2004','31-05-2004','30-08-2004','25-12-2004','26-12-2004','27-12-2004','28-12-2004','01-01-2005','03-01-2005','25-03-2005','28-03-2005','02-05-2005','30-05-2005','29-08-2005','27-12-2005','28-12-2005','02-01-2006','14-04-2006','17-04-2006','01-05-2006','29-05-2006','28-08-2006','25-12-2006','26-12-2006','02-01-2007','06-04-2007','09-04-2007','07-05-2007','28-05-2007','27-08-2007','25-12-2007','26-12-2007','01-01-2008','21-03-2008','24-03-2008','05-05-2008','26-05-2008','25-08-2008','25-12-2008','26-12-2008','01-01-2009','10-04-2009','13-04-2009','04-05-2009','25-05-2009','31-08-2009','25-12-2009','28-12-2009','01-01-2010','02-04-2010','05-04-2010','03-05-2010','31-05-2010','30-08-2010','24-12-2010','27-12-2010','28-12-2010','31-12-2010','03-01-2011','22-04-2011','25-04-2011','29-04-2011','02-05-2011','30-05-2011','29-08-2011','26-12-2011','27-12-2011') THEN 0 ELSE 1 END)-1 AS Week_Day From O_ASSESSMENTS, ALL_OBJECTS WHERE O_ASSESSMENTS.ASM_QSA_ID IN ('TYPE1') AND O_ASSESSMENTS.ASM_END_DATE >= '01/01/2012' AND O_ASSESSMENTS.ASM_ID = 'A00000' AND ROWNUM <= O_ASSESSMENTS.ASM_END_DATE-O_ASSESSMENTS.ASM_START_DATE+1 GROUP BY O_ASSESSMENTS.ASM_ID, O_ASSESSMENTS.ASM_START_DATE, O_ASSESSMENTS.ASM_END_DATE Basically, I'm wondering if a) I should stop wasting my time on this or b) is it possible to get this to work for multiple clients? Any pointers appreciated thanks! Edit: Further clarification - I already work out timescales using excel, but it would be ideal if we could do it in the report as the report in question is something that we would like end users to be able to run without any further manipulation. Edit: MarkBannister's answer works perfectly albeit slowly (though I had expected as much given it's not the preferred solution) - the challenge now lies in me integrating this into an existing report! with calendar_cte as (select to_date('01-01-2000')+level-1 calendar_date, case when to_char(to_date('01-01-2000')+level-1, 'day') in ('sunday ','saturday ') then 0 when to_date('01-01-2000')+level-1 in ('03-01-2000','21-04-2000','24-04-2000','01-05-2000','29-05-2000','28-08-2000','25-12-2000','26-12-2000','01-01-2001','13-04-2001','16-04-2001','07-05-2001','28-05-2001','27-08-2001','25-12-2001','26-12-2001','01-01-2002','29-03-2002','01-04-2002','06-04-2002','03-06-2002','04-06-2002','26-08-2002','25-12-2002','26-12-2002','01-01-2003','18-04-2003','21-04-2003','05-05-2003','26-05-2003','25-08-2003','25-12-2003','26-12-2003','01-01-2004','09-04-2004','12-04-2004','03-05-2004','31-05-2004','30-08-2004','25-12-2004','26-12-2004','27-12-2004','28-12-2004','01-01-2005','03-01-2005','25-03-2005','28-03-2005','02-05-2005','30-05-2005','29-08-2005','27-12-2005','28-12-2005','02-01-2006','14-04-2006','17-04-2006','01-05-2006','29-05-2006','28-08-2006','25-12-2006','26-12-2006','02-01-2007','06-04-2007','09-04-2007','07-05-2007','28-05-2007','27-08-2007','25-12-2007','26-12-2007','01-01-2008','21-03-2008','24-03-2008','05-05-2008','26-05-2008','25-08-2008','25-12-2008','26-12-2008','01-01-2009','10-04-2009','13-04-2009','04-05-2009','25-05-2009','31-08-2009','25-12-2009','28-12-2009','01-01-2010','02-04-2010','05-04-2010','03-05-2010','31-05-2010','30-08-2010','24-12-2010','27-12-2010','28-12-2010','31-12-2010','03-01-2011','22-04-2011','25-04-2011','29-04-2011','02-05-2011','30-05-2011','29-08-2011','26-12-2011','27-12-2011','01-01-2012','02-01-2012') then 0 else 1 end working_day from dual connect by level <= 1825 + sysdate - to_date('01-01-2000') ) SELECT a.ASM_ID, a.ASM_START_DATE, a.ASM_END_DATE, sum(c.working_day)-1 AS Week_Day From O_ASSESSMENTS a join calendar_cte c on c.calendar_date between a.ASM_START_DATE and a.ASM_END_DATE WHERE a.ASM_QSA_ID IN ('TYPE1') and a.ASM_END_DATE >= '01/01/2012' GROUP BY a.ASM_ID, a.ASM_START_DATE, a.ASM_END_DATE
There are a few ways to do this. Perhaps the simplest might be to create a CTE that produces a virtual calendar table, based on Oracle's connect by syntax, and then join it to the Assesments table, like so: with calendar_cte as ( select to_date('01-01-2000')+level-1 calendar_date, case when to_char(to_date('01-01-2000')+level-1, 'Day') in ('Sunday ','Saturday ') then 0 when to_date('01-01-2000')+level-1 in ('03-01-2000','21-04-2000','24-04-2000','01-05-2000','29-05-2000','28-08-2000','25-12-2000','26-12-2000','01-01-2001','13-04-2001','16-04-2001','07-05-2001','28-05-2001','27-08-2001','25-12-2001','26-12-2001','01-01-2002','29-03-2002','01-04-2002','06-04-2002','03-06-2002','04-06-2002','26-08-2002','25-12-2002','26-12-2002','01-01-2003','18-04-2003','21-04-2003','05-05-2003','26-05-2003','25-08-2003','25-12-2003','26-12-2003','01-01-2004','09-04-2004','12-04-2004','03-05-2004','31-05-2004','30-08-2004','25-12-2004','26-12-2004','27-12-2004','28-12-2004','01-01-2005','03-01-2005','25-03-2005','28-03-2005','02-05-2005','30-05-2005','29-08-2005','27-12-2005','28-12-2005','02-01-2006','14-04-2006','17-04-2006','01-05-2006','29-05-2006','28-08-2006','25-12-2006','26-12-2006','02-01-2007','06-04-2007','09-04-2007','07-05-2007','28-05-2007','27-08-2007','25-12-2007','26-12-2007','01-01-2008','21-03-2008','24-03-2008','05-05-2008','26-05-2008','25-08-2008','25-12-2008','26-12-2008','01-01-2009','10-04-2009','13-04-2009','04-05-2009','25-05-2009','31-08-2009','25-12-2009','28-12-2009','01-01-2010','02-04-2010','05-04-2010','03-05-2010','31-05-2010','30-08-2010','24-12-2010','27-12-2010','28-12-2010','31-12-2010','03-01-2011','22-04-2011','25-04-2011','29-04-2011','02-05-2011','30-05-2011','29-08-2011','26-12-2011','27-12-2011') then 0 else 1 end working_day from dual connect by level <= 36525 + sysdate - to_date('01-01-2000') ) SELECT a.ASM_ID, a.ASM_START_DATE, a.ASM_END_DATE, sum(c.working_day) AS Week_Day From O_ASSESSMENTS a join calendar_cte c on c.calendar_date between a.ASM_START_DATE and a.ASM_END_DATE WHERE a.ASM_QSA_ID IN ('TYPE1') and a.ASM_END_DATE >= '01/01/2012' -- and a.ASM_ID = 'A00000' GROUP BY a.ASM_ID, a.ASM_START_DATE, a.ASM_END_DATE This will produce a virtual table populated with dates from 01 January 2000 to 10 years after the current date, with all weekends marked as non-working days and all days specified in the second in clause (ie. up to 27 December 2011) also marked as non-working days. The drawback of this method (or any method where the holiday dates are hardcoded into the query) is that each time new holiday dates are defined, every single query that uses this approach will have to have those dates added.
If you can't use a calendar table in Oracle, you might be better off exporting to Excel. Brute force always works. Networkdays() "returns the number of whole working days between start_date and end_date. Working days exclude weekends and any dates identified in holidays." Excluding weekends seems fairly straightforward. Every 7-day period will contain two weekend days. You'll just need to take some care with the leftover days. Holidays are a different story. You have to either store them or pass them as an argument. If you could store them, you'd store them in a calendar table, and your problem would be over. But you can't do that. So you're looking at passing them as an argument. Off the top of my head--and I haven't had any tea yet this morning--I'd consider a common table expression or a wrapper for a stored procedure.