Counting of Tries for a log - sql

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?

Related

multiple BLOB file insertion in oracle using SQLdeveloper

I want to insert Multiple BLOB file at once ​​in "USER_PROFILE" table using loop by procedure/function.
Working on Oracle DB using SQLdeveloper
BLOB Files:
user.resetpassword.email.body.html
password.expiry.notification.email.html
password.expiry.notification.subject.txt
user.resetpassword.email.subject.txt
BLOB FILE DIRECTORY : U_PROFILE
BLOB column name : PROP_VALUE
Unique key : PROP_KEY (This column naming conventions will be same with BLOB file names)
CREATE TABLE "USER_PROFILE" (
"USER_PROFILE_PID" VARCHAR2(40 BYTE)
, "PROP_KEY" VARCHAR2(100 BYTE)
, "PROP_VALUE" BLOB
, "MODIFIED_DTS" DATE
, "BUILD_VERSION" VARCHAR2(100 BYTE)
, "DESCRIPTION" VARCHAR2(4000 BYTE)
)
​;
INSERT INTO user_profile (
user_profile_pid
, prop_key
, modified_dts
, build_version
, description
) VALUES (
'CTP-1000'
, 'password.expiry.notification.email'
, NULL
, '1.2'
, 'User Account Expiry notification'
);
INSERT INTO user_profile (
user_profile_pid
, prop_key
, modified_dts
, build_version
, description
) VALUES (
'CTP-1001'
, 'password.expiry.notification.subject'
, NULL
, '1.2'
, 'User Account Expiry notification subject'
);

oracle query failed with SQL error code 185

I am trying to insert rows into a table. I m using oracle database. The query looks as follows:
INSERT INTO bv_chglogentry
SELECT DISTINCT account_coid,
To_timestamp('2018-02-01-16.04.22.428161',
'YYYY-MM-DD-HH24.MI.SSXFF'),
1,
'HP15004',
'~HP15004',
' ',
' ',
Hextoraw('00257EAB')
|| Hextoraw('0001517F0804011B'),
Hextoraw('0000000F07650368'),
' ',
0,
' ',
Trunc(To_number(To_char(To_date('2015-05-02', 'YYYY-MM-DD'), 'J'
))),
134480155,
86399,
124060520
FROM inputaccounts;
The structure of the tables looks as follows;
BV_CHGLOGENTRY
---------------
COID_ NOT NULL CHAR(26 CHAR)
TIMESTAMP_ NOT NULL TIMESTAMP(9)
PRODUCT_ID NOT NULL NUMBER(10)
ADMIN_UNIT_ID NOT NULL CHAR(8 CHAR)
OPERATOR_ID NOT NULL CHAR(10 CHAR)
OVER_ADMIN_UNIT_ID NOT NULL CHAR(8 CHAR)
OVER_OPERATOR_ID NOT NULL CHAR(10 CHAR)
CHANGE_DATE NOT NULL RAW(12 BYTE)
DESCRIPTION_ NOT NULL RAW(220 BYTE)
BUSINESS_EVENT_ID NOT NULL CHAR(32 CHAR)
OWNER_TYPE NOT NULL NUMBER(10)
OWNER_ID NOT NULL CHAR(26 CHAR)
DATE_ NOT NULL NUMBER(10)
ZONE_ NOT NULL NUMBER(10)
TIME_ NOT NULL NUMBER(10)
CHG_LOG_TYPE NOT NULL NUMBER(10)
INPUTACCOUNTS
--------------
ACCOUNT_COID NOT NULL CHAR(26)
TXN_ID NOT NULL CHAR(26)
there is a column DATE_ which is of NUMBER type. I m having problem with inserting proper data in that column. The number of columns i'm inserting is 16 which is correct in BV_CHGLOGENTRY. Can you please let me know what might be the problem with the above query.
The value you are getting in the date column, after doing insert is not incorrect. It's just converted into Julian calendar format. Because you are converting the date into Julian calendar in your select query i.e:
Trunc(To_number(To_char(To_date('2015-05-02', 'YYYY-MM-DD'), 'J')))
The value that you are getting the date column is correct i.e (2457145).
To avoid this, you can use replace() function in your SELECT query where you are doing Trunc(To_number(To_char(To_date('2015-05-02', 'YYYY-MM-DD'), 'J')))to get readable date in number format.
Sample query:
select to_number(replace('2015-08-02', '-')) from dual;
Output: 20150802

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.

How to get data from oracle database in hourly basis

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';