How to get data from oracle database in hourly basis - sql

i have one table call CMH_VITALSIGN, There is one column in this table as Time, which stores date and time (3/2/2016 11:33:17 AM). Now i want to get data from this table on hourly basis. MY Table columns is ..
(
VITAL_SIGNNO NUMBER,
CONSULT_NO VARCHAR2(16 BYTE),
REG_NO VARCHAR2(16 BYTE),
ADMISSION_NO VARCHAR2(16 BYTE),
DN_NO VARCHAR2(16 BYTE),
CONSULT_DT DATE,
TEMP_C NUMBER(4,2),
TEMP_F NUMBER(5,2),
PULSE NUMBER(3),
RR NUMBER(2),
BP_HIGHER NUMBER(5,2),
BP_LOWER NUMBER(5,2),
RESP NUMBER(3),
SPECIALITY_NO VARCHAR2(16 BYTE),
ENTERED_BY VARCHAR2(16 BYTE),
ENTRY_TIMESTAMP DATE,
UPDATED_BY VARCHAR2(16 BYTE),
UPDATE_TIMESTAMP DATE,
COMPANY_NO VARCHAR2(10 BYTE) DEFAULT 1,
SL_NO VARCHAR2(16 BYTE)
)
now i want to show data as line chart hourly base...
SELECT to_date(ENTRY_TIMESTAMP) DAY,
DECODE(TO_CHAR(ENTRY_TIMESTAMP,'HH24'),'11',pulse, 0) "00",
DECODE(TO_CHAR(ENTRY_TIMESTAMP,'HH24'),'12',pulse,0)"01",
DECODE(TO_CHAR(ENTRY_TIMESTAMP,'HH24'),'13',pulse,0) "02",
DECODE(TO_CHAR(ENTRY_TIMESTAMP,'HH24'),'14',pulse,0) "03"
FROM CMH_VITALSIGN
WHERE pulse IS NOT NULL
--where to_date(ENTRY_TIMESTAMP) ='01_MAY-2010'
--GROUP by to_char(ENTRY_TIMESTAMP,'YYYY-MON-DD'), to_date(ENTRY_TIMESTAMP)
ORDER BY to_date(ENTRY_TIMESTAMP);
Can anybody help me in this query.

If you want calculate data per every hour try implement this:
SELECT TO_CHAR( d, 'yyyy-mm-dd hh24') hour, COUNT(*)
FROM (SELECT SYSDATE + LEVEL / 26 / 60 d
FROM DUAL
CONNECT BY LEVEL < 1000)
GROUP BY TO_CHAR( d, 'yyyy-mm-dd hh24')

SELECT TO_CHAR ( ENTRY_TIMESTAMP, 'HH24') AS hour_num,
PULSE,
TEMP_C,
TEMP_F,
RR,
BP_HIGHER,
BP_LOWER,
RESP
FROM cmh_vitalsign
WHERE TRUNC (ENTRY_TIMESTAMP) = TO_DATE ( '3/2/2016', 'MM/DD/YYYY')
and ADMISSION_NO = 'A011009011297';

Related

Oracle procedure inserting multiple rows from result of query

I am attempting to create a procedure that will INSERT multiple rows into a table from the results of a query in the procedure.
The setup below works fine but I am having difficulty inserting the employee_id, timeoff_date from the output of the query into the timeoff table.
Below is my test CASE. I'm testing in live sql so we can both have the same Oracle version. Any help would be greatly appreciated.
CREATE OR REPLACE TYPE obj_date IS OBJECT (
date_val DATE
);
CREATE OR REPLACE TYPE nt_date IS TABLE OF obj_date;
create or replace function generate_dates_pipelined(
p_from in date,
p_to in date
)
return nt_date
pipelined
is
begin
for c1 in (
with calendar (start_date, end_date ) as (
select trunc(p_from), trunc(p_to) from dual
union all
select start_date + 1, end_date
from calendar
where start_date + 1 <= end_date
)
select start_date as day
from calendar
) loop
pipe row (obj_date(c1.day));
end loop;
return;
end generate_dates_pipelined;
create table holidays(
holiday_date DATE not null,
holiday_name VARCHAR2(20),
constraint holidays_pk primary key (holiday_date),
constraint is_midnight check ( holiday_date = trunc ( holiday_date ) )
);
INSERT into holidays (HOLIDAY_DATE,HOLIDAY_NAME) WITH dts as (
select to_date('01-AUG-2021 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'August 1st 2021' from dual union all
select to_date('05-AUG-2021 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'August 5th 2021' from dual) SELECT * from dts;
Create table employees(
employee_id NUMBER(6),
first_name VARCHAR2(20),
last_name VARCHAR2(20),
card_num VARCHAR2(10),
work_days VARCHAR2(7)
);
ALTER TABLE employees
ADD ( CONSTRAINT employees_pk
PRIMARY KEY (employee_id));
INSERT INTO employees
(
EMPLOYEE_ID,
first_name,
last_name,
card_num,
work_days)
WITH names AS (
SELECT 1, 'Jane', 'Doe','F123456', 'NYYYYYN' FROM dual UNION ALL
SELECT 2, 'Madison', 'Smith','R33432','NYYYYYN'FROM dual UNION ALL
SELECT 3, 'Justin', 'Case','C765341','NYYYYYN'FROM dual UNION ALL
SELECT 4, 'Mike', 'Jones', 'D564311','NYYYYYN' FROM dual) SELECT * FROM names;
create table timeoff(
seq_num integer GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
employee_id NUMBER(6),
timeoff_date DATE,
timeoff_type VARCHAR2(1) DEFAULT 'V',
constraint timeoff_chk check (timeoff_date=trunc(timeoff_date, 'dd')),
constraint timeoff_pk primary key (employee_id, timeoff_date)
);
CREATE OR REPLACE PROCEDURE create_timeoff_requests (start_date DATE, end_date DATE)
IS
type t_date is table of date;
l_res t_date;
BEGIN
SELECT
c.date_val
BULK COLLECT INTO l_res
FROM employees e
INNER JOIN TABLE (generate_dates_pipelined (start_date, end_date))c
PARTITION BY ( e.employee_id )
ON (SUBSTR(e.work_days, TRUNC(c.date_val) - TRUNC(c.date_val, 'IW') + 1, 1) = 'Y')
WHERE NOT EXISTS (
SELECT 1
FROM holidays h
WHERE c.date_val = h.holiday_date
)
ORDER BY
e.employee_id,
c.date_val;
-- debug
-- for i in 1..l_res.count -- loop
--dbms_output.put_line(l_res(i));
-- end loop;
END;
EXEC create_timeoff_requests (DATE '2021-08-01', DATE '2021-08-10');
I think it might be easier to create a procedure which looks like this (Just replace the declared constants in the block with parameters in your procedure definition):
CREATE PROCEDURE create_timeoff_requests
(
p_dStart DATE,
p_dEnd DATE,
p_nEmployeeID INTEGER
p_sType VARCHAR2
)
IS
BEGIN
INSERT INTO timeoff (employee_id, timeoff_date, timeoff_type)
SELECT e.employee_id, do.day_off, p_sType
FROM employees e
CROSS JOIN (SELECT p_dStart+LEVEL AS DAY_OFF
FROM DUAL
CONNECT BY LEVEL <= p_dEnd - p_dStart) do
WHERE e.employee_id = p_nEmployeeID
AND SUBSTR(e.workdays, TO_CHAR(do.day_off, 'D'), 1) = 'Y'
AND NOT EXISTS (SELECT 'X' FROM holidays h WHERE h.holiday_date = do.day_off);
END;
/

Counting of Tries for a log

I have 3 tables. INV_HDR, PAYM_LOG, PAY_CA. The schema are as follow
CREATE TABLE INV_HDR
(
INV_HDR_ID NUMBER(19, 0) DEFAULT "SEQ_INV_HDR_ID"."NEXTVAL" NOT NULL
, INV_NO VARCHAR2(30 BYTE) NOT NULL
, INV_TYPE_CD VARCHAR2(10 BYTE)
, INV_PARTY_UEN VARCHAR2(17 BYTE)
, INV_DT DATE
, GIRO_STS_CD VARCHAR2(11 BYTE) DEFAULT '0' NOT NULL
, INV_STS_CD VARCHAR2(11 BYTE) DEFAULT '1' NOT NULL
, PAYMENT_STS_CD VARCHAR2(11 BYTE) DEFAULT '1' NOT NULL
, RFD_STS_CD VARCHAR2(11 BYTE) DEFAULT '0' NOT NULL
, WAIVE_WRITE_DRAW_STS_CD VARCHAR2(11 BYTE) DEFAULT '0' NOT NULL
) ;
CREATE TABLE PAYM_LOG
(
PAYMENT_LOG_ID NUMBER(19, 0) DEFAULT "SEQ_PAYMENT_LOG_ID"."NEXTVAL" NOT NULL
, INV_HDR_ID NUMBER(19, 0) NOT NULL
, INV_PARTY_UEN VARCHAR2(17 BYTE)
, INV_NO VARCHAR2(30 BYTE) NOT NULL
, TRANS_DT DATE
, AMT NUMBER(15, 2) DEFAULT 0 NOT NULL
, VOUCHER_NO VARCHAR2(30 BYTE)
, REASON VARCHAR2(60 BYTE)
, CREATED_BY VARCHAR2(35 BYTE) NOT NULL
, CREATED_DT TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL
, LAST_UPDATED_BY VARCHAR2(35 BYTE) NOT NULL
, LAST_UPDATED_DT TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL
, PAYMENT_STS VARCHAR2(11 BYTE)
) ;
CREATE TABLE PAY_CA
(
PAY_CA_ID NUMBER(19, 0) DEFAULT "SEQ_PAY_CA_ID"."NEXTVAL" NOT NULL
, CA_ID VARCHAR2(2 BYTE)
, TRANS_DT DATE
, JOURNAL_HDR_ID NUMBER(19, 0)
, CREATED_BY VARCHAR2(35 BYTE) NOT NULL
, CREATED_DT TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL
, LAST_UPDATED_BY VARCHAR2(35 BYTE) NOT NULL
)
INV_HDR
1. Invoice is created for every Invoice generated. PK = INV_HDR_ID
2. GIRO_STS_CD will be updated from 2(Success) to 3(Fail) when payment has been made successfully
PAYM_LOG
1. Every attempted payment is recorded.
2. PAYMENT_STS = 12 (Fail), 13 (Pass)
PAY_CA
1. Only successful payment that has been made will have records created.
Thing is I would like to generate a report where there are 4 statuses.
1. GIRO Successful -- 1st time successful payment
2. GIRO Failed
3. GIRO Failed - 1st Retry
4. GIRO Failed - 2nd Retry
My report columns will be
S/N | INV_HDR.UNV_PARTY_UEN | CA_ID | Payment Status | Amount | Retry
With a summary to count the 4 different statuses.
Summary Count Amount
GIRO Successful x 100
GIRO Failed y 200
GIRO Failed - 1st Retry z 50
GIRO Failed - 2nd Retry w 10
Is it possible to do it in a single SQL? I have something like this so far..
SELECT PAY_CA.CA_ID, INV_HDR.INV_PARTY_UEN ,
INV_HDR.GIRO_STS_CD AS PAYMENT_STATUS,
PAY_CA.AMT,
SUM (
CASE
WHEN PAYM_LOG.PAYMENT_STS = '12'
THEN 1
ELSE 0
END ) AS RETRY
FROM INV_HDR,
PAYM_LOG,
PAY_CA
WHERE INV_HDR.INV_HDR_ID = CA_PAY.INV_HDR_ID
AND INV_HDR.INV_NO = PAYM_LOG.INV_NO
AND INV_HDR.GIRO_STS_CD IN ('2' ,'3')
GROUP BY PAY_CA.CA_ID ,
INV_HDR.INV_PARTY_UEN ,
INV_HDR.GIRO_STS_CD ,
PAY_CA.AMT
Thing is I need to retrieve both Failed and successful payment. Joining with PAY_CA will only allow me to get successful payments. How do i also get non-successful payments into counting of the retries?

how to insert '$' sign in some columns having data type number

I have one table:
CREATE TABLE Prod(
PRODUCT VARCHAR2(26 BYTE),
CUSTOMER VARCHAR2(16 BYTE),
QTR1 NUMBER(5,0),
QTR2 NUMBER(5,0),
QTR3 NUMBER(5,0),
QTR4 NUMBER(5,0)
);
I want to insert '$' sign into column 'qtr1','qtr2','qtr3','qtr4'.
Example: If 200, 400, 500 in column then after inserting it shows as $200,$400,$500.
Use TO_CHAR with either $ or L (with the NLS_CURRENCY set in either the session/database or as an explicit argument) in the format model:
SELECT TO_CHAR( qtr1, '$99990' ) AS qtr1,
TO_CHAR( qtr2, '$99990' ) AS qtr2,
TO_CHAR( qtr3, 'L99990' ) AS qtr3,
TO_CHAR( qtr4, 'L99990', 'NLS_CURRENCY=$' ) AS qtr4
FROM PROD

Inserting a Date data

so I have this table
RPG_RETCON (
UNIQUE_ID VARCHAR2(100 BYTE),
CONTAINER VARCHAR2(100 BYTE),
DATA_POINT_NAME VARCHAR2(100 BYTE),
SOURCE_VALUE VARCHAR2(100 BYTE),
CSS_VALUE VARCHAR2(100 BYTE),
STATUS VARCHAR2(100 BYTE)
)
And I I'm trying to insert this select statement into that table.
INSERT INTO RPG_RETCON
(SELECT A.POOL_CUSIP_ID AS UNIQUE_ID,
'1_13_1C' AS CONTAINER,
'SECU_ACTL_STLM_DT' AS COLUMN_NAME1,
TO_CHAR(A.SECU_ACTL_STLM_DT),
TO_CHAR(B.SECU_ACTL_STLM_DT),
CASE
WHEN A.SECU_ACTL_STLM_DT = B.SECU_ACTL_STLM_DT
THEN
'PASS'
ELSE
'FAIL'
END
AS STATUS
FROM POOL_1_13_1C_TRGT A
LEFT JOIN POOL_1_13_1C_CSS B ON A.POOL_CUSIP_ID = B.POOL_CUSIP_ID);
Now the problem is that SECU_ACTL_STLM_DT is a date field and when I try to do the inserts, I get an invalid number error. If I take away the TO_CHAR to just A.SECU_ACTL_STLM_DT,
B.SECU_ACTL_STLM_DT,
I get invalid month.
Note: I absolutely cannot change
SOURCE_VALUE VARCHAR2(100 BYTE)
CSS_VALUE VARCHAR2(100 BYTE)
-- Within the table structure...
They need to be VARCHAR2 Data types.
Are there any suggestions to where I can insert this select statement error free?
I think your code should work. However, I would list the columns explicitly and add date formats for the insert. Perhaps this will help:
INSERT INTO RPG_RETCON(UNIQUE_ID, CONTAINER, COLUMN_NAME1, SOURCE_VALUE, CSS_VALUE, STATUS)
SELECT A.POOL_CUSIP_ID AS UNIQUE_ID, '1_13_1C' AS CONTAINER,
'SECU_ACTL_STLM_DT' AS COLUMN_NAME1,
TO_CHAR(A.SECU_ACTL_STLM_DT, 'YYYY-MM-DD'),
TO_CHAR(B.SECU_ACTL_STLM_DT, 'YYYY-MM-DD'),
(CASE WHEN A.SECU_ACTL_STLM_DT = B.SECU_ACTL_STLM_DT
THEN 'PASS'
ELSE 'FAIL'
END) AS STATUS
FROM POOL_1_13_1C_TRGT A LEFT JOIN
POOL_1_13_1C_CSS B
ON A.POOL_CUSIP_ID = B.POOL_CUSIP_ID;
It is possible that one of the SECU_ACTL_STLM_DT columns is not a date and the comparison is failing.

ORACLE SQL ORA-22814 attribute or element value is larger than specified in type

I'm calling a function that returns a type of table.
My function receives a variable of the type varchar. Because i return a type that i created i had to make cast and multiset...
My problems is that, the select inside my multiset executes without problem outside of the function, but when i execute my function it gives me ORA-22814 attribute or element value is larger than specified in type error.
NOTE: i always run the script with the same variable value
Type OBJECT:
create or replace
TYPE Z_PEDIDO_SeB IS OBJECT(
Contrato varchar2(4000 BYTE),
DataObjectivo date,
DataObjectivoSpecified date,
Descricao varchar2(500 BYTE),
DireccaoRequerente varchar2(50 BYTE),
Empresa varchar2(50 BYTE),
Estado varchar2(50 BYTE),
GestorDDS varchar2(100 BYTE),
GestorEncomenda varchar2(30 BYTE),
Referencia varchar2(50 BYTE),
Link varchar2(500 BYTE),
ObsComite varchar2(4000 BYTE),
OutrosFornecedores varchar2(4000 BYTE),
OutrosSistAfectados varchar2(4000 BYTE),
PrincipalSistAfectado varchar2(4000 BYTE),
Prioridade varchar2(50 BYTE),
Rede varchar2(50 BYTE),
Requerente varchar2(100 BYTE),
TestesAceitacao varchar2(10 BYTE),
proj_id varchar2(50 BYTE));
Type TABLE:
create or replace
TYPE Z_TABLE_PEDIDO_SeB IS TABLE OF Z_PEDIDO_SeB;
FUNCTION:
create or replace
function Z_GetDadosCreateUpdate_SEB(
proj_id in varchar2
)
return Z_TABLE_PEDIDO_SeB as
t_dados Z_TABLE_PEDIDO_SeB;
begin
select
cast(
multiset(
--STARTS HERE WHAT I RUN WITHOUT PROBLEM OUTSIDE THE FUNCTION
select
(SELECT line_text
from long_text
where key2 = proj_id and key1 = 'contrato'
) Contrato,
NVL(SCHEDULE_FINISH,ACTUAL_FINISH) DataObjectivo,
NVL(SCHEDULE_FINISH,ACTUAL_FINISH) DataObjectivoSpecified,
pedidos.description as Descricao,
costum.direcaorequerente DireccaoRequerente,
costum.empresa Empresa,
estado.description Estado,
costum.gestordds GestorDDS,
(select recursos.description
from structure recursos,
resources,
workflow,
wf_team
where recursos.structure_code = resources.resource_code
and workflow.planning_code = projectos.structure_code
and wf_team.workflow_id = workflow.workflow_id
and wf_team.lifecycle_role_code = '868'
and wf_team.user_name = resources.LOGON_ID
and rownum = 1
) GestorEncomenda,
pedidos.structure_code ID,
(SELECT line_text
from long_text
where key2 = proj_id and key1 = 'urlcadernoreq'
) Link,
(SELECT line_text
from long_text
where key2 = proj_id and key1 = 'observacoescomite'
) ObsComite,
(SELECT line_text
from long_text
where key2 = proj_id and key1 = 'outrosfornecedores'
) OutrosFornecedores,
(SELECT line_text
from long_text
where key2 = proj_id and key1 = 'outrossistemas'
) OutrosSistAfectados,
(SELECT line_text
from long_text
where key2 = proj_id and key1 = 'principalsistema'
) PrincipalSistAfectado,
costum.prioridade Prioridade,
(SELECT rede.description
from structure rede, planning_entity proj
where proj.code88 = rede.structure_code
and proj.planning_code = proj_id
) Rede,
costum.requerente Requerente,
(SELECT rede.description
from structure rede, planning_entity proj
where proj.code89 = rede.structure_code
and proj.planning_code = proj_id
) TestesAceitacao,
projectos. structure_code proj_id
from structure projectos,
planning_entity,
structure pedidos,
structure estado,
custom_data costum
where projectos.structure_code = planning_entity.planning_code
and planning_entity.planning_code = planning_entity.ppl_code
and pedidos.structure_code = planning_entity.code31
and estado.structure_code = planning_entity.code20
and projectos.structure_code = proj_id
and costum.planning_code = proj_id
-- HERE ENDS WHAT I RUN OUTSIDE THE FUNCTION WITHOUT A PROBLEM
)
as Z_TABLE_PEDIDO_SeB)
into
t_dados
from
dual;
return t_dados;
end Z_GetDadosCreateUpdate_SEB;
What i execute:
SELECT * FROM table (PVDEV.Z_GetDadosCreateUpdate_SEB('184765'));
Error i got:
ORA-22814: valor do atributo ou elemento é superior ao especificado no tipo
ORA-06512: na "PVDEV.Z_GETDADOSCREATEUPDATE_SEB", linha 7
22814. 00000 - "attribute or element value is larger than specified in type"
*Cause: Value provided for the object type attribute or collection element
exceeded the size specified in the type declaration.
*Action: Choose another value and retry the operation.
NOTE: if i try 18476 instead of 184765 it runs with no problem. So? how did i put such restriction? Where?
NOTE: Now i'm showing the error, the real one, i'm really sorry for the mistake
I would really appreciate any answer as other's people work is waiting for my part :S. Anyway thanks in advance for any information.
If I were you I would declared the function as the pipelined one and returned values by pipe row statement. And surely get rid of CAST/multiset. For loop statement is your choice. I'm sure it will work using my piece of advice.