I have the following code
SELECT SUM(nvl(book_value,
0))
INTO v_balance
FROM account_details
WHERE currency = 'UGX';
--Write the balance away
SELECT SUM(nvl(book_value,
0))
INTO v_balance
FROM account_details
WHERE currency = 'USD';
--Write the balance away
Now the problem is, there might not be data in the table for that specific currency, but there might be data for the 'USD' currency. So basically I want to select the sum into my variable and if there is no data I want my stored proc to continue and not throw the 01403 exception.
I don't want to put every select into statement in a BEGIN EXCEPTION END block either, so is there some way I can suppress the exception and just leave the v_balance variable in an undefined (NULL) state without the need for exception blocks?
select nvl(balance,0)
into v_balance
from
(
select sum(nvl(book_value,0)) as balance
from account_details
where currency = 'UGX'
);
SELECT L1.PKCODE L1CD, L1.NAME L1N, L1.LVL L1LVL,
L2.PKCODE L2CD, L2.NAME L2N, L2.LVL L2LVL,
L5.PKCODE L5CD, L5.NAME L5N,
INFOTBLM.OPBAL ( L5.PKCODE, :PSTDT, :PSTUC, :PENUC, :PSTVT, :PENVT ) OPBAL,
INFOTBLM.DEBIT ( L5.PKCODE, :PSTDT,:PENDT, :PSTUC, :PENUC, :PSTVT, :PENVT ) AMNTDR,
INFOTBLM.CREDIT ( L5.PKCODE, :PSTDT,:PENDT, :PSTUC, :PENUC, :PSTVT, :PENVT ) AMNTCR
FROM FSLVL L1, FSLVL L2, FSMAST L5
WHERE L2.FKCODE = L1.PKCODE
AND L5.FKCODE = L2.PKCODE
AND L5.PKCODE Between :PSTCD AND NVL(:PENCD,:PSTCD)
GROUP BY L1.PKCODE , L1.NAME , L1.LVL ,
L2.PKCODE , L2.NAME , L2.LVL ,
L5.PKCODE , L5.NAME
ORDER BY L1.PKCODE, L2.PKCODE, L5.PKCODE
Related
NOT IN condition, it is not working. I have these records in the table
proj_comp_id
proj_no
1050-WE-0005
8661
TOGG0000001
1581
5207-00GAC01AA502
8662
so it should add an error for the 2nd value..but not working pls help. below code is adding errors for 1 and 3rd lines also.
SELECT p.proj_comp_no AS primarykey,
p.proj_comp_id AS proj_comp_id,
pr.proj_id AS proj_id,
p.proj_no,
CASE
WHEN p.proj_comp_id NOT IN ('5207-%',
'1051-%',
'1050-%',
'1048-%',
'1049-%',
'2006-%',
'5203-%',
'1055-%',
'1056-%',
'1054-%') and p.proj_no NOT like (1581)
THEN
'1TAG_ID does not start with project prefix, pls add project prefix(5207-, 1049-,1048)'
ELSE
'NO ERROR'
END AS tag_ID_ERROR,
p.descr AS descr
FROM proj_comp p
LEFT JOIN std_mat_cls l
ON l.std_mat_cls_no = p.std_mat_cls_no
LEFT JOIN std_mat b
ON p.proj_comp_id = b.std_mat_id
LEFT JOIN proj pr
ON p.proj_no = pr.proj_no
WHERE (SELECT m.mat_stat_ext_id
FROM mat_stat_ext m
WHERE p.mat_stat_ext_no = m.mat_stat_ext_no) IN ('N', 'P')
AND b.std_mat_id IS NULL
***
That must be because IN isn't appropriate in this context. You should switch to something else, e.g. separate LIKE conditions:
No:
WHEN p.proj_comp_id NOT IN ( '5207-%', '1051-%', '1050-%', etc.
Yes:
WHEN p.proj_comp_id NOT LIKE '5207-%'
AND p.proj_comp_id NOT LIKE '1051-%'
AND p.proj_comp_id NOT LIKE '1050-%'
etc.
The other way to test this could be solved using the InStr() function...
WHEN
InStr('5207-, 1051-. 1050-, 1048-, 1049-, 2006-, 5203-, 1055-, 1056-, 1054-', SubStr(p.proj_comp_id, 1, 5)) = 0
This program compiles correctly, we are on V7R3 - but when running it receives an SQLCOD of -101 and an SQLSTATE code is 54011 which states: Too many columns were specified for a table, view, or table function. This is a very small JSON that is being created so I do not think that is the issue.
The RPGLE code:
dcl-s OutFile sqltype(dbclob_file);
xfil_tofile = '/ServiceID-REFCODJ.json';
Clear OutFile;
OutFile_Name = %TrimR(XFil_ToFile);
OutFile_NL = %Len(%TrimR(OutFile_Name));
OutFile_FO = IFSFileCreate;
OutFile_FO = IFSFileOverWrite;
exec sql
With elm (erpRef) as (select json_object
('ServiceID' VALUE trim(s.ServiceID),
'ERPReferenceID' VALUE trim(i.RefCod) )
FROM PADIMH I
INNER JOIN PADGUIDS G ON G.REFCOD = I.REFCOD
INNER JOIN PADSERV S ON S.GUID = G.GUID
WHERE G.XMLTYPE = 'Service')
, arr (arrDta) as (values json_array (
select erpRef from elm format json))
, erpReferences (refs) as ( select json_object ('erpReferences' :
arrDta Format json) from arr)
, headerData (hdrData) as (select json_object(
'InstanceName' : trim(Cntry) )
from padxmlhdr
where cntry = 'US')
VALUES (
select json_object('header' : hdrData format json,
'erpReferenceData' value refs format json)
from headerData, erpReferences )
INTO :OutFile;
Any help with this would be very much appreciated, this is our first attempt at creating JSON for sending and have not experienced this issue before.
Thanks,
John
I am sorry for the delay in getting back to this issue. It has been corrected, the issue was with the "values" statement.
This is the correct code needed to make it work correctly:
Select json_object('header' : hdrData format json,
'erpReferenceData' value refs format json)
INTO :OutFile
From headerData, erpReferences )
I have a stored procedure that is getting stuck on the merge into all of a sudden but has been working fine for years. Any advice ? The SP is in Oracle SQL developer and it is being executed in a Visual Studios SSIS package
Query below:
MERGE INTO CORE_APPT C
USING (
SELECT DISTINCT
TST_TKR_ID,
TST_TKR_ID_SRC,
APPT_ID_SRC,
APPT_CRETN_DTE,
APNTMT_DT,
ETS_APNTMT_ID,
ETS_TST_CNTR,
TST_STS_TYP_DESC,
APPT_STS_TYP,
TST_DLVR_MTHD_TYP_CDE,
DLVR_GRP_TYP,
TST_LNCH_TM,
TRNSCTN_ID,
TST_PKG_ID,
RND_TRIP_TST_FLG,
UPDT_DTE_TM,
PROD_ID,
ACTN_DTE_TM,
TST_DURN,
BRKRG_APPT_UPDT_DTM,
TST_APPT_TYP_CDE,
CNCL_MKUP_RSN_TYP,
DLNQNT_HLD_FLG,
TST_TKR_TST_UPDT_DTE_TM,
SCR_HLD_LVL_TYP_CDE,
SCR_CNCL_FLG,
ETS_CANDTE_SCR_HLD_FLG,
TST_CNTR_HLD_FLG,
TST_ADM_HLD_FLG,
ETS_SCR_CNCL_FLG,
TST_ADM_HLD_RSN_CDE,
TST_CNTR_HLD_RSN_CDE,
ETS_CANDTE_SCR_HLD_RSN_CDE,
ETS_SCR_CNCL_RSN_CDE,
DLNQNT_HLD_RSN_CDE,
TST_TKR_SCR_STS_TYP_CDE,
TST_TKR_SCR_STS_RSN_TYP_CDE,
PYMT_NO,
DATA_SRCE AS DATA_SRCE,
CURRENT_FLAG,
PRGRM_TYP,
sysdate as RCD_CRTD_DTE,
user as RCD_CRTD_USER,
REG_SYS_ID AS REG_SYS_ID,
TST_NAM AS TST_NAM,
CNTRY_SETL_RSP AS CNTRY_SETL_RSP
FROM STG_CMMN_APPT A
WHERE A.CURRENT_FLAG = 'Y'
) S
ON (C.APPT_ID_SRC = S.APPT_ID_SRC
AND C.REG_SYS_ID = S.REG_SYS_ID
AND C.DATA_SRCE = S.DATA_SRCE) -- added in EREG4.0 changes
WHEN MATCHED THEN UPDATE
SET C.CURRENT_FLAG = 'N'
WHERE C.CURRENT_FLAG = 'Y'
WHEN NOT MATCHED THEN INSERT
(
C.TST_TKR_ID,
C.TST_TKR_ID_SRC,
C.APPT_ID_SRC,
C.APPT_CRETN_DTE,
C.APNTMT_DT,
C.ETS_APNTMT_ID,
C.TST_CNTR_ID_SRC,
C.TST_STS_TYP_DESC,
C.APPT_STS_TYP,
C.TST_DLVR_MTHD_TYP_CDE,
C.DLVR_GRP_TYP,
C.TST_LNCH_TM,
C.TRNSCTN_ID,
C.TST_PKG_ID,
C.RND_TRIP_TST_FLG,
C.UPDT_DTE_TM,
C.PROD_ID,
C.ACTN_DTE_TM,
C.TST_DURN,
C.BRKRG_APPT_UPDT_DTM,
C.TST_APPT_TYP_CDE,
C.CNCL_MKUP_RSN_TYP,
C.DLNQNT_HLD_FLG,
C.TST_TKR_TST_UPDT_DTE_TM,
C.SCR_HLD_LVL_TYP_CDE,
C.SCR_CNCL_FLG,
C.ETS_CANDTE_SCR_HLD_FLG,
C.TST_CNTR_HLD_FLG,
C.TST_ADM_HLD_FLG,
C.ETS_SCR_CNCL_FLG,
C.TST_ADM_HLD_RSN_CDE,
C.TST_CNTR_HL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D_USER,
S.REG_SYS_ID,
S.TST_NAM,
S.CNTRY_SETL_RSP
)
;
TIA!
I have data set like below:
PIC_NUMBER|C_DATE|OR_QUANTITY
1|2017-03-01|10
1|2017-03-02|11
1|2017-03-03|12
1|2017-03-04|13
1|2017-03-05|14
1|2017-03-06|15
1|2017-03-07|16
2|2017-03-02|20
2|2017-03-04|13
2|2017-03-05|14
3|2017-03-02|5
3|2017-03-03|6
3|2017-03-05|7
3|2017-03-06|8
3|2017-03-07|9
4|2017-03-01|10
4|2017-03-02|11
4|2017-03-03|12
4|2017-03-04|13
4|2017-03-05|14
4|2017-03-06|15
4|2017-03-07|16
1|2017-03-08|20
1|2017-03-09|21
1|2017-03-10|22
1|2017-03-11|23
1|2017-03-12|24
1|2017-03-13|25
1|2017-03-14|26
2|2017-03-08|30
2|2017-03-09|31
2|2017-03-10|32
2|2017-03-11|33
2|2017-03-12|34
2|2017-03-13|35
2|2017-03-14|36
3|2017-03-08|30
3|2017-03-09|31
3|2017-03-12|34
3|2017-03-14|36
4|2017-03-08|20
4|2017-03-09|21
4|2017-03-10|22
4|2017-03-11|23
4|2017-03-12|24
4|2017-03-13|25
4|2017-03-14|26
And I want to sum OR_QUANTITY in a way that is exclude lesser date OR_QANTITY ,and sum will be for same PIC_NUMBER.
Example result set is:
PIC_NUMBER|C_DATE|SUM_OR_QUANTITY
1|2017-03-01|252
1|2017-03-02|242
1|2017-03-03|231
1|2017-03-04|219
1|2017-03-05|206
1|2017-03-06|192
1|2017-03-07|177
2|2017-03-02|278
2|2017-03-04|258
2|2017-03-05|245
3|2017-03-02|166
3|2017-03-03|161
3|2017-03-05|155
3|2017-03-06|148
3|2017-03-07|140
4|2017-03-01|252
4|2017-03-02|242
4|2017-03-03|231
4|2017-03-04|219
4|2017-03-05|206
4|2017-03-06|192
4|2017-03-07|177
1|2017-03-08|161
1|2017-03-09|141
1|2017-03-10|120
1|2017-03-11|98
1|2017-03-12|75
1|2017-03-13|51
1|2017-03-14|26
2|2017-03-08|231
2|2017-03-09|201
2|2017-03-10|170
2|2017-03-11|138
2|2017-03-12|105
2|2017-03-13|71
2|2017-03-14|36
3|2017-03-08|131
3|2017-03-09|101
3|2017-03-12|70
3|2017-03-14|36
4|2017-03-08|161
4|2017-03-09|141
4|2017-03-10|120
4|2017-03-11|98
4|2017-03-12|75
4|2017-03-13|51
4|2017-03-14|26
Can we write recursive functions in hive for this aggregation ?
This will give the desired result
select PIC_NUMBER, val1 , sum(OR_QUANTITY) from
( select a.PIC_NUMBER,a.C_DATE ,OR_QUANTITY,
case when (a.C_DATE >= temp.C_DATE ) then temp.C_DATE ELSE null END as val1
from table_name a , table_name temp
where temp.PIC_NUMBER = a.PIC_NUMBER ) temp1
where val1 is not null
group by PIC_NUMBER ,val1
I am working on Oracle Apex and not that familiar with it. The application I am working on has already been developed by some other developer who has left the company. The issue is - There is a main/super user with which the actual db connection is made (using DADS.conf file entry). After that, user is presented with a Login screen where he enters db subuser credentials. We have few application level contexts defined, so after subuser login we set the context like username for that subuser. In the application there is a report that is supposed to show data for logged in subuser. Now, when first user logs in, the reports shows correct data. When second user logs in from a different machine report starts showing data for the second user in both the application instances. Sometimes second user's report shows data for first user. This behavior is random. Apart from the report other application works fine. I seems that Apex in this case is using same physical database connection and hence the contexts are getting overwritten. I might be wrong here. I am very confused and unable to find any solution. Any help is appreciated.
Here is the report query (much complicated) -
With
Date_range as (
select * from X_LOHAC_DateRANGE_WK
)
--
,Haud_a
AS ( select wor_works_order_no haud_pk_id, WOR_CHAR_ATTRIB110 HAUD_NEW_VALUE, WOR_DATE_ATTRIB131 HAUD_TIMESTAMP
from work_orders
where 1=1
AND WOR_CHAR_ATTRIB110 in ('REV', 'REVUPD','REVCOMM','APPCOMM','APP','REJCOMM','REJ','INTREJ','APPUN')
AND WOR_DATE_ATTRIB131 BETWEEN (select min(st_range) from Date_range) AND (select max(end_range) from Date_range)
)
, haud as (
select h.*, range_value from haud_a h, Date_range
where HAUD_TIMESTAMP BETWEEN st_range AND end_range
)
--
--
--
, claims as
(
select a.woc_works_order_no, a.woc_interim_no, woc_claim_value, claim_previous from
(select woc_works_order_no, woc_interim_no, woc_claim_value
, lag(woc_claim_value) over (partition by woc_works_order_no order by woc_interim_no Asc) as claim_previous
from work_order_claims
order by woc_works_order_no, woc_interim_no asc) a
, (select woc_works_order_no, max(woc_interim_no) woc_interim_no from work_order_claims group by woc_works_order_no) b
where a.woc_works_order_no = b.woc_works_order_no
and a. woc_interim_no = b.woc_interim_no
)
--
, main as (
SELECT DISTINCT
'<p title="Click for forms" id="'||works_order_number||'" onmouseover="showWOLDetails(this);">'|| WORKs_ORDER_NUMBER||'</p>' WORKS_ORDER_NUMBER
,decode( DECODE (mai_sdo_util.wo_has_shape (hig.get_sysopt ('SDOWOLNTH'), wor.works_order_number),
'TRUE', 'Y','N'),
'N',
'<img width=24 height=24 src="/im4_framework/images/grey_globe.png" title="No Location">'
,'') map
,decode(im_framework.has_doc(works_order_number,'WORK_ORDERS'),0,
'<img width=24 height=24 src="/im4_framework/images/mfclosed.gif" title="No Documents">'
,'') DOCS
,(select ial_meaning from nm_inv_attri_lookup where ial_domain = 'INVOICE_STATUS' and ial_value = wor_char_attrib110) INVOICE_STATUS
,WOR_CHAR_ATTRIB111 as "INVOICE_STATUS_COMMENT"
,works_order_description
, claim_previous Previous_Claim_Amount
, woc_claim_value New_Claim_Amount
, WOR_CHAR_ATTRIB111 Claim_Comments
,bud.work_category_description Budget_Description
, 'BOQ' BOQ
,WOR_CHAR_ATTRIB115 "Correct area of work "
,WOR_CHAR_ATTRIB116 "Quality of Work OK"
,WOR_CHAR_ATTRIB70 "Correct BOQ_Uplifts"
,WOR_CHAR_ATTRIB113 "Before_After_Photos_Present" --"Before After Photos Present"
,WOR_CHAR_ATTRIB114 "Certification Comments"
--,(select hus_name from hig_audits, hig_users where haud_pk_id = haud.haud_pk_id and haud_timestamp = haud.haud_timestamp and haud_new_value = haud.haud_new_value and haud_attribute_name = haud.haud_attribute_name and rownum =1) Reviewed_By
, (select HUS_NAME from hig_users where hus_user_id = WOR_NUM_ATTRIB04 ) Reviewed_By
, works_order_number wor_number
, 'Edit' rec_edit
--
FROM imf_mai_work_orders_all_attrib wor,
imf_mai_work_order_lines wol,
haud
,claims
,imf_mai_budgets bud
,pod_nm_element_security,
pod_budget_security
WHERE 1=1
and wol.budget_id = bud.budget_id
AND works_order_number = haud_pk_id
AND works_order_number = claims.woc_works_order_no(+)
AND works_order_number = work_order_number
AND pod_nm_element_security.element_id = wol.network_element_id
AND pod_budget_security.BUDGET_CODE = wol.work_category
AND WOR_CHAR_ATTRIB110 = haud_new_value
AND range_value = :P40_DAYS
AND WOR_CHAR_ATTRIB110 = :P40_PRIORITY
)
--
Select * from main;
Setting Context just uses DBMS_SESSION package as follows -
DBMS_SESSION.SET_CONTEXT('NM3SEC', p_attribute, p_value);
In this case - p_attribute is USERNAME
You can't use DBMS_SESSION with apex since it's a stateless framework pooling multiple connections between multiple sessions.
Apex uses url based session-id's.
You should rather
1) Create an application item in Shared Components.
2) Set the value of the item as a post login procedure using apex_util.set_session_state
Use the value of that item in your SQL code.