In my project in my_table time of any event is stored in number format. Now I have to convert it to oracle datetime format.
Here is an explanation below:
Example1 :
•Sched_Arr_Tm = 0450, will equal 04:30 am (the first 2 is HH (24 hour clock. Since 04 < 12, then just use that number as the hour)) and the next 2 are the fractional equivalent of an hour (.50 * 60 min = 30 min)
•Sched_Arr_Tm = 2100, will equal 9:00 PM (since 21> 12, then take 21-12=9)
•Sched_Arr_Tm = 1475, will equal 02:45 Pm (the first 2 is HH (24 hour clock. Since 14 > 12. Then take 14-12=2), then just use that number as the hour)) and the next 2 are the fractional equivalent of an hour (.75 * 60 min = 45 min)
•Sched_Arr_Tm = 0075, will equal 12:45 AM (since the hour = 00, then the hour= 12) and the next 2 are the fractional equivalent of an hour (.75 * 60 min = 45 min)
I am able to extract data according to above login but getting error while converting it to date.
select sched_arr_tm,
LPAD(substr(tn.sched_arr_tm, 1,length(tn.sched_arr_tm) - 2),2,'0') as HH,
RPAD(TRUNC(TO_NUMBER(substr(tn.sched_arr_tm,3,length(tn.sched_arr_tm) - 2)) * .60,0),2,'0') as MM,
'00' AS SS,
LPAD(substr(tn.sched_arr_tm,1,length(tn.sched_arr_tm) - 2),2,'0')
||':' ||
RPAD(TRUNC(TO_NUMBER(substr(tn.sched_arr_tm,3,length(tn.sched_arr_tm) - 2)) * .60,0),2,'0')
||':'||
LPAD(0,2,0) AS DTTM,
TO_DATE(LPAD(substr(tn.sched_arr_tm,1,length(tn.sched_arr_tm) - 2),2,'0')
||':' ||
RPAD(TRUNC(TO_NUMBER(substr(tn.sched_arr_tm,3,length(tn.sched_arr_tm) - 2)) * .60,0),2,'0')
||':'||
LPAD(00,2,0),'HH24:MI:SS') AS DTTM,
tn.sched_slip_arr_tm
from MY_TABLE;
I am getting this error:
ORA-01858: a non-numeric character was found where a numeric was expected.
you can do this with:
SQL> with data as (select 450 Sched_Arr_Tm from dual
2 union all
3 select 1475 from dual
4 union all
5 select 2100 from dual)
6 select Sched_Arr_Tm, to_date(hours||':'||(60*(mins/100)), 'hh24:mi')
7 from (select Sched_Arr_Tm, substr(Sched_Arr_Tm, -2) mins,
8 substr(Sched_Arr_Tm, 1, length(Sched_Arr_Tm)-2) hours
9 from data)
10 /
SCHED_ARR_TM TO_DATE(HOURS||':
------------ -----------------
450 01-jan-2013 04:30
1475 01-jan-2013 14:45
2100 01-jan-2013 21:00
SQL>
Related
It is necessary to find the average number of processed messages by the subscriber in 10 seconds within an hour. PLSQL
There are columns: subscriber, date ('dd.mm.yyyy hh.mm.ss') and messages, they are related (each message has its own time and the subscriber who processed it).
the idea was this:
SELECT subscriber, HH24, AVG(CNT) FROM (
SELECT subscriber,
trunc(date, 'HH24') HH24,
trunc(date - INTERVAL '10' SECOND) SS,
count(messages) CNT
FROM tables
where date IS NOT NULL
GROUP BY subscriber,
trunc(date, 'HH24'),
trunc(date - INTERVAL '10' SECOND)
order by subscriber)
GROUP BY subscriber, HH24
Expected Result:
subscriber HH24 CNT
subscriber 1 01.01.01 21:00:00 8,88
subscriber 1 01.01.01 22:00:00 7,88
subscriber 2 01.01.01 21:00:00 6,66
subscriber 3 01.01.01 22:00:00 5,54
My query produces something like the following:
subscriber HH24 CNT
subscriber 1 01.01.01 21:00:00 400
subscriber 1 01.01.01 22:00:00 500
subscriber 2 01.01.01 21:00:00 300
subscriber 3 01.01.01 22:00:00 500
Most likely my request does not group the data for 10 seconds, tell me where I made a mistake? Thank!
Please try this:
select subscriber, ss, count(сообщения) cnt from (
select subscriber,
trunc(date) + (floor((date-trunc(date))*24*60*60/10)*10)/24/60/60 as ss,
сообщения
from tables
where date is not null)
group by subscriber, ss;
I'd better explain the big expression:
(date-trunc(date))*24*60*60 -- Time expressed as number of seconds since midnight (T1)
(floor(T1/10)*10) -- ... rounded down to nearest 10 seconds (T2)
T2/24/60/60 -- ... converted to fraction of a day (T3)
trunc(date)+T3 -- ... added back on to date
Not clear what you are looking for. For this kind of task I use this generic function:
CREATE OR REPLACE FUNCTION MakeInterval(ts IN TIMESTAMP, roundInterval IN INTERVAL DAY TO SECOND) RETURN TIMESTAMP DETERMINISTIC IS
denom INTEGER;
BEGIN
IF roundInterval >= INTERVAL '1' HOUR THEN
denom := EXTRACT(HOUR FROM roundInterval);
IF MOD(24, denom) <> 0 THEN
RAISE VALUE_ERROR;
END IF;
RETURN TRUNC(ts) + TRUNC(EXTRACT(HOUR FROM ts) / denom) * denom * INTERVAL '1' HOUR;
ELSIF roundInterval >= INTERVAL '1' MINUTE THEN
denom := EXTRACT(MINUTE FROM roundInterval);
IF MOD(60, denom) <> 0 THEN
RAISE VALUE_ERROR;
END IF;
RETURN TRUNC(ts, 'hh') + TRUNC(EXTRACT(MINUTE FROM ts) / denom) * denom * INTERVAL '1' MINUTE;
ELSE
denom := EXTRACT(SECOND FROM roundInterval);
IF MOD(60, denom) <> 0 THEN
RAISE VALUE_ERROR;
END IF;
RETURN TRUNC(ts, 'mi') + TRUNC(EXTRACT(SECOND FROM ts) / denom) * denom * INTERVAL '1' SECOND;
END IF;
END MakeInterval;
Then you can use it simple as MakeInterval("date", INTERVAL '10' SECOND)
However, maybe you are looking for windowing_clause, for example
AVG(CNT) OVER (PARTITION BY subscriber ORDER BY "date" RANGE BETWEEN CURRENT_ROW AND INTERVAL '10' SECOND FOLLOWING)
I explained poorly, but the problem was solved as follows:
SELECT subscriber, HH24, AVG(cnt / 3600) * 100 / 10 cnt
AVG - average value, CNT / 3600 - divide by the number of seconds per hour, * 100 - translate in percent, / 10 - we get the answer for 10 seconds
I am performing the following Oracle selects:
Select 1 / 48 * 24 * 60 From Dual
result: 29.99999999999999999999999999999999999995
Select 1 / 48 * 60 * 24 From Dual
result: 30
Select 1 / 48 * (60 * 24) From Dual
result: 29.99999999999999999999999999999999999995
The interesting thing here is that if I multiply 1 / 48 * 24 the returned result is 0.499999999999... instead of 0.5. This leads to 29.999 result.
Why is this result returned? What should I do in order to avoid such calculations that seem to be different than expected?
Am having query,in which two fields and getting as output pps_id and total_weight. Here pps_id is the column from the table and total_weight we are calculating from inner query. after doing all process in query we are order by the query by total weight. Its taking more cost and response.Is there any way to improve this query performance.
SELECT PPS_ID, TOTAL_WEIGHT
FROM ( SELECT PPS_ID, TOTAL_WEIGHT
FROM (SELECT pps_id,
ROUND (
( ( (60 * name_pct_match / 100)
+ prs_weight
+ year_weight
+ dt_weight)
/ 90)
* 100)
total_weight
FROM (SELECT pps_id,
ROUND (func_compare_name ('aaaa',
UPPER (name_en),
' ',
60))
name_pct_match,
DECODE (prs_nationality_id, 99, 15, 0)
prs_weight,
10 mother_weight,
100 total_attrib_weight,
CASE
WHEN TO_NUMBER (
TO_CHAR (birth_date, 'yyyy')) =
1986
THEN
5
ELSE
0
END
year_weight,
CASE
WHEN TO_CHAR (
TO_DATE ('12-JAN-86',
'DD-MON-RRRR'),
'dd') =
TO_CHAR (birth_date, 'dd')
AND TO_CHAR (
TO_DATE ('12-JAN-86',
'DD-MON-RRRR'),
'mm') =
TO_CHAR (birth_date, 'mm')
THEN
10
WHEN TO_DATE ('12-JAN-86', 'DD-MON-RRRR') BETWEEN birth_date
- 6
AND birth_date
+ 6
THEN
8
WHEN TO_DATE ('12-JAN-86', 'DD-MON-RRRR') BETWEEN birth_date
- 28
AND birth_date
+ 28
THEN
5
WHEN TO_DATE ('12-JAN-86', 'DD-MON-RRRR') BETWEEN birth_date
- 90
AND birth_date
+ 90
THEN
3
ELSE
0
END
dt_weight
FROM individual_profile
WHERE birth_date = '12-JAN-86'
AND IS_ACTIVE = 1
AND gender_id = 1
AND ROUND (func_compare_name ('aaa',
UPPER (name_en),
' ',
60)) > 20))
WHERE TOTAL_WEIGHT >= 100
ORDER BY total_weight DESC)
WHERE ROWNUM <= 10
i have tried by splitting the query and put values in temp tables and tried but it also taking time. I want to improve the performance of the query
I have a millions of string record like this one with 310 types of them that have different format to get sequence,year,month and day from..
the script will get the sequence,year,month and day... now I want a Pl/Sql that will get the max and min value number of the sequence and find the missing number where is year and month are for example 14 - 06 how ??
You don't want to be looking at dual at all here; certainly not attempting to insert. You need to track the highest and lowest values you've seen as you iterate through the loop. based on some of the elements of ename representing dates I'm pretty sure you want all your matches to be 0-9, not 1-9. You're also referring to the cursor name as you access its fields, instead of the record variable name:
FOR List_ENAME_rec IN List_ENAME_cur loop
if REGEXP_LIKE(List_ENAME_rec.ENAME,'emp[-][0-9]{4}[_][0-9]{2}[_][0-9]{2}[_][0-9]{2}[_][0-9]{4}[_][G][1]') then
V_seq := substr(List_ENAME_rec.ename,5,4);
V_Year := substr(List_ENAME_rec.ename,10,2);
V_Month := substr(List_ENAME_rec.ename,13,2);
V_day := substr(List_ENAME_rec.ename,16,2);
if min_seq is null or V_seq < min_seq then
min_seq := v_seq;
end if;
if max_seq is null or V_seq > max_seq then
max_seq := v_seq;
end if;
end if;
end loop;
With values in the table of emp-1111_14_01_01_1111_G1 and emp-1115_14_02_02_1111_G1, that reports max_seq 1115 min_seq 1111.
If you really wanted to involve dual you could do this inside the loop, instead of the if/then/assign pattern, but it's not necessary:
select least(min_seq, v_seq), greatest(max_seq, v_seq)
into min_seq, max_seq
from dual;
I have no idea what the procedure is going to do; there seems to be no relationship between whatever you've got in test1 and the values you're finding.
You don't need any PL/SQL for this though. You can get the min/max values from a simple query:
select min(to_number(substr(ename, 5, 4))) as min_seq,
max(to_number(substr(ename, 5, 4))) as max_seq
from table1
where status = 2
and regexp_like(ename,
'emp[-][0-9]{4}[_][0-9]{2}[_][0-9]{2}[_][0-9]{2}[_][0-9]{4}[_][G][1]')
MIN_SEQ MAX_SEQ
---------- ----------
1111 1115
And you can use those to generate a list of all values in that range:
with t as (
select min(to_number(substr(ename, 5, 4))) as min_seq,
max(to_number(substr(ename, 5, 4))) as max_seq
from table1
where status = 2
and regexp_like(ename,
'emp[-][0-9]{4}[_][0-9]{2}[_][0-9]{2}[_][0-9]{2}[_][0-9]{4}[_][G][1]')
)
select min_seq + level - 1 as seq
from t
connect by level <= (max_seq - min_seq) + 1;
SEQ
----------
1111
1112
1113
1114
1115
And a slightly different common table expression to see which of those don't exist in your table, which I think is what you're after:
with t as (
select to_number(substr(ename, 5, 4)) as seq
from table1
where status = 2
and regexp_like(ename,
'emp[-][0-9]{4}[_][0-9]{2}[_][0-9]{2}[_][0-9]{2}[_][0-9]{4}[_][G][1]')
),
u as (
select min(seq) as min_seq,
max(seq) as max_seq
from t
),
v as (
select min_seq + level - 1 as seq
from u
connect by level <= (max_seq - min_seq) + 1
)
select v.seq as missing_seq
from v
left join t on t.seq = v.seq
where t.seq is null
order by v.seq;
MISSING_SEQ
-----------
1112
1113
1114
or if you prefer:
...
select v.seq as missing_seq
from v
where not exists (select 1 from t where t.seq = v.seq)
order by v.seq;
SQL Fiddle.
Based on comments I think you want the missing values for the sequence for each combination of the other elements of the ID (YY_MM_DD). This will give you that breakdown:
with t as (
select to_number(substr(ename, 5, 4)) as seq,
substr(ename, 10, 2) as yy,
substr(ename, 13, 2) as mm,
substr(ename, 16, 2) as dd
from table1
where status = 2
and regexp_like(ename,
'emp[-][0-9]{4}[_][0-9]{2}[_][0-9]{2}[_][0-9]{2}[_][0-9]{4}[_][G][1]')
),
r (yy, mm, dd, seq, max_seq) as (
select yy, mm, dd, min(seq), max(seq)
from t
group by yy, mm, dd
union all
select yy, mm, dd, seq + 1, max_seq
from r
where seq + 1 <= max_seq
)
select yy, mm, dd, seq as missing_seq
from r
where not exists (
select 1 from t
where t.yy = r.yy
and t.mm = r.mm
and t.dd = r.dd
and t.seq = r.seq
)
order by yy, mm, dd, seq;
With output like:
YY MM DD MISSING_SEQ
---- ---- ---- -------------
14 01 01 1112
14 01 01 1113
14 01 01 1114
14 02 02 1118
14 02 02 1120
14 02 03 1127
14 02 03 1128
SQL Fiddle.
If you want to look for a particular date you cold filter that (either in t, or the first branch in r), but you could also change the regex pattern to include the fixed values; so to look for 14 06 the pattern would be 'emp[-][0-9]{4}_14_06_[0-9]{2}[_][0-9]{4}[_][G][1]', for example. That's harder to generalise though, so a filter (where t.yy = '14' and t.mm = '06' might be more flexible.
If you insist in having this in a procedure, you can make the date elements optional and modify the regex pattern:
create or replace procedure show_missing_seqs(yy in varchar2 default '[0-9]{2}',
mm in varchar2 default '[0-9]{2}', dd in varchar2 default '[0-9]{2}') as
pattern varchar2(80);
cursor cur (pattern varchar2) is
with t as (
select to_number(substr(ename, 5, 4)) as seq,
substr(ename, 10, 2) as yy,
substr(ename, 13, 2) as mm,
substr(ename, 16, 2) as dd
from table1
where status = 2
and regexp_like(ename, pattern)
),
r (yy, mm, dd, seq, max_seq) as (
select yy, mm, dd, min(seq), max(seq)
from t
group by yy, mm, dd
union all
select yy, mm, dd, seq + 1, max_seq
from r
where seq + 1 <= max_seq
)
select yy, mm, dd, seq as missing_seq
from r
where not exists (
select 1 from t
where t.yy = r.yy
and t.mm = r.mm
and t.dd = r.dd
and t.seq = r.seq
)
order by yy, mm, dd, seq;
begin
pattern := 'emp[-][0-9]{4}[_]'
|| yy || '[_]' || mm || '[_]' || dd
|| '[_][0-9]{4}[_][G][1]';
for rec in cur(pattern) loop
dbms_output.put_line(to_char(rec.missing_seq, 'FM0000'));
end loop;
end show_missing_seqs;
/
I don't know why you insist it has to be done like this or why you want to use dbms_output as you're relying on the client/caller displaying that; what will your job do with the output? You could make this return a sys_refcursor which would be more flexible. but anyway, you can call it like this from SQL*Plus/SQL Developer:
set serveroutput on
exec show_missing_seqs(yy => '14', mm => '01');
anonymous block completed
1112
1113
1114
I don't know how to convert integer into percentage, please help me. Thank you
Here's the query:
SELECT 'Data' || ',' ||
TO_CHAR(D.DTIME_DAY,'MM/dd/yyyy') || ',' ||
NVL(o.CNT_OPENED,0) || ',' || --as cnt_opened
NVL(c.CNT_CLOSED,0) --as cnt_closed
FROM OWNER_DWH.DC_DATE d
LEFT JOIN (SELECT TRUNC(t.CREATE_TIME, 'MM') AS report_date,
count(*) AS cnt_opened
FROM APP_ACCOUNT.OTRS_TICKET t
WHERE t.CREATE_TIME BETWEEN SYSDATE -120 AND SYSDATE
GROUP BY TRUNC(t.CREATE_TIME, 'MM')) o
ON d.DTIME_DAY=o.REPORT_DATE
LEFT JOIN (SELECT TRUNC(t.CLOSE_TIME, 'MM') as report_date,
count(*) AS cnt_closed
FROM APP_ACCOUNT.OTRS_TICKET t
WHERE t.CLOSE_TIME BETWEEN SYSDATE -120 AND SYSDATE
GROUP BY TRUNC(t.CLOSE_TIME, 'MM')) c
ON D.DTIME_DAY=c.REPORT_DATE
WHERE d.DTIME_DAY BETWEEN SYSDATE -120 AND TRUNC(SYSDATE) -1 AND
d.DTIME_DAY = TRUNC(d.DTIME_DAY, 'MM') AND
TRUNC(d.DTIME_DAY,'MM')= d.DTIME_DAY
ORDER BY D.DTIME_DAY;
The output of that query:
Data,10/01/2013,219,201
Data,11/01/2013,249,234
Data,12/01/2013,228,224
Data,01/01/2014,269,256
example output that I need is like this:
Data,10/01/2013,219, 52%, 201, 45%
Data,11/01/2013,249, 75%, 234, 60%
.......
........
Formula:
create_time + close time = total / create_time (for cnt_opened each column) = percentage
create_time + close time = total / close_time (for cnt_closed each column) = percentage
Try this:
Basically just add the total of CNT_OPENED and CNT_CLOSED, then whichever you want to take the percentage of, multiply that by 100 and divide by the sum.
For instance, CNT_OPENED = 219 and CNT_CLOSED = 201 so the total is 420. Multiply CNT_OPENED by 100 and then divide by 420 -> (219 * 100) / 420 = 52. Do the same thing with CNT_CLOSED.
Note that this WILL result in an exception if both CNT_OPENED and CNT_CLOSED are 0.
SELECT 'Data'
||','||TO_CHAR(D.DTIME_DAY,'MM/dd/yyyy')
||','||NVL(o.CNT_OPENED,0) --as cnt_opened
||','||(NVL(o.CNT_OPENED,0) * 100) / (NVL(o.CNT_OPENED,0) + NVL(o.CNT_CLOSED,0)) || '%'
||','||NVL(c.CNT_CLOSED,0) --as cnt_closed
||','||(NVL(o.CNT_CLOSED,0) * 100) / (NVL(o.CNT_OPENED,0) + NVL(o.CNT_CLOSED,0)) || '%'
That will also potentially give you a million decimal places, so if you only want to take it out to a couple, simply use the TRUNC function and specify your precision (2 decimal places in this case):
TRUNC((NVL(o.CNT_OPENED,0) * 100) / (NVL(o.CNT_OPENED,0) + NVL(o.CNT_CLOSED,0)), 2)