Inline Select Query
i want to check the pending payments against the PurchaseOrder.
i wrote this query but got no rows returned.
SQL> SELECT (B.POHAMT-C.FTHAMT) NET
2 FROM
3 (
4 SELECT NVL(POH_gross_lc_amount,0)-nvl(poh_comm_lc_amount,0) POHAMT
5 FROM PURCHASE_ORDER_HEADER
6 WHERE POH_NO = 5
7 AND POH_CODE = 'PUR') B,
8 (
9 SELECT SUM(NVL(FTH_LC_AMOUNT,0)) FTHAMT
10 FROM FIN_TXN_HEADER
11 WHERE FTH_REF_CODE = 'PUR'
12 AND FTH_REF_NO = 5
13 GROUP BY FTH_REF_CODE,FTH_REF_NO) C;
no rows selected
let me show you the break-up of the above query.
there is 1 record existing in the purchase order, for which the payment is yet to be made.
SQL> SELECT
2 NVL(POH_gross_lc_amount,0)-nvl(poh_comm_lc_amount,0) POHAMT
3 FROM PURCHASE_ORDER_HEADER
4 WHERE POH_NO = 5
5 AND POH_CODE = 'PUR';
POHAMT
265
for the second part there is no payment exist for the poh_no 5, so no records returned, fair enough.
SQL> SELECT FTH_REF_CODE,FTH_REF_NO,SUM(NVL(FTH_LC_AMOUNT,0)) FTHAMT
2 FROM FIN_TXN_HEADER
3 WHERE FTH_REF_CODE = 'PUR'
4 AND FTH_REF_NO = 5
5 GROUP BY FTH_REF_CODE,FTH_REF_NO;
no rows selected
what the above query should do is, incase if there is no payment exist, then it should returned 265 for the Net column please help where am i wrong.
user613563 wrote:
i want to check the pending payments against the PurchaseOrder.
i wrote this query but got no rows returned.
SQL> SELECT (B.POHAMT-C.FTHAMT) NET
2 FROM
3 (
4 SELECT NVL(POH_gross_lc_amount,0)-nvl(poh_comm_lc_amount,0) POHAMT
5 FROM PURCHASE_ORDER_HEADER
6 WHERE POH_NO = 5
7 AND POH_CODE = 'PUR') B,
8 (
9 SELECT SUM(NVL(FTH_LC_AMOUNT,0)) FTHAMT
10 FROM FIN_TXN_HEADER
11 WHERE FTH_REF_CODE = 'PUR'
12 AND FTH_REF_NO = 5
13 GROUP BY FTH_REF_CODE,FTH_REF_NO) C;
no rows selectedplease help where am i wrong.Not sure if I got everything right. Main problem is to create a row first. Then you can work with NVL.
One way could be to outer join the two queries.
untested
SELECT nvl(c.FTH_REF_CODE,'HEADER'), c.FTH_REF_NO, NVL(C.FTHAMT,B.POHAMT) NET
FROM
(SELECT 1 dummy, NVL(POH_gross_lc_amount,0)-nvl(poh_comm_lc_amount,0) POHAMT
FROM PURCHASE_ORDER_HEADER
WHERE POH_NO = 5
AND POH_CODE = 'PUR') B
LEFT OUTER JOIN
(SELECT 1 dummy, SUM(NVL(FTH_LC_AMOUNT,0)) FTHAMT, FTH_REF_CODE,FTH_REF_NO
FROM FIN_TXN_HEADER
WHERE FTH_REF_CODE = 'PUR'
AND FTH_REF_NO = 5
GROUP BY FTH_REF_CODE,FTH_REF_NO) C
ON b.dummy = c.dummy
Similar Messages
-
Inline select statement and SQL optimizatino
Hi everyone,
I am trying to optimze a script but can't get the fulle explainplan in PL/SQL.
The problem is that the select statements in de column defenition is not explained. below are the 2 scripts I want to compare:
SQL1:
Select
z.title
, (select name from members where id =z.id) as name
, z.id
from job z
SQL2
Select
z.title
, d.name
, z.id
from job z
left outer join members d on z.id=d.id
My question is: what is the effect on performance and cost of the 'inline select' statement of name in SQL1
hope to hear form you soon
Harm
Edited by: HALI on 25-nov-2010 1:14In respons to your question below is the output:
I altered the used names, tablenames etc. according to our privacy pollacy.
A far as I can see the inline statement is not traced.
The query with ansi-join:
xplain plan for
select
id as Interne
, mv.ie_id as Act
, 'H_W' as Rl
from testing i
left outer join resutino m on i.hand=m.id
left outer join vert mv on m.voor || case when m.voeg is not null then ' '|| m.voeg end ||' '||m.naam = mv.source and mv.field = 'employee'
explain plan succeeded.
select * from table(dbms_xplan.display)
PLAN_TABLE_OUTPUT
| Id | Operation | Name | Rows | Bytes | Cost |
| 0 | SELECT STATEMENT | | 684 | 47880 | 19 |
| 1 | HASH JOIN OUTER | | 684 | 47880 | 19 |
| 2 | HASH JOIN OUTER | | 684 | 22572 | 15 |
| 3 | TABLE ACCESS FULL | testing | 684 | 5472 | 7 |
| 4 | TABLE ACCESS FULL | resutino | 308 | 7700 | 7 |
| 5 | TABLE ACCESS FULL | VERT | 166 | 6142 | 3 |
Note: cpu costing is off, 'PLAN_TABLE' is old version
13 rows selected
The inline select query:
xplain plan for
SELECT
case when (select ie_id from vert xov where xov.field = 'field' and source in
(select m.voor || case when m.voeg is not null then ' '|| m.voegend ||' '||m.naam from resultino m where i.hand= m.id)) is not null then
'X'||(select ie_id from vert xov where xov.field = 'field' and source in
(select m.voor || case when m.voeg is not null then ' '|| m.voegend ||' '||m.naam from resultino m where i.hand= m.id))
else 'X' || (select ie_id from vert xov where xov.source = 'none' and xov.veld = 'employee')
end as Act,
'ADM' as Rol,
i.id as Interne
FROM testing i
explain plan succeeded.
select * from table(dbms_xplan.display)
PLAN_TABLE_OUTPUT
| Id | Operation | Name | Rows | Bytes | Cost |
| 0 | SELECT STATEMENT | | 684 | 5472 | 7 |
| 1 | TABLE ACCESS FULL | testing | 684 | 5472 | 7 |
Note: cpu costing is off, 'PLAN_TABLE' is old version
9 rows selected -
Needed help to improve the performance of a select query?
Hi,
I have been preparing a report which involves data to be fetched from 4 to 5 different tables and calculation has to performed on some columns also,
i planned to write a single cursor to populate 1 temp table.i have used INLINE VIEW,EXISTS more frequently in the select query..please go through the query and suggest me a better way to restructure the query.
cursor c_acc_pickup_incr(p_branch_code varchar2, p_applDate date, p_st_dt date, p_ed_dt date) is
select sca.branch_code "BRANCH",
sca.cust_ac_no "ACCOUNT",
to_char(p_applDate, 'YYYYMM') "YEARMONTH",
sca.ccy "CURRENCY",
sca.account_class "PRODUCT",
sca.cust_no "CUSTOMER",
sca.ac_desc "DESCRIPTION",
null "LOW_BAL",
null "HIGH_BAL",
null "AVG_CR_BAL",
null "AVG_DR_BAL",
null "CR_DAYS",
null "DR_DAYS",
--null "CR_TURNOVER",
--null "DR_TURNOVER",
null "DR_OD_DAYS",
(select sum(gf.limit_amount * (scal.linkage_percentage / 100)) +
(case when (p_applDate >= sca.tod_limit_start_date and
p_applDate <= nvl(sca.tod_limit_end_date, p_applDate)) then
sca.tod_limit else 0 end) dd
from getm_facility gf, sttm_cust_account_linkages scal
where gf.line_code || gf.line_serial = scal.linked_ref_no
and cust_ac_no = sca.cust_ac_no) "OD_LIMIT",
--sc.credit_rating "CR_GRADE",
null "AVG_NET_BAL",
null "UNAUTH_OD_AMT",
sca.acy_blocked_amount "AMT_BLOCKED",
(select sum(amt)
from ictb_entries_history ieh
where ieh.acc = sca.cust_ac_no
and ieh.brn = sca.branch_code
and ieh.drcr = 'D'
and ieh.liqn = 'Y'
and ieh.entry_passed = 'Y'
and ieh.ent_dt between p_st_dt and p_ed_dt
and exists (
select * from ictm_pr_int ipi, ictm_rule_frm irf
where ipi.product_code = ieh.prod
and ipi.rule = irf.rule_id
and irf.book_flag = 'B')) "DR_INTEREST",
(select sum(amt)
from ictb_entries_history ieh
where ieh.acc = sca.cust_ac_no
and ieh.brn = sca.branch_code
and ieh.drcr = 'C'
and ieh.liqn = 'Y'
and ieh.entry_passed = 'Y'
and ieh.ent_dt between p_st_dt and p_ed_dt
and exists (
select * from ictm_pr_int ipi, ictm_rule_frm irf
where ipi.product_code = ieh.prod
and ipi.rule = irf.rule_id
and irf.book_flag = 'B')) "CR_INTEREST",
(select sum(amt) from ictb_entries_history ieh
where ieh.brn = sca.branch_code
and ieh.acc = sca.cust_ac_no
and ieh.ent_dt between p_st_dt and p_ed_dt
and exists (
select product_code
from ictm_product_definition ipd
where ipd.product_code = ieh.prod
and ipd.product_type = 'C')) "FEE_INCOME",
sca.record_stat "ACC_STATUS",
case when (trunc(sca.ac_open_date,'MM') = trunc(p_applDate,'MM')
and not exists (select 1
from ictm_tdpayin_details itd
where itd.multimode_payopt = 'Y'
and itd.brn = sca.branch_code
and itd.acc = sca.cust_ac_no
and itd.multimode_offset_brn is not null
and itd.multimode_tdoffset_acc is not null))
then 1 else 0 end "NEW_ACC_FOR_THE_MONTH",
case when (trunc(sca.ac_open_date,'MM') = trunc(p_applDate,'MM')
and trunc(sc.cif_creation_date,'MM') = trunc(p_applDate,'MM')
and not exists (select 1
from ictm_tdpayin_details itd
where itd.multimode_payopt = 'Y'
and itd.brn = sca.branch_code
and itd.acc = sca.cust_ac_no
and itd.multimode_offset_brn is not null
and itd.multimode_tdoffset_acc is not null))
then 1 else 0 end "NEW_ACC_FOR_NEW_CUST",
(select 1 from dual
where exists (select 1 from ictm_td_closure_renew itcr
where itcr.brn = sca.branch_code
and itcr.acc = sca.cust_ac_no
and itcr.renewal_date = sysdate)
or exists (select 1 from ictm_tdpayin_details itd
where itd.multimode_payopt = 'Y'
and itd.brn = sca.branch_code
and itd.acc = sca.cust_ac_no
and itd.multimode_offset_brn is not null
and itd.multimode_tdoffset_acc is not null)) "RENEWED_OR_ROLLOVER",
(select maturity_date from ictm_acc ia
where ia.brn = sca.branch_code
and ia.acc = sca.cust_ac_no) "MATURITY_DATE",
sca.ac_stat_no_dr "DR_DISALLOWED",
sca.ac_stat_no_cr "CR_DISALLOWED",
sca.ac_stat_block "BLOCKED_ACC", Not Reqd
sca.ac_stat_dormant "DORMANT_ACC",
sca.ac_stat_stop_pay "STOP_PAY_ACC", --New
sca.ac_stat_frozen "FROZEN_ACC",
sca.ac_open_date "ACC_OPENING_DT",
sca.address1 "ADD_LINE_1",
sca.address2 "ADD_LINE_2",
sca.address3 "ADD_LINE_3",
sca.address4 "ADD_LINE_4",
sca.joint_ac_indicator "JOINT_ACC",
sca.acy_avl_bal "CR_BAL",
0 "DR_BAL",
0 "CR_BAL_LCY", t
0 "DR_BAL_LCY",
null "YTD_CR_MOVEMENT",
null "YTD_DR_MOVEMENT",
null "YTD_CR_MOVEMENT_LCY",
null "YTD_DR_MOVEMENT_LCY",
null "MTD_CR_MOVEMENT",
null "MTD_DR_MOVEMENT",
null "MTD_CR_MOVEMENT_LCY",
null "MTD_DR_MOVEMENT_LCY",
'N' "BRANCH_TRFR", --New
sca.provision_amount "PROVISION_AMT",
sca.account_type "ACCOUNT_TYPE",
nvl(sca.tod_limit, 0) "TOD_LIMIT",
nvl(sca.sublimit, 0) "SUB_LIMIT",
nvl(sca.tod_limit_start_date, global.min_date) "TOD_START_DATE",
nvl(sca.tod_limit_end_date, global.max_date) "TOD_END_DATE"
from sttm_cust_account sca, sttm_customer sc
where sca.branch_code = p_branch_code
and sca.cust_no = sc.customer_no
and ( exists (select 1 from actb_daily_log adl
where adl.ac_no = sca.cust_ac_no
and adl.ac_branch = sca.branch_code
and adl.trn_dt = p_applDate
and adl.auth_stat = 'A')
or exists (select 1 from catm_amount_blocks cab
where cab.account = sca.cust_ac_no
and cab.branch = sca.branch_code
and cab.effective_date = p_applDate
and cab.auth_stat = 'A')
or exists (select 1 from ictm_td_closure_renew itcr
where itcr.acc = sca.cust_ac_no
and itcr.brn = sca.branch_code
and itcr.renewal_date = p_applDate)
or exists (select 1 from sttm_ac_stat_change sasc
where sasc.cust_ac_no = sca.cust_ac_no
and sasc.branch_code = sca.branch_code
and sasc.status_change_date = p_applDate
and sasc.auth_stat = 'A')
or exists (select 1 from cstb_acc_brn_trfr_log cabtl
where cabtl.branch_code = sca.branch_code
and cabtl.cust_ac_no = sca.cust_ac_no
and cabtl.process_status = 'S'
and cabtl.process_date = p_applDate)
or exists (select 1 from sttbs_provision_history sph
where sph.branch_code = sca.branch_code
and sph.cust_ac_no = sca.cust_ac_no
and sph.esn_date = p_applDate)
or exists (select 1 from sttms_cust_account_dormancy scad
where scad.branch_code = sca.branch_code
and scad.cust_ac_no = sca.cust_ac_no
and scad.dormancy_start_dt = p_applDate)
or sca.maker_dt_stamp = p_applDate
or sca.status_since = p_applDate
l_tb_acc_det ty_tb_acc_det_int;
l_brnrec cvpks_utils.rec_brnlcy;
l_acbr_lcy sttms_branch.branch_lcy%type;
l_lcy_amount actbs_daily_log.lcy_amount%type;
l_xrate number;
l_dt_rec sttm_dates%rowtype;
l_acc_rec sttm_cust_account%rowtype;
l_acc_stat_row ty_r_acc_stat;
Edited by: user13710379 on Jan 7, 2012 12:18 AMI see it more like shown below (possibly with no inline selects
Try to get rid of the remaining inline selects ( left as an exercise ;) )
and rewrite traditional joins as ansi joins as problems might arise using mixed syntax as I have to leave so I don't have time to complete the query
select sca.branch_code "BRANCH",
sca.cust_ac_no "ACCOUNT",
to_char(p_applDate, 'YYYYMM') "YEARMONTH",
sca.ccy "CURRENCY",
sca.account_class "PRODUCT",
sca.cust_no "CUSTOMER",
sca.ac_desc "DESCRIPTION",
null "LOW_BAL",
null "HIGH_BAL",
null "AVG_CR_BAL",
null "AVG_DR_BAL",
null "CR_DAYS",
null "DR_DAYS",
-- null "CR_TURNOVER",
-- null "DR_TURNOVER",
null "DR_OD_DAYS",
w.dd "OD_LIMIT",
-- sc.credit_rating "CR_GRADE",
null "AVG_NET_BAL",
null "UNAUTH_OD_AMT",
sca.acy_blocked_amount "AMT_BLOCKED",
x.dr_int "DR_INTEREST",
x.cr_int "CR_INTEREST",
y.fee_amt "FEE_INCOME",
sca.record_stat "ACC_STATUS",
case when trunc(sca.ac_open_date,'MM') = trunc(p_applDate,'MM')
and not exists(select 1
from ictm_tdpayin_details itd
where itd.multimode_payopt = 'Y'
and itd.brn = sca.branch_code
and itd.acc = sca.cust_ac_no
and itd.multimode_offset_brn is not null
and itd.multimode_tdoffset_acc is not null
then 1
else 0
end "NEW_ACC_FOR_THE_MONTH",
case when (trunc(sca.ac_open_date,'MM') = trunc(p_applDate,'MM')
and trunc(sc.cif_creation_date,'MM') = trunc(p_applDate,'MM')
and not exists(select 1
from ictm_tdpayin_details itd
where itd.multimode_payopt = 'Y'
and itd.brn = sca.branch_code
and itd.acc = sca.cust_ac_no
and itd.multimode_offset_brn is not null
and itd.multimode_tdoffset_acc is not null
then 1
else 0
end "NEW_ACC_FOR_NEW_CUST",
(select 1 from dual
where exists(select 1
from ictm_td_closure_renew itcr
where itcr.brn = sca.branch_code
and itcr.acc = sca.cust_ac_no
and itcr.renewal_date = sysdate
or exists(select 1
from ictm_tdpayin_details itd
where itd.multimode_payopt = 'Y'
and itd.brn = sca.branch_code
and itd.acc = sca.cust_ac_no
and itd.multimode_offset_brn is not null
and itd.multimode_tdoffset_acc is not null
) "RENEWED_OR_ROLLOVER",
m.maturity_date "MATURITY_DATE",
sca.ac_stat_no_dr "DR_DISALLOWED",
sca.ac_stat_no_cr "CR_DISALLOWED",
-- sca.ac_stat_block "BLOCKED_ACC", --Not Reqd
sca.ac_stat_dormant "DORMANT_ACC",
sca.ac_stat_stop_pay "STOP_PAY_ACC", --New
sca.ac_stat_frozen "FROZEN_ACC",
sca.ac_open_date "ACC_OPENING_DT",
sca.address1 "ADD_LINE_1",
sca.address2 "ADD_LINE_2",
sca.address3 "ADD_LINE_3",
sca.address4 "ADD_LINE_4",
sca.joint_ac_indicator "JOINT_ACC",
sca.acy_avl_bal "CR_BAL",
0 "DR_BAL",
0 "CR_BAL_LCY", t
0 "DR_BAL_LCY",
null "YTD_CR_MOVEMENT",
null "YTD_DR_MOVEMENT",
null "YTD_CR_MOVEMENT_LCY",
null "YTD_DR_MOVEMENT_LCY",
null "MTD_CR_MOVEMENT",
null "MTD_DR_MOVEMENT",
null "MTD_CR_MOVEMENT_LCY",
null "MTD_DR_MOVEMENT_LCY",
'N' "BRANCH_TRFR", --New
sca.provision_amount "PROVISION_AMT",
sca.account_type "ACCOUNT_TYPE",
nvl(sca.tod_limit, 0) "TOD_LIMIT",
nvl(sca.sublimit, 0) "SUB_LIMIT",
nvl(sca.tod_limit_start_date, global.min_date) "TOD_START_DATE",
nvl(sca.tod_limit_end_date, global.max_date) "TOD_END_DATE"
from sttm_cust_account sca,
sttm_customer sc,
(select sca.cust_ac_no
sum(gf.limit_amount * (scal.linkage_percentage / 100)) +
case when p_applDate >= sca.tod_limit_start_date
and p_applDate <= nvl(sca.tod_limit_end_date, p_applDate)
then sca.tod_limit else 0
end
) dd
from sttm_cust_account sca
getm_facility gf,
sttm_cust_account_linkages scal
where gf.line_code || gf.line_serial = scal.linked_ref_no
and cust_ac_no = sca.cust_ac_no
group by sca.cust_ac_no
) w,
(select acc,
brn,
sum(decode(drcr,'D',amt)) dr_int,
sum(decode(drcr,'C',amt)) cr_int
from ictb_entries_history ieh
where ent_dt between p_st_dt and p_ed_dt
and drcr in ('C','D')
and liqn = 'Y'
and entry_passed = 'Y'
and exists(select null
from ictm_pr_int ipi,
ictm_rule_frm irf
where ipi.rule = irf.rule_id
and ipi.product_code = ieh.prod
and irf.book_flag = 'B'
group by acc,brn
) x,
(select acc,
brn,
sum(amt) fee_amt
from ictb_entries_history ieh
where ieh.ent_dt between p_st_dt and p_ed_dt
and exists(select product_code
from ictm_product_definition ipd
where ipd.product_code = ieh.prod
and ipd.product_type = 'C'
group by acc,brn
) y,
ictm_acc m,
(select sca.cust_ac_no,
sca.branch_code
coalesce(nvl2(coalesce(t1.ac_no,t1.ac_branch),'exists',null),
nvl2(coalesce(t2.account,t2.account),'exists',null),
nvl2(coalesce(t3.acc,t3.brn),'exists',null),
nvl2(coalesce(t4.cust_ac_no,t4.branch_code),'exists',null),
nvl2(coalesce(t5.cust_ac_no,t5.branch_code),'exists',null),
nvl2(coalesce(t6.cust_ac_no,t6.branch_code),'exists',null),
nvl2(coalesce(t7.cust_ac_no,t7.branch_code),'exists',null),
decode(sca.maker_dt_stamp,p_applDate,'exists'),
decode(sca.status_since,p_applDate,'exists')
) existence
from sttm_cust_account sca
left outer join
(select ac_no,ac_branch
from actb_daily_log
where trn_dt = p_applDate
and auth_stat = 'A'
) t1
on (sca.cust_ac_no = t1.ac_no
and sca.branch_code = t1.ac_branch
left outer join
(select account,account
from catm_amount_blocks
where effective_date = p_applDate
and auth_stat = 'A'
) t2
on (sca.cust_ac_no = t2.account
and sca.branch_code = t2.branch
left outer join
(select acc,brn
from ictm_td_closure_renew itcr
where renewal_date = p_applDate
) t3
on (sca.cust_ac_no = t3.acc
and sca.branch_code = t3.brn
left outer join
(select cust_ac_no,branch_code
from sttm_ac_stat_change
where status_change_date = p_applDate
and auth_stat = 'A'
) t4
on (sca.cust_ac_no = t4.cust_ac_no
and sca.branch_code = t4.branch_code
left outer join
(select cust_ac_no,branch_code
from cstb_acc_brn_trfr_log
where process_date = p_applDate
and process_status = 'S'
) t5
on (sca.cust_ac_no = t5.cust_ac_no
and sca.branch_code = t5.branch_code
left outer join
(select cust_ac_no,branch_code
from sttbs_provision_history
where esn_date = p_applDate
) t6
on (sca.cust_ac_no = t6.cust_ac_no
and sca.branch_code = t6.branch_code
left outer join
(select cust_ac_no,branch_code
from sttms_cust_account_dormancy
where dormancy_start_dt = p_applDate
) t7
on (sca.cust_ac_no = t7.cust_ac_no
and sca.branch_code = t7.branch_code
) z
where sca.branch_code = p_branch_code
and sca.cust_no = sc.customer_no
and sca.cust_ac_no = w.cust_ac_no
and sca.cust_ac_no = x.acc
and sca.branch_code = x.brn
and sca.cust_ac_no = y.acc
and sca.branch_code = y.brn
and sca.cust_ac_no = m.acc
and sca.branch_code = m.brn
and sca.cust_ac_no = z.sca.cust_ac_no
and sca.branch_code = z.branch_code
and z.existence is not nullRegards
Etbin -
I have an interactive application that periodically feeds a legacy batch transaction application with data to keep it in sync with my app. For demonstrating my problem I am using a similar but much simpler example here, using two tables only: CUST and ORD. The CUST table contains customer specific data and the ORD table contains orders placed by each customer. The primary key of CUST table is the CU_CODE that is also a foreign key (OR_CU_CODE) in the ORD table. The PK of ORD table is the OR_CODE column. See their description below:
Table: CUST
Name Null? Type
CU_CODE NOT NULL VARCHAR2(6)
CU_TYPE NOT NULL VARCHAR2(2)
CU_NAME NOT NULL VARCHAR2(80)
CU_ADDR VARCHAR2(120)
Table: ORD
Name Null? Type
OR_CODE NOT NULL VARCHAR2(8)
OR_CU_CODE NOT NULL VARCHAR2(6)
OR_DATE NOT NULL DATE
OR_TERMS VARCHAR2(40)
The legacy app needs to get both a customer and an order transaction for any order whose date is more recent than a given date. To create those transactions, I run a SELECT query like this:
SELECT 'UPD,CUST='||CU_CODE||',TYPE='||CU_TYPE||',NAME='||CU_NAME
||',ADDR='||CU_ADDR
FROM (SELECT DISTINCT CU_CODE, CU_TYPE, CU_NAME, CU_ADDR
FROM CUST, ORD WHERE OR_CU_CODE = CU_CODE
AND OR_DATE > SOME_DATE
ORDER BY 1)
UNION ALL
SELECT 'UPD,ORD='||OR_CODE||',CUST='||OR_CU_CODE||',DATE='
||TO_CHAR(OR_DATE, 'YYYYMMDD')||',TERMS='||OR_TERMS
FROM (SELECT OR_CODE, OR_CU_CODE, OR_DATE, OR_TERMS
FROM ORD WHERE OR_DATE > SOME_DATE
ORDER BY 2, 1);
This produces a batch update transaction file similar to the following:
UPD,CUST=CU1111,TYPE=COM,NAME=ACME,ADDR=ACME Address
UPD,CUST=CU2222,TYPE=RES,NAME=John Q,ADDR=John Q Address
UPD,CUST=CU3333,TYPE=GOV,NAME=IRS,ADDR=IRS Address
UPD,ORD=OR001234,CUST=CU1111,DATE=20061204,TERMS=Term1
UPD,ORD=OR001235,CUST=CU1111,DATE=20061206,TERMS=Term1
UPD,ORD=OR001236,CUST=CU1111,DATE=20061207,TERMS=Term1
UPD,ORD=OR002234,CUST=CU2222,DATE=20061205,TERMS=Term2
UPD,ORD=OR002235,CUST=CU2222,DATE=20061207,TERMS=Term2
UPD,ORD=OR003237,CUST=CU3333,DATE=20070102,TERMS=Term3
Both transaction types are sorted by the customer code first but customer update transactions also need a DISTINCT clause because we only want to send one custumer update per customer even if there were more orders placed by that customer.
This thing has been working fine in most cases except when a large transaction file is generated because the legacy app can only handle input files of only certain max. size. In such cases the operators need to manually split up the file into several smaller ones but still making sure that each file segment has customer and order transactions belonging to the same customers only. This can be really tedious with hundreads of customers and their orders. The file splitting would be much easier if I could rewrite the original SELECT statement to produce the transaction data in the following order:
UPD,CUST=CU1111,TYPE=COM,NAME=ACME,ADDR=ACME Address
UPD,ORD=OR001234,CUST=CU1111,DATE=20061204,TERMS=Term1
UPD,ORD=OR001235,CUST=CU1111,DATE=20061206,TERMS=Term1
UPD,ORD=OR001236,CUST=CU1111,DATE=20061207,TERMS=Term1
UPD,CUST=CU2222,TYPE=RES,NAME=John Q,ADDR=John Q Address
UPD,ORD=OR002234,CUST=CU2222,DATE=20061205,TERMS=Term2
UPD,ORD=OR002235,CUST=CU2222,DATE=20061207,TERMS=Term2
UPD,CUST=CU3333,TYPE=GOV,NAME=IRS,ADDR=IRS Address
UPD,ORD=OR003237,CUST=CU3333,DATE=20070102,TERMS=Term3
Here a given customer update transaction would be followed by all orders for that customer and so on.
I want the new query also in pure SQL, not in PL SQL. As dealing with DB and Oracle is only a small part of my job, I just can't figure out a solution to this problem. I suspect many of the regulars here can help me.
Thanks,
Joe
PS: Sorry, the table descriptions looked much better in the edit screen but I hope you can still figure it out from the display window.Try something like the following (not tested)
SELECT transaction FROM (
SELECT CU_CODE,1,'UPD,CUST='||CU_CODE||',TYPE='||CU_TYPE||',NAME='||CU_NAME
||',ADDR='||CU_ADDR transaction
FROM (SELECT DISTINCT CU_CODE, CU_TYPE, CU_NAME, CU_ADDR
FROM CUST, ORD WHERE OR_CU_CODE = CU_CODE
AND OR_DATE > SOME_DATE)
UNION ALL
SELECT OR_CU_CODE,2,'UPD,ORD='||OR_CODE||',CUST='||OR_CU_CODE||',DATE='
||TO_CHAR(OR_DATE, 'YYYYMMDD')||',TERMS='||OR_TERMS transaction
FROM (SELECT OR_CODE, OR_CU_CODE, OR_DATE, OR_TERMS
FROM ORD WHERE OR_DATE > SOME_DATE)
ORDER BY 1,2;Basically, I added some columns to your query for sorting purposes then used that query as an inline view to only get the data you are interested in. Hope it runs. If not, let me know and I'll create the table and actually test it. -
Hi Experts,
I am having a select query in which I am using a variable in the where condition but it is giving error. Please suggest how to use variable in the select query.
The query I am using is a s below.
select * from zexc_rec into table it_ZEXC_REC
where
LIFNR in S_LIFNR and
DOCNO in S_DOCNO and
DOCTYP in doc_typ and
DATE1 in S_DATE1 and
MATNR in S_MATNR.
Here doc_typ is a variable.
Thanks.Rahul,
use RANGES type variable instead of variable . It acts as a select-options variable. Thn use this variable in SELECT query with IN.
Eg :
RANGES r_t510 FOR t510-lgart.
r_t510-low = '1600'.
APPEND r_t510.
r_t510-low = '3190'.
APPEND r_t510.
Note : can be use SIGN, OPTIONS properties too in RANGES type.
More deatils go through on HELP of RANGES
Rgds,
Ranjith -
Oracle 11g :SELECT query blocked..??
Hi Experts,
could you please explain why the below SQL query is blocked?
SELECT 1 FROM DUAL is blocking the SQL statement on GTTAPPUSR@gttccuatcriba04 ( SID=469 ) blocked SQL -> DELETE FROM GTTDB.PURCHASE_ENTRY_ID=:1
SELECT 1 FROM DUAL is blocking the SQL statement on GTTAPPUSR@gttccuatcriba04 ( SID=367 ) blocked SQL -> DELETE FROM GTTDB.PURCHASE_ENTRY_ID=:1
I am scratching my head without any solution when I had a look at the db today. Thanks in advance for your help.
Regards,
Boris
Edited by: user12075620 on Dec 4, 2012 8:58 AMThe SELECT statement is not blocking the UPDATE. As I said in the previous reply, the string that this query produces does not match the logic.
This query is (at least on the surface) correctly identifying that session 1 is blocking session 2. Session 1 holds some lock that session 2 is waiting on. So far, so good. Since session 2 is waiting on the lock, we can easily enough see what session 2 is running (the UPDATE statement). But since session 1 is not blocked, it is potentially off running a ton of other SQL statements (or no SQL statement at all). The query is looking to see what session 1 is running currently. It has no way of determining what session 1 ran at some point in the past to acquire the lock in the first place.
Going back to my KING example,
At noon, session 1 runs
UPDATE emp
SET sal = sal * 2
WHERE ename = 'KING'Session 1 now has a lock on the KING row in the EMP table. But session 1 neither commits nor rolls back, it is still in a transaction. Session 1 might not have any more activity for a long time-- the user might go off to lunch, for example (obviously, applications should not be designed to allow users to maintain open transactions indefinitely, but not all applications are designed correctly). Or it might start running other queries. Let's say that session 1 now runs a query that is going to go for an hour
SELECT *
FROM giant_view_with_lots_of_computationsNow, at 12:45, session 2 comes in and runs
UPDATE emp
SET bonus = 100
WHERE ename = 'KING'Session 2 is blocked. Session 2 is running the UPDATE statement. Session 1 still holds the lock but it is running some completely unrelated SQL statement.
If we run the query you posted, the query will correctly report that session 1 is running the query against the GIANT_VIEW_WITH_LOTS_OF_COMPUTATIONS but incorrectly imply that this SELECT query is the source of the lock. It is not. It simply happens to be the query that the session that does hold the lock happens to be executing at the current moment (why the application seems to be running a lot of queries that select a constant from dual is a separate question).
Justin -
Oracle SQL Select query takes long time than expected.
Hi,
I am facing a problem in SQL select query statement. There is a long time taken in select query from the Database.
The query is as follows.
select /*+rule */ f1.id,f1.fdn,p1.attr_name,p1.attr_value from fdnmappingtable f1,parametertable p1 where p1.id = f1.id and ((f1.object_type ='ne_sub_type.780' )) and ( (f1.id in(select id from fdnmappingtable where fdn like '0=#1#/14=#S0058-3#/17=#S0058-3#/18=#1#/780=#5#%')))order by f1.id asc
This query is taking more than 4 seconds to get the results in a system where the DB is running for more than 1 month.
The same query is taking very few milliseconds (50-100ms) in a system where the DB is freshly installed and the data in the tables are same in both the systems.
Kindly advice what is going wrong??
Regards,
PurushothamSQL> @/alcatel/omc1/data/query.sql
2 ;
9 rows selected.
Execution Plan
Plan hash value: 3745571015
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | SORT ORDER BY | |
| 2 | NESTED LOOPS | |
| 3 | NESTED LOOPS | |
| 4 | TABLE ACCESS FULL | PARAMETERTABLE |
|* 5 | TABLE ACCESS BY INDEX ROWID| FDNMAPPINGTABLE |
|* 6 | INDEX UNIQUE SCAN | PRIMARY_KY_FDNMAPPINGTABLE |
|* 7 | TABLE ACCESS BY INDEX ROWID | FDNMAPPINGTABLE |
|* 8 | INDEX UNIQUE SCAN | PRIMARY_KY_FDNMAPPINGTABLE |
Predicate Information (identified by operation id):
5 - filter("F1"."OBJECT_TYPE"='ne_sub_type.780')
6 - access("P1"."ID"="F1"."ID")
7 - filter("FDN" LIKE '0=#1#/14=#S0058-3#/17=#S0058-3#/18=#1#/780=#5#
8 - access("F1"."ID"="ID")
Note
- rule based optimizer used (consider using cbo)
Statistics
0 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
0 bytes sent via SQL*Net to client
0 bytes received via SQL*Net from client
0 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
9 rows processed
SQL> -
Select query differences between oracle 9 and oracle 8.
Hi,
I have a problem using the select query between oracle 7 and oracle 9i I don't have the same result :
ex:
With oracle7
SQL> select 'champ1','champ2' from DUAL;
'CHAMP 'CHAMP
champ1 champ2
With Oracle 9
SQL> select 'champ1','champ2' from DUAL;
'CHAMP1' 'CHAMP2'
champ1 champ2
So Can someone tell me how to solve this problem ? Is there a parameter in oracle 9 to set?
Thanx.Whenever you are posting anything over internet forums like this - you should be very careful about not just posting the details which requires to solve the problem - also should be sensible about your question.
This is not at all desired when you are posting such question. It may be because - you may not well aware of the fact.
My suggestion is -> First Go through the basics Of SQL in general.
Then go for any specific product like Oracle/ SQL Server/ Sybase etc.
And, finally learn the advanced commands of that DB.
You asked it - may be you thought the difference in output in terms of lines. But, that is not your actual output. That is the graphical display part only.
Anyway,
You can get the quite familier output by first type the following command ->
set lin 310Regards.
Satyaki De. -
Select Query resulting in Scientific Notation
Hello all,
I am running a Select query through a batch file that extracts data from an Oracle database. Several of the fields that I am extracting from contain numbers that are up to 38 digits long. When I extract the data, it converts the numbers into scientific notation and it is important for me to have the entire field. Is there something I can change to my query that will pull the data in its entire form? This is what I'm running now:
select * FROM ML.APPT where APPTDATE >= to_date('01/1/2010','mm/dd/yyyy'
I apologize in advance if this has been answered already.
Thanks!>
When the extractor finishes, it returns the data into a flat file.
don't quite understand the TO_CHAR function. Does this function mean I need to say something like this: select "TO_CHAR('column name', 99999999999999999999999999999999999999" FROM ML.APPT where APPTDATE >= to_date('01/1/2010','mm/dd/yyyy')
>
Yes- if the tool you use to extract the data (your 'extractor') is converting the numeric data to a string then it is responsible for creating the string in the proper format. If the number is an integer that can have as many digits as you have '9's in your sample format string then that is what you need to do.
Here is how sql*plus (Oracle's tool) will display the data using default settings
SQL> select 12345678901234567890123456789012345678 no_format,
2 to_char(12345678901234567890123456789012345678, '99999999999999999999999999
999999999999') with_format
3 from dual;
NO_FORMAT WITH_FORMAT
1.2346E+37 12345678901234567890123456789012345678
SQL>
----- TOAD will display something similiar but the default uses more decimal digits in the scientific notation data
NO_FORMAT,WITH_FORMAT
1.23456789012346E37, 12345678901234567890123456789012345678You can either format the numeric data in the query using TO_CHAR or the 'extractor' can do it when it converts the data to a string. -
Select-options in SELECT query - syntax error
Hi all,
I get the error below when I try to use the select options in a SELECT query . Please help me.
"The IN operator with "SO_AWART" is followed neither by an internal
table nor by a value list."
The code i have used(Logical database PNP is used):
TABLES: pernr,
catsdb.
INCLUDE ztime_cwtr_top. " global Data
INCLUDE ztime_cwtr_f01.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME.
SELECT-OPTIONS SO_AWART FOR CATSDB-AWART.
PARAMETERS P_THRES TYPE I.
SELECTION-SCREEN END OF BLOCK B1.
Get data from CATSDB table. Workdates within the date interval are considered.
SELECT pernr workdate awart catsquantity beguz enduz status
FROM catsdb
INTO TABLE it_catsdb
WHERE pernr = pernr-pernr AND
workdate GE pn-begda AND
workdate LE pn-endda AND
status IN ('20', '30') AND
awart IN so_awart .
awart IN ('1100', '1137', '1138', '1139', '1140',
'1147', '1148', '1149', '1157', '2003' ).
when I give the values directly i do not get any syntax error, but when I use select options in the where condition I get the syntax error.
I have tried different options like using only the select-options in the where condition.
Thanks in advance.....
MadhuSolved.
Code with syntax error:
include z...top .
include z...fo1.
select-options: xxxxxxx
Code with no syntax error:
select-options: xxxxxxx
include z...top .
include z...fo1.
Thanks for all your help,
Madhu -
Query in Select query operation
Please excuse me for posting this again.
I'm stuck up with this for 3 days. Lenghty post pls do read and help me.
How is the select query going to be changed in the SAP so that the original database behind it understands it. I want to know the operation of IN operation in where clause where in we can use ranges.
I'm asking this because I'm facing a problem using Native SQL.
Query is like this:
EXEC SQl.
OPEN C1 for
SELECT field1 from table@domain where field2 IN :ABAP_VAR.
ENDEXEC.
EXEC SQL.
FETCH C1 NEXT INTO :VAR1 , :VAR2
ENDEXEC.
Oracle usually recognises this statement IN ( '2222' , '3658' , '6895' )
But here this field that is build dynamically like this is not recognising them as seperate fileds but instead it is taking them as a single string and not selecting anything.
But if a single value is passed it is fetching the data into the cursor. And the code is working fine.
Any help in this is highly appreciable."if a in r_range" is is effectively: does the value held in 'a' comply with the values held in range 'r_range'. Ranges can hold NE, patterns etc so this can get quite complex.
You can see that it is very different from the SQL type IN.
If your range hold several values you could unstrip it into a string varaible so that it ended up having a value something like: ( '2222' , '3658' , '6895' ). Not sure if you need the '(' but it would be something like this:
eg,
assuming your range is simple and only holds 'I' and 'EQ' values:
data v_string type string.
loop at r_range.
concatenate v_string '@''' r_range-low '''@' into v_string.
endloop.
REPLACE
ALL OCCURRENCES OF '@'
IN v_string
WITH ' '.
Then you can use:
SELECT field1 from table@domain where field2 IN :v_string. -
SELECt query in UNiX environment
Iam using hp unix with oracle 11g
when i try to spool 26 lack records it is giving me error
" There is not enough memory available now.
O/S Message: Broken pipe"
is there is any other way SQL commands, we can limit the SELECT query to spool specific number of times other than using
FOR LOOP.. with LIMIT cluaselogdata=`${SQLPLUS} -S username/password<< EOF
SET ECHO OFF
SET LINESIZE 2000
SET NEWPAGE 0
SET SPACE 0
SET PAGESIZE 0
SET FEEDBACK OFF
SET HEADING OFF
SET TRIMSPOOL ON
SET TAB OFF
set autoprint on
set serveroutput on
set spool on
spool ${SPOOL_FILE}
set termout off
WHENEVER SQLERROR EXIT SQL.SQLCODE
whenever oserror exit 1;
$GetQuery ==> there come's the SELECT statement which spool's 25 lakh records.
spool off
EXIT
END`
While spooling records it throws the error
There is not enough memory available now.
O/S Message: Broken pipe
i want u use spooling instead of using util_file to write each and every rows in a file
Edited by: user9080289 on Apr 29, 2010 5:04 AM -
Select query in materialized view with two dblinks
Hi All,
We have oracle 10g On windows.
We are trying to create materialized view. Scenario is we have base table on other database and we are creating mview on different database.
Basa database have two schema's and i am selecting records from that two schema's using two private db links.
But when i am tryin gto create mview its not getting created. After 15 hrs. its still showing creation command and not finished.
Query is :-
Is it good practice to have two db links in select query of materialized view.Billy Verreynne wrote:
Chanchal Wankhade wrote:
Is it good practice to have two db links in select query of materialized view.Same db link being used twice, or two different db links?
If the former, you ideally want the local Oracle db to send the join to the remote database, and for the remote database to drive the join between those 2 tables. There is a hint (<i>driving_site</i>) that can be used - or the join query can be defined on the remote database as a view, and the local materialised view can then use that remote view.
If you have 2 different db links and joining across these - usually a bad idea to perform distributed database joins. There are lots of limitations as to how the tables can be joined. Worse case, full table scans of both remote tables, pulling all the rows from the 2 remote database tables to the local database, and joining these on the local database.
I have seen some severe performance issues in the past as a result of distributed joins. I'll rather use 2 materialised views for pulling both distributed tables's data locally, and then do the join on local data (using indexes, partition pruning, etc)Hi Billy,
My scenario is i have two database database A and database B. Database A is having two schema's SCOTT AND HR. SCOTT schema have select privileges on HR schema.
DB LINK is between Database B to Database A. name is db.link.B.A.oracle.com.
What if i priovide while creating materialized view, the schema name before the table name in database B for this particuler table so it will pick up the table from that schema using same DB LINK(db.link.B.A.oracle.com.) that i am using to fetch records from SCOTT schema.
Above schnario is like two base schema's and one db link using two schema. -
Select query in MB51 failing only for posting date(BUDAT)02/02/2010
Hello Experts,
I am having below select query.
It is running perfectly fine for all the dates except 02/02/2010.On all other dates it is getting executed with in second.
For 02/02/2010 it keeps on running for more than 10 minutes and then fails with timeout error.
Please let me know if anyone know any solution for this.
select (g_t_fields)
into corresponding fields of table itab
from mkpf inner join mseg
on mkpfmandt = msegmandt
and mkpfmblnr = msegmblnr
and mkpfmjahr = msegmjahr
WHERE MKPF~BUDAT in BUDAT
and MSEG~BWART in BWART
and MSEG~CHARG in CHARG
and MKPF~FRBNR in FRBNR
and MSEG~KUNNR in KUNNR
and MSEG~LGORT in LGORT
and MSEG~LIFNR in LIFNR
and MSEG~MATNR in MATNR
and MSEG~SOBKZ in SOBKZ
and MKPF~USNAM in USNAM
and MKPF~VGART in VGART
and MSEG~WERKS in WERKS
and MKPF~XBLNR in XBLNR
%_HINTS
ORACLE '&SUBSTITUTE VALUES&'
Moderator message - Please see Please Read before Posting in the Performance and Tuning Forum before posting and please use code tags when posting code - post locked
Edited by: Rob Burbank on Feb 4, 2010 9:02 AMHi Sameer,
Please check on which date the production order has status as "completed".
It could be on 01.02.2015.
Thanks,
Vimal -
Hi!
Is it possible to modify a select query for a report in the following way:
I have created two fields where users input values and operators.
Operators selection is a static list consisting of: =, like , in
Value field is a text field.
The current query is:
SELECT
A,
B,
C
from TABLE
I would like to add:
WHERE
A : P1_OPERATOR : P1_VALUE
but it doesn't work; I get a notification that the operator is incorrect.
It has a defaulft value = and when I set let's say 5 in the value field the whole query should be:
SELECT
A,
B,
C
from TABLE
WHERE
A = 5
Regards!Hmm, I get the following error:
Updatable report parse error:
ORA-20001: Query must begin with SELECT or WITH
The only difference I see is that your type is SQL Query (PL/SQL function body returning SQL query) while I have SQL Query (updateable report) hardcoded.
I have APEX 4.2.2, I'm not quite sure how to change this...
This is my code:
begin
:P7_QUERY := 'select ROWID,BNK_ID,MSR_PRD_ID,SRC_STM_ID,ID,ID_RETKA,RSP,OZNAKA_RETKA,DATUM_STANJA,OZNAKA_IZVJESCA,OZNAKA_KOMITENTA,MBR_KOMITENTA,KOMITENT_NEREZ,ZUPANIJA,DRZAVA,SEKTOR_NEREZIDENTA,VRSTA_POVEZANOSTI,INSTRUMENT,ISIN,VALUTA,OTKAZNI_ROK,IZVORNO_DOSPIJECE,VRSTA_INDEKSACIJE,VALUTA_INDEKSACIJE,PORTFELJ,UTRZIVOST_KREDITA,ZNACAJKE_KAPITALA,RIZICNA_SKUPINA,UGRADJENI_DERIVAT,ODNOSNA_VARIJABLA,PREDZNAK,IZNOS,IZNOS_ACTUAL,VRSTA_IZNOSA,KOMITENT_PBR,UDJELI_POVEZ_C,AR_ID,AU_ID,ACT_AR_BAL_KN,ACT_AR_BAL,AR_BUSS_ID,MTI_CCY_TP_ID,REG_NO,REG_SFX,JMBG_ID_NO,IP_ID,NO_DYS_OO,TAX_ID_NO,INSTRUMENT_OLD,PREDZNAK_OLD,IZNOS_ACTUAL_OLD,ACT_AR_BAL_KN_OLD,ACT_AR_BAL_OLD,NAPOMENA,NOVI_POSAO_F,LISTA_SUMARNA,LISTA_REKAP,POSTOTAK1,POSTOTAK2,POSTOTAK3,DZS_IDY_CL_ID,HNB_IP_CL_ID,EXG_RT_CRD_RSK_F from DWP.IZV_SLOG_DET
where ' || :P7_X_FC1 || :P7_X_O1 || :P7_X_O1;
return :P7_QUERY;
end;
I have also set Use Generic Column Names (parse query at runtime only) nad P7_QUERY as page item to submit.
Regards,
Ivan
Maybe you are looking for
-
How to adjust zoom in Word 12.3.1?
My friend has a MacBook Pro which was updated to 10.8.2 this fall in the Apple store in Dublin. He is having trouble adjusting the zoom feature in Word (12.3.1) he says it is set to 87% and can't figure out how to make it 150% so he can read it easie
-
We would like to start using the subcontract functionality to send material out to sterilization. From what I understand we would use the routing for this purpose by selecting external processing in the routing and selecting the subcontract button.
-
I am trying to empty my trash bin, but I keep getting a message that says one of the documents in it is in use. It is not in use. What can I do to delete it? Thanks, Denise
-
Querying on Maintenance Plan's content tables
Hello, Currently, i'm trying to know if there is any task in any maintenance plan for a specific database name on SQL Server 2008 and so on. I want to do it for doing a performance check-up of my server and of my associated website (asp.net). I'm abl
-
Sorry for a stupid topic but just wondering how the speed broadband checker works out what speed you can have? I'm just asking this as every time I do it, it says 2.5mb but I'm on 3.70mb. So, how dose it work out what speed you can get? Also, don't w