Query returning different counts
Dear all,
10.2.0.4 on solaris ..
Why is the 2 queries are displaying different rows
SQL> select count(*) FROM MEDT_BL where rowid in(SELECT rowid FROM MEDT_BL WHERE BILLDATE IN ('21-JUN-2011','21-MAY-2011','21-APR-2011') UNION ALL
SELECT rowid FROM MEDT_BL WHERE NVL(BILLED,'N') = 'Y'); 2
COUNT(*)
40131732
SQL> select count(*) from (SELECT * FROM MEDT_BL WHERE BILLDATE IN
('21-JUN-2011','21-MAY-2011','21-APR-2011') UNION ALL SELECT * FROM MEDT_BL WHERE NVL(BILLED,'N') <> 'Y') 2
COUNT(*)
7426398Thanks
Kai
Your second query double counts any records that meet both conditions while you first query counts each row only once if it matches either condition. The sub-query in the IN clause is implicitly "distincted". Consider
SQL > select * from t;
ID DESCR
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
7 Seven
8 Eight
9 Nine
10 Ten
11 Eleven
12 Twelve
13 Thirteen
14 Fourteen
15 Fifteen
SQL > select * from t
2 where rowid in (select rowid from t
3 where mod(id, 2) = 0
4 union all
5 select rowid from t
6 where descr like '%teen');
ID DESCR
2 Two
4 Four
6 Six
8 Eight
10 Ten
12 Twelve
13 Thirteen
14 Fourteen
15 Fifteen
SQL > select *
2 from (select * from t
3 where mod(id, 2) = 0
4 union all
5 select * from t
6 where descr like '%teen');
ID DESCR
2 Two
4 Four
6 Six
8 Eight
10 Ten
12 Twelve
14 Fourteen
13 Thirteen
14 Fourteen
15 FifteenIn the first version the row for 14, Fourteen only appeared once because the rowids were "distincted" while in the second query, it appears twice. Once from the top half of the union because 14 is even, and once from the bottom half of the union because Fourteen ends with teen.
To make the second query equivalent to the first, you would need to use a union all, or combine both predicates into one query like:
select count(*)
from MEDT_BL
where billdate in ('21-JUN-2011','21-MAY-2011','21-APR-2011') or
nvl(billed,'N') = 'Y')As a side note, '21-JUN-2011','21-MAY-2011', and '21-APR-2011' are not dates, they are strings. Assuming that the billdate column is actually a date datatype, you need to use to_date to make the comparision correct:
select count(*)
from MEDT_BL
where billdate in (TO_DATE('21-JUN-2011', 'dd-mon-yyyy'),
TO_DATE('21-MAY-2011', 'dd-mon-yyyy'),
TO_DATE('21-APR-2011', 'dd-mon-yyyy')) or
nvl(billed,'N') = 'Y')John
Similar Messages
-
CONNECT BY QUERY returns different count of rown in 10g resp. 11g databases
Folks,
I just spot a bellow issue in our databases.
1.) 10g database:
Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.5.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> select * from global_name;
SQL> SELECT ROWNUM
2 FROM dual
3 CONNECT BY ROWNUM <= 2;
ROWNUM
1
2
3
SQL> 2. 11g database
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select * from global_name;
SQL> SELECT ROWNUM
2 FROM dual
3 CONNECT BY ROWNUM <= 2;
ROWNUM
1
2
SQL> As you can see in 10g above query returns 3 records, in 11g only 2.
Why it's like that? Can it be somehow set, to produce the same output?
Many thanks,
TomasI get correct rows on both database version !
Oracle Versions
10g 10.2.0.1.0
11g 11.1.0.6.0
SQL> select rownum from dual connect by rownum <= 2;
ROWNUM
1
2
SQL> select * from v$version ;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> select rownum from dual connect by rownum <= 2;
ROWNUM
1
2
SQL> select * from v$version ;
BANNER
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
PL/SQL Release 11.1.0.6.0 - Production
CORE 11.1.0.6.0 Production
TNS for 32-bit Windows: Version 11.1.0.6.0 - Production
NLSRTL Version 11.1.0.6.0 - Production -
Same query in 9i & 11g returning different counts
Recently our DB was upgraded from 9i to 11g. We observed some weird results like same query returning more records in 11g than in 9i. I later found out that was because of Null values. I used below commands to achieve same results.
1) ALTER session set optimizer_features_enable='9.2.0'; -
2) SELECT /*+ optimizer_features_enable=('9.2.0' )*/ *
FROM TABLE_NAME
WHERE
ORDER BY COL1 DESC , COL2 ASC,COL3 ASC;
Do we have any documentation/material/link which explains this behavior? Any documentation on data type difference between 9i & 11g? Similar issue i observed in connect by prior.I had to select level in order to get the same records but in 9i without selecting level also we were getting desired result.
ThanksHi,
MSINHA wrote:
Recently our DB was upgraded from 9i to 11g. We observed some weird results like same query returning more records in 11g than in 9i. I later found out that was because of Null values. I used below commands to achieve same results.
1) ALTER session set optimizer_features_enable='9.2.0'; -
2) SELECT /*+ optimizer_features_enable=('9.2.0' )*/ *
FROM TABLE_NAME
WHERE
ORDER BY COL1 DESC , COL2 ASC,COL3 ASC;If you're getting any results other than an error message, then I don't believe you're running the query above. You need include some condition in the WHERE clause, or leave out the WHERE clause altogether.
Do you have row-level security in either database?
As Blushadow said, post a complete test script that people can use to re-create the problem and test their ideas. Include CREATE TABLE and INSERT statements for some sample data, and the complete query that you're using. If you are using dbms_rls row-level security, include the function and the code to enable it.
There's no version 9h or 9j, so it's kind of silly to say you're using 9i. The same goes for 10g. Why not give the actual version numbers, such as 9.2.0.6.0 and 10.2.0.3.0?
See the forum FAQ {message:id=9360002} -
Same Query returning different result (Different execution plan)
Hi all,
To day i have discovered a strange thing: a query that return a different result when using a different execution plan.
The query :
SELECT *
FROM schema.table@database a
WHERE column1 IN ('3')
AND column2 = '101'
AND EXISTS
(SELECT null
FROM schema.table2 c
WHERE a.column3 = SUBSTR (c.column1, 2, 12));where schema.table@database is a remote table.
when executed with the hint /*+ ordered use_nl(a c) */ these query return no result and its execution plan is :
Rows Row Source Operation
0 NESTED LOOPS (cr=31 r=0 w=0 time=4894659 us)
4323 SORT UNIQUE (cr=31 r=0 w=0 time=50835 us)
4336 TABLE ACCESS FULL TABLE2 (cr=31 r=0 w=0 time=7607 us)
0 REMOTE (cr=0 r=0 w=0 time=130536 us)When i changed the execution plan with the hint /*+ use_hash(c a) */
Rows Row Source Operation
3702 HASH JOIN SEMI (cr=35 r=0 w=0 time=497839 us)
22556 REMOTE (cr=0 r=0 w=0 time=401176 us)
4336 TABLE ACCESS FULL TABLE2 (cr=35 r=0 w=0 time=7709 us)It seem that when the execution plan have changed the remote query return no result.
It'is a bug or i have missed somthing ?
PS: The two table are no subject to insert or update statement.
Oracle version : 9.2.0.2.0
System version : HP-UX v1
Thanks.H.Mahmoud wrote:
Oracle version : 9.2.0.2.0
System version : HP-UX v1Hard to say. You're using a very old and deprecated version of the database, and one that was known to contain bugs.
9.2.0.7 was really the lowest version of 9i that was considered to be 'stable', but even so, it's old and lacking in many ways.
Consider upgrading to the latest database version at your earliest opportunity. (or at least apply patches up to the latest 9i version before querying if there is bugs in your really low buggy version) -
Query returns different Explain Plan
Hi,
I have two databases that are identical to each other. But I have a query that runs on both and returns different explain plans in both. It runs slower in one than other. How can I troubleshoot this further to get faster times in the slower database?
THanks,
PrachiQuery:
=====
SELECT sum(total_contacts), sum(email_partner_news), sum(email_tech_news),
sum(email_enduser_news), sum(email), sum(email_gmo), sum(gmo_nophone),
sum(email_gmo_noaddress), sum(email_gmo_address), sum(
email_gmo_nocertaddr), sum(email_gmo_certaddress), sum(
email_gmo_nophone_noaddr), sum(phone_number), sum(gmo_with_phone),
sum(phone_number_email_gmo_noaddr), sum(phone_number_email_gmo_nocert),
sum(phone_number_address), sum(phone_number_address_noemail), sum(
phone_address_email_nogmo), sum(phone_number_address_email), sum(
phone_number_certaddr), sum(phone_number_certaddr_noemail), sum(
phone_certaddr_email_nogmo), sum(phone_number_address_gmo), sum(
phone_certaddr_email_gmo), sum(address), sum(
address_nophone_number_nogmo), sum(address_certified), sum(
certaddr_nophone_nogmo), sum(address_email_nophone_gmo), sum(
certaddr_email_nophone_gmo), sum(phone_gmo), sum(smail_gmo), sum(fax_gmo
), sum(email_wcast), sum(email_inews), sum(email_salrt), sum(
phone_gmo_phone), sum(smail_gmo_address), sum(fax_gmo_fax)
FROM contact_values_vw_tc_2 cv, ((SELECT gcd_contact_id
FROM contact_values)
INTERSECT
((SELECT gcd_contact_id
FROM gcddata.contact_country s, gcd.country c,
segmentation_query_values v
WHERE s.country_id = c.country_id
AND c.region_id = v.query_value
AND v.selection_type = 'I'
AND v.query_id = 2088
AND v.query_sequence = 1)
INTERSECT
(SELECT gcd_contact_id
FROM gcddata.contact_country s, gcd.country c,
segmentation_query_values v
WHERE s.country_id = c.country_id
AND c.sub_region_id = v.query_value
AND v.selection_type = 'I'
AND v.query_id = 2088
AND v.query_sequence = 2)) ) sl
WHERE cv.gcd_contact_id = sl.gcd_contact_id
AND cv.country_id IN (SELECT cm.country_id
FROM segmentation_user_country_map cm
WHERE cm.user_name = 'E30590')
========================================
Execution Plan - FAST
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=32931 Card=1 Bytes=73)
1 0 SORT (AGGREGATE)
2 1 HASH JOIN (Cost=32931 Card=386388 Bytes=28206324)
3 2 VIEW (Cost=29617 Card=1142043 Bytes=14846559)
4 3 INTERSECTION
5 4 SORT (UNIQUE)
6 5 INDEX (FAST FULL SCAN) OF 'CONTACT_VALUES1_PK' (UNIQUE) (Cost=5 Card=1907737 Bytes=11446422)
7 4 INTERSECTION
8 7 SORT (UNIQUE)
9 8 HASH JOIN (Cost=655 Card=1998575 Bytes=57958675)
10 9 HASH JOIN (Cost=6 Card=110 Bytes=2200)
11 10 TABLE ACCESS (BY INDEX ROWID) OF 'SEGMENTATION_QUERY_VALUES' (Cost=3 Card=7 Bytes=91)
12 11 INDEX (RANGE SCAN) OF 'SEG_QUERY_VALUES_ PK' (UNIQUE) (Cost=2 Card=15)
13 10 TABLE ACCESS (FULL) OF 'COUNTRY' (Cost=2 Card=120 Bytes=840)
14 9 TABLE ACCESS (FULL) OF 'CONTACT_COUNTRY1' (Cost=643 Card=2199855 Bytes=19798695)
15 7 SORT (UNIQUE)
16 15 HASH JOIN (Cost=655 Card=1142043 Bytes=33119247)
17 16 HASH JOIN (Cost=6 Card=63 Bytes=1260)
18 17 TABLE ACCESS (BY INDEX ROWID) OF 'SEGMENTATION_QUERY_VALUES' (Cost=3 Card=7 Bytes=91)
19 18 INDEX (RANGE SCAN) OF 'SEG_QUERY_VALUES_PK' (UNIQUE) (Cost=2 Card=15)
20 17 TABLE ACCESS (FULL) OF 'COUNTRY' (Cost=2 Card=120 Bytes=840)
21 16 TABLE ACCESS (FULL) OF 'CONTACT_COUNTRY1' (Cost=643 Card=2199855 Bytes=19798695)
22 2 HASH JOIN (Cost=2174 Card=645445 Bytes=38726700)
23 22 SORT (UNIQUE)
24 23 TABLE ACCESS (FULL) OF 'SEGMENTATION_USER_COUNTRY_MAP' (Cost=5 Card=43 Bytes=473)
25 22 TABLE ACCESS (FULL) OF 'CONTACT_VALUES1' (Cost=2151 Card=1907737 Bytes=93479113)
=====================================================
Execution Plan -SLOW
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=8751 Card=1 Bytes=73 )
1 0 SORT (AGGREGATE)
2 1 HASH JOIN (SEMI) (Cost=8751 Card=14477 Bytes=1056821)
3 2 MERGE JOIN (Cost=8583 Card=89527 Bytes=5550674)
4 3 TABLE ACCESS (BY INDEX ROWID) OF 'CONTACT_VALUES1' ( Cost=826 Card=1852109 Bytes=90753341)
5 4 INDEX (FULL SCAN) OF 'CONTACT_VALUES1_PK' (UNIQUE) (Cost=26 Card=1852109)
6 3 SORT (JOIN) (Cost=7757 Card=89527 Bytes=1163851)
7 6 VIEW (Cost=7454 Card=89527 Bytes=1163851)
8 7 INTERSECTION
9 8 SORT (UNIQUE)
10 9 INDEX (FAST FULL SCAN) OF 'CONTACT_VALUES1_PK' (UNIQUE) (Cost=5 Card=1852109 Bytes=11112654)
11 8 INTERSECTION
12 11 SORT (UNIQUE)
13 12 HASH JOIN (Cost=640 Card=250676 Bytes=7269604)
14 13 HASH JOIN (Cost=6 Card=28 Bytes=560)
15 14 TABLE ACCESS (BY INDEX ROWID) OF 'SEGMENTATION_QUERY_VALUES' (Cost=3 Card=1 Bytes=13)
16 15 INDEX (RANGE SCAN) OF 'SEG_QUERY_VALUES_PK' (UNIQUE) (Cost=2 Card=2)
17 14 TABLE ACCESS (FULL) OF 'COUNTRY' (Cost =2 Card=120 Bytes=840)
18 13 TABLE ACCESS (FULL) OF 'CONTACT_COUNTRY1 ' (Cost=628 Card=2135370 Bytes=19218330)
19 11 SORT (UNIQUE)
20 19 HASH JOIN (Cost=640 Card=89527 Bytes=25962 83)
21 20 HASH JOIN (Cost=6 Card=10 Bytes=200)
22 21 TABLE ACCESS (BY INDEX ROWID) OF 'SEGMENTATION_QUERY_VALUES' (Cost=3 Card=1 Bytes=13)
23 22 INDEX (RANGE SCAN) OF 'SEG_QUERY_VALUES_PK' (UNIQUE) (Cost=2 Card=2)
24 21 TABLE ACCESS (FULL) OF 'COUNTRY' (Cost =2 Card=120 Bytes=840)
25 20 TABLE ACCESS (FULL) OF 'CONTACT_COUNTRY1 ' (Cost=628 Card=2135370 Bytes=19218330)
26 2 TABLE ACCESS (FULL) OF 'SEGMENTATION_USER_COUNTRY_MAP'
(Cost=5 Card=45 Bytes=495) -
Oracle function and query return different results
Hi, I am using oracle 10g database.
Function is :
create or replace FUNCTION FUNC_FAAL(myCode number,firstDate date
*, secondDate date)*
RETURN INTEGER as
rtr integer;
BEGIN
select count() into rtr*
from myschema.my_table tbl where tbl.myDateColumn between firstDate and
secondDate and tbl.kkct is null and tbl.myNumberColumn = myCode ;
return (rtr);
END FUNC_FAAL;
This function returns 117177 as result.
But if I run same query in the function seperately ;
select count()*
from myschema.my_table tbl
where tbl.myDateColumn between firstDate and secondDate
and tbl.kkct is null and tbl.myNumberColumn = myCode ;
I get different result 11344 (which is the right one).
Table and function are in the same schema.
What can be the problem ?
Thanks.1. i think ur parameter name and Column names are same Firstdate and seconddate try to choose different name
2. try using Trunc function around your dates
where trunc(tbl.myDateColumn) between trunc(firstDate) and trunc(secondDate)then compare the result....sometimes time elements comes into play.
Baig
[My Oracle Blog|http://baigsorcl.blogspot.com/] -
Same query returning different result set
hi all,
i am using db 10g.
i have a query like below
SELECT SUM(EID_AMOUNT)amt,EID_INCR_CODE,EIH_EFF_DATE
FROM EMP_INC_HEADER,EMP_INC_DETAILS
WHERE EIH_EMP_CODE = EID_EMP_CODE
AND EIH_EFF_DATE = EIH_EFF_DATE
AND EIH_STATUS = 'P'
AND EID_EMP_CODE = '003848'
GROUP BY EID_INCR_CODE,EIH_EFF_DATE
ORDER BY EID_INCR_CODE,EIH_EFF_DATE;which is leading to the output
AMT EID_INCR_CODE EIH_EFF_D
2000 BASIC 21-SEP-10
2000 BASIC 23-SEP-10
2000 BASIC 15-OCT-10
2000 BASIC 21-OCT-10
1200 HTRAN 21-SEP-10
1200 HTRAN 23-SEP-10
1200 HTRAN 15-OCT-10
1200 HTRAN 21-OCT-10
800 OTHERS 21-SEP-10
800 OTHERS 23-SEP-10
800 OTHERS 15-OCT-10
800 OTHERS 21-OCT-10i have query
SELECT SUM(EID_AMOUNT)amt,EID_INCR_CODE,EID_EFF_DATE
FROM EMP_INC_HEADER,EMP_INC_DETAILS
WHERE EIH_EMP_CODE = EID_EMP_CODE
AND EIH_EFF_DATE = EID_EFF_DATE
AND EIH_STATUS = 'P'
AND EID_EMP_CODE = '003848'
GROUP BY EID_INCR_CODE,EID_EFF_DATE
ORDER BY EID_INCR_CODE,EID_EFF_DATE;
leading to
AMT EID_INCR_CODE EID_EFF_D
500 BASIC 21-SEP-10
500 BASIC 23-SEP-10
500 BASIC 15-OCT-10
500 BASIC 21-OCT-10
300 HTRAN 21-SEP-10
300 HTRAN 23-SEP-10
300 HTRAN 15-OCT-10
300 HTRAN 21-OCT-10
200 OTHERS 21-SEP-10
200 OTHERS 23-SEP-10
200 OTHERS 15-OCT-10
200 OTHERS 21-OCT-10
12 rows selected.what second query is returning is correct. as per the table.
but my question what is the difference between my first and second query.
in what way it is different(i am not getting any idea).
if any one is having any clue please share with me.
Thanks..what is the difference between my first and second queryLook like there's no difference.
I think you really meant something like
SELECT SUM(EID_AMOUNT) amt,EID_INCR_CODE,EIH_EFF_DATE
FROM EMP_INC_HEADER EIH,
EMP_INC_DETAILS EID
WHERE EIH.EMP_CODE = EID.EMP_CODE
AND EIH.EFF_DATE = EIH.EFF_DATE
AND EIH.STATUS = 'P'
AND EID.EMP_CODE = '003848'
GROUP BY EID.INCR_CODE,EIH.EFF_DATE
ORDER BY EID.INCR_CODE,EIH.EFF_DATERegards
Etbin -
Query returning different number of rows standalone vs insert statement
Hi,
We are using Oracle 10g. We are facing a issue where a SELECT inserts 26294 rows when used with a insert statement. The same select (cut-and-paste) when executed standalone, returns only 60 rows. Any idea what could be causing this?
Thanks in advance,
Hari Narayanan
TIAA-CREF
See details below,
SQL> INSERT INTO cref.position_recon_breaks (
2 effective_date,
3 fund_entity_id,
4 security_alias,
5 accounting_system,
6 pace_shares,
7 accounting_sys_shares,
8 accounting_sys_unp_trd_shares
9 )
10 SELECT pr.effective_date,
11 pr.ENTITY_ID,
12 pr.SECURITY_ALIAS,
13 'MELLON',
14 CREF_SHARES PACE_SHARES,
15 CORP_SHARES ACCOUNTING_SYS_SHARES,
16 CORP_UNP_TRD_SHARES ACCOUNTING_SYS_UNP_TRD_SHARES
17 FROM cref.MELLON_POSITION_RECON pr
18 WHERE ABS(SHARES_DIFFERENCE) >= 1;
25294 rows created.
SQL> select count(*) from
2 (SELECT pr.effective_date,
3 pr.ENTITY_ID,
4 pr.SECURITY_ALIAS,
5 'MELLON',
6 CREF_SHARES PACE_SHARES,
7 CORP_SHARES ACCOUNTING_SYS_SHARES,
8 CORP_UNP_TRD_SHARES ACCOUNTING_SYS_UNP_TRD_SHARES
9 FROM cref.MELLON_POSITION_RECON pr
10 WHERE ABS(SHARES_DIFFERENCE) >= 1);
COUNT(*)
60charred/jzhang,
Thanks for your responses. Just as an additional info, MELLON_POSITION_RECON is one hell of a view - not written by me :) - with unions and inline queries.
If it would be of any help, here is the script,
CREATE OR REPLACE FORCE VIEW CREF.MELLON_POSITION_RECON
(EFFECTIVE_DATE, FUND_CODE, ENTITY_ID, ENTITY_NAME, SECURITY_ALIAS,
SECURITY_NAME, PRIMARY_ASSET_ID, CREF_SECURITY_COUNT, CORP_SECURITY_COUNT, CREF_CURRENCY_CODE,
CORP_CURRENCY_CODE, CREF_EXCHANGE_RATE, CORP_EXCHANGE_RATE, CREF_SHARES, CORP_SHARES,
CREF_PRICE_LOCAL, CORP_PRICE_LOCAL, CREF_MARKET_VALUE_USD, CORP_MARKET_VALUE_USD, CREF_MARKET_VALUE_LOCAL,
CORP_MARKET_VALUE_LOCAL, CREF_ACCRUED_INCOME_USD, CORP_ACCRUED_INCOME_USD, CORP_UNP_TRD_MARKET_VALUE, CORP_UNP_TRD_SHARES,
SHARES_DIFFERENCE, SHARES_DIFF_INCL_UNP, LOCAL_PRICE_DIFFERENCE, MKT_VALUE_USD_DIFF, MKT_VALUE_USD_DIFF_INCL_UNP,
ACCRUED_INCOME_DIFF)
AS
SELECT /*+ ORDERED */
P.EFFECTIVE_DATE
,E.CODE FUND_CODE
,P.ENTITY_ID
,E.LONG_NAME ENTITY_NAME
,P.SECURITY_ALIAS
,S.ISSUE_NAME SECURITY_NAME
,S.PRIMARY_ASSET_ID
,P.CREF_SECURITY_COUNT
,P.CORP_SECURITY_COUNT CORP_SECURITY_COUNT
,P.CREF_CURRENCY_CODE
,P.CORP_CURRENCY_CODE CORP_CURRENCY_CODE
,P.CREF_EXCHANGE_RATE
,P.CORP_EXCHANGE_RATE CORP_EXCHANGE_RATE
,P.CREF_SHARES
,P.CORP_SHARES CORP_SHARES
,P.CREF_PRICE_LOCAL
,P.CORP_PRICE_LOCAL CORP_PRICE_LOCAL
,ROUND(P.CREF_MARKET_VALUE_USD,2) CREF_MARKET_VALUE_USD
,P.CORP_MARKET_VALUE_USD CORP_MARKET_VALUE_USD
,ROUND(P.CREF_MARKET_VALUE_LOCAL,2) CREF_MARKET_VALUE_LOCAL
,P.CORP_MARKET_VALUE_LOCAL CORP_MARKET_VALUE_LOCAL
,ROUND(P.CREF_ACCRUED_INCOME_USD,2) CREF_ACCRUED_INCOME_USD
,P.CORP_ACCRUED_INCOME_USD
,P.CORP_UNP_TRD_MARKET_VALUE
,P.CORP_UNP_TRD_SHARES
,NVL(P.CORP_SHARES, 0) - NVL(P.CREF_SHARES, 0) SHARES_DIFFERENCE
,NVL(P.CORP_SHARES,0) +
(NVL(P.CORP_UNP_TRD_SHARES, 0) - NVL(P.CREF_SHARES, 0)) SHARES_DIFF_INCL_UNP
,NVL(P.CREF_PRICE_LOCAL, 0) - NVL(P.CORP_PRICE_LOCAL, 0) LOCAL_PRICE_DIFFERENCE
,NVL(ROUND(P.CREF_MARKET_VALUE_USD,2),0) - NVL(P.CORP_MARKET_VALUE_USD,0) MKT_VALUE_USD_DIFF
,NVL(ROUND(P.CREF_MARKET_VALUE_USD,2),0) -
(NVL(P.CORP_MARKET_VALUE_USD, 0) + NVL(P.CORP_UNP_TRD_MARKET_VALUE,0)) MKT_VALUE_USD_DIFF_INCL_UNP
,NVL(ROUND(P.CREF_ACCRUED_INCOME_USD,2),0) - NVL(P.CORP_ACCRUED_INCOME_USD, 0) ACCRUED_INCOME_DIFF
FROM
SELECT ENTITY_ID
,EFFECTIVE_DATE
,SECURITY_ALIAS
,SUM(CREF_SECURITY_COUNT) CREF_SECURITY_COUNT
,SUM(CORP_SECURITY_COUNT) CORP_SECURITY_COUNT
,MAX(CREF_CURRENCY_CODE) CREF_CURRENCY_CODE
,MAX(CORP_CURRENCY_CODE) CORP_CURRENCY_CODE
,MAX(CREF_EXCHANGE_RATE) CREF_EXCHANGE_RATE
,MAX(CORP_EXCHANGE_RATE) CORP_EXCHANGE_RATE
,SUM(CREF_SHARES) CREF_SHARES
,SUM(CORP_SHARES) CORP_SHARES
,MAX(CREF_PRICE_LOCAL) CREF_PRICE_LOCAL
,MAX(CORP_PRICE_LOCAL) CORP_PRICE_LOCAL
,SUM(CREF_MARKET_VALUE_USD) CREF_MARKET_VALUE_USD
,SUM(CORP_MARKET_VALUE_USD) CORP_MARKET_VALUE_USD
,SUM(CREF_MARKET_VALUE_LOCAL) CREF_MARKET_VALUE_LOCAL
,SUM(CORP_MARKET_VALUE_LOCAL) CORP_MARKET_VALUE_LOCAL
,SUM(CREF_ACCRUED_INCOME_USD) CREF_ACCRUED_INCOME_USD
,SUM(CORP_ACCRUED_INCOME_USD) CORP_ACCRUED_INCOME_USD
,MIN(QUERY_TIMESTAMP) QUERY_TIMESTAMP
,SUM(CORP_UNP_TRD_MARKET_VALUE) CORP_UNP_TRD_MARKET_VALUE
,SUM(CORP_UNP_TRD_SHARES) CORP_UNP_TRD_SHARES
FROM
SELECT -- WANT ONE ROW PER FUND PER SECURITY
/*+ index(p I_POS_ENTID_SRCINTFC_CREF) index (pd) */
E.FUND_ENTITY_ID ENTITY_ID
,P.EFFECTIVE_DATE
,PD.SECURITY_ALIAS
,SYSDATE QUERY_TIMESTAMP
,1 CREF_SECURITY_COUNT
,MAX(PD.LOCAL_CURRENCY) CREF_CURRENCY_CODE
,MAX(PD.MKT_EXCHANGE_RATE) CREF_EXCHANGE_RATE
,SUM(PD.SHARE_PAR_VALUE) CREF_SHARES
,MAX(PD.PRICE) CREF_PRICE_LOCAL
,SUM(PD.MARKET_VALUE) CREF_MARKET_VALUE_USD
,SUM(PD.LOCAL_MARKET_VALUE) CREF_MARKET_VALUE_LOCAL
,SUM(PD.ACCRUED_INCOME) CREF_ACCRUED_INCOME_USD
,0 CORP_SECURITY_COUNT
,TO_CHAR(NULL) CORP_CURRENCY_CODE
,0 CORP_SHARES
,TO_NUMBER(NULL) CORP_PRICE_LOCAL
,TO_NUMBER(NULL) CORP_EXCHANGE_RATE
,0 CORP_MARKET_VALUE_USD
,0 CORP_MARKET_VALUE_LOCAL
,0 CORP_ACCRUED_INCOME_USD
,0 CORP_UNP_TRD_MARKET_VALUE
,0 CORP_UNP_TRD_SHARES
FROM CREF.ENTITY E,
CREF.ENTITY EF fund entity NF
,HOLDINGDBO.POSITION P
,(SELECT MAX(EFFECTIVE_DATE) CURRENT_DATE
FROM HOLDINGDBO.POSITION P,
PACE_MASTERDBO.INTERFACES I
WHERE I.SHORT_DESC = 'MELLON'
AND I.INSTANCE = P.SRC_INTFC_INST) DT
,HOLDINGDBO.POSITION_DETAIL PD
,PACE_MASTERDBO.INTERFACES I
WHERE E.PORTFOLIO_ENTITY_TYPE_CODE = 'PORT'
AND E.ENTITY_ID = P.ENTITY_ID
AND EF.ENTITY_ID = E.FUND_ENTITY_ID -- NF
AND EF.ACCOUNTING_SYSTEM = 'MELLON' -- NF
AND E.ENTITY_ID = P.ENTITY_ID
AND I.SHORT_DESC = 'STARDIRECT'
AND I.INSTANCE = P.SRC_INTFC_INST
AND P.EFFECTIVE_DATE = DT.CURRENT_DATE
AND P.POSITION_ID = PD.POSITION_ID
-- "GROUP BY" COMBINES THE LONG AND SHORT POSITIONS
GROUP BY P.EFFECTIVE_DATE, E.FUND_ENTITY_ID, PD.SECURITY_ALIAS
UNION ALL
SELECT -- CORPORATE SENDS ONE ROW PER FUND PER SECURITY (NO SHORTS)
/*+ index(p I_POS_ENTID_SRCINTFC_CREF) index (pd) */
E.ENTITY_ID
,P.EFFECTIVE_DATE
,PD.SECURITY_ALIAS
,SYSDATE QUERY_TIMESTAMP
,0 CREF_SECURITY_COUNT
,TO_CHAR(NULL) CREF_CURRENCY_CODE
,0 CREF_SHARES
,TO_NUMBER(NULL) CREF_PRICE_LOCAL
,TO_NUMBER(NULL) CREF_EXCHANGE_RATE
,0 CREF_MARKET_VALUE_USD
,0 CREF_MARKET_VALUE_LOCAL
,0 CREF_ACCRUED_INCOME_USD
,1 CORP_SECURITY_COUNT
,PD.LOCAL_CURRENCY CORP_CURRENCY_CODE
,PD.SHARE_PAR_VALUE CORP_SHARES
,PD.PRICE CORP_PRICE_LOCAL
,pd.mkt_exchange_rate CORP_EXCHANGE_RATE
,PD.MARKET_VALUE CORP_MARKET_VALUE_USD
,PD.LOCAL_MARKET_VALUE CORP_MARKET_VALUE_LOCAL
,PD.ACCRUED_INCOME CORP_ACCRUED_INCOME_USD
,0 CORP_UNP_TRD_MARKET_VALUE
,0 CORP_UNP_TRD_SHARES
FROM CREF.ENTITY E
,HOLDINGDBO.POSITION P
,(SELECT MAX(EFFECTIVE_DATE) CURRENT_DATE
FROM HOLDINGDBO.POSITION P,
PACE_MASTERDBO.INTERFACES I
WHERE I.SHORT_DESC = 'MELLON'
AND I.INSTANCE = P.SRC_INTFC_INST) DT
,HOLDINGDBO.POSITION_DETAIL PD
,PACE_MASTERDBO.INTERFACES I
WHERE E.FUND_FLAG = 'Y' --
AND E.ENTITY_ID = P.ENTITY_ID
AND I.SHORT_DESC = 'MELLON'
AND I.INSTANCE = P.SRC_INTFC_INST
AND P.EFFECTIVE_DATE = DT.CURRENT_DATE
AND P.POSITION_ID = PD.POSITION_ID
UNION ALL
SELECT
UTS.FUND_ENTITY_ID ENTITY_ID
,UTS.EFFECTIVE_DATE
,UTS.SECURITY_ALIAS
,SYSDATE QUERY_TIMESTAMP
,0 CREF_SECURITY_COUNT
,TO_CHAR(NULL) CREF_CURRENCY_CODE
,0 CREF_SHARES
,TO_NUMBER(NULL) CREF_PRICE_LOCAL
,TO_NUMBER(NULL) CREF_EXCHANGE_RATE
,0 CREF_MARKET_VALUE_USD
,0 CREF_MARKET_VALUE_LOCAL
,0 CREF_ACCRUED_INCOME_USD
,0 CORP_SECURITY_COUNT
,TO_CHAR(NULL) CORP_CURRENCY_CODE
,0 CORP_SHARES
,TO_NUMBER(NULL) CORP_PRICE_LOCAL
,TO_NUMBER(NULL) CORP_EXCHANGE_RATE
,0 CORP_MARKET_VALUE_USD
,0 CORP_MARKET_VALUE_LOCAL
,0 CORP_ACCRUED_INCOME_USD
,UTS.SUM_MARKET_VALUE CORP_UNP_TRD_MARKET_VALUE
,UTS.SUM_SHARES CORP_UNP_TRD_SHARES
FROM
(SELECT MAX(EFFECTIVE_DATE) CURRENT_DATE
FROM HOLDINGDBO.POSITION P,
PACE_MASTERDBO.INTERFACES I
WHERE I.SHORT_DESC = 'MELLON'
AND I.INSTANCE = P.SRC_INTFC_INST) DT
,cref.UNPROCESSED_TRADES_SUM UTS
WHERE DT.CURRENT_DATE = UTS.EFFECTIVE_DATE
AND UTS.UPDATED_BY = 'MELLON'
GROUP BY EFFECTIVE_DATE, ENTITY_ID, SECURITY_ALIAS
) P,
cref.ENTITY E,
SECURITYDBO.SECMASTER_HISTORY S
WHERE P.ENTITY_ID = E.ENTITY_ID
AND P.SECURITY_ALIAS = S.SECURITY_ALIAS
AND S.SRC_INTFC_INST = (SELECT INSTANCE FROM PACE_MASTERDBO.INTERFACES
WHERE SHORT_DESC = 'EAGLE PACE')
AND S.EFFECTIVE_DATE = (SELECT MAX(S1.EFFECTIVE_DATE)
FROM SECURITYDBO.SECMASTER_HISTORY S1
WHERE S1.SRC_INTFC_INST = S.SRC_INTFC_INST
AND S1.SECURITY_ALIAS = S.SECURITY_ALIAS
AND S1.EFFECTIVE_DATE <= P.EFFECTIVE_DATE); -
Mysql query returns different number of records from coldfusion and navicat
Hi
I'm hoping that someone can suggest a basic strategy to debug this.
I have a fairly large and complicated query that is executed by a function in a cfc.
It returns (for example) 100 rows.
If I use cfdump and then copy and paste the SQL of the query (with the variables, of course) into Navicat and execute exactly the same query on the same mySQL database, it returns 130 rows.
Same SQL string, same database, same data - the only difference is that in one instance Navicat submits the query and in the other, Coldfusion does.
Has anyone ever had anything like this happen before?Ok I found my own bug. Of *course* the sql queries were not identical.. they could not possibly have been. My mistake was thinking that they were.
The problem was part of the WHERE clause:
AND orderid in (500,503,505)
In the coldfusion code this was
AND orderid in (<cfqueryparam cfsqltype="cf_sql_varchar" value="#lstOrderID#">)
which of course rendered in mySQL as AND orderid in ('500,503,505')
This was not immediately apparent as the cfdump returns this as AND orderid in (?) with the variable in the array below. -
Same query giving different results
Hi
I m surprised to see the behaviour of oracle. I have two different sessions for same scheema on same server. In both sessions same query returns different results. The query involves some calculations like sum and divisions on number field.
I have imported this data from another server using export / import utility available with 9i server. Before export every thing was going fine. Is there some problem with this utility.
I m using Developer 6i as the front end for my client server application. The behaviour of my application is very surprizing as once it shows the correct data and if I close the screen and reopen, it shows wrong data.
I m really stucked with the abnormal behaviour. Please tell me the possiblities and corrective action for these conditions.
Regards
Asad.There is nothing uncommitted in both the sessions. But still different results are returned.
I m sending u the exact query and result returned in both sessions.
Session 1:
SQL> rollback;
Rollback complete.
SQL> SELECT CC.CREDIT_HRS,GP.GRADE_PTS
2 FROM GRADE G, COURSE_CODE CC, GRADE_POLICY GP
3 WHERE G.COURSE_CDE=CC.COURSE_CDE
4 AND G.SELECTION_ID=45 AND G.GRADE_TYP=GP.GRADE_TYP
5 AND G.TERM_PROG_ID=17 AND GP.TERM_ID=14
6 /
CREDIT_HRS GRADE_PTS
3 4
4 3.33
4 3.33
3 4
3 4
3 4
3 4
7 rows selected.
SQL>
SESSION 2:
SQL> rollback;
Rollback complete.
SQL> SELECT CC.CREDIT_HRS,GP.GRADE_PTS
2 FROM GRADE G, COURSE_CODE CC, GRADE_POLICY GP
3 WHERE G.COURSE_CDE=CC.COURSE_CDE
4 AND G.SELECTION_ID=45 AND G.GRADE_TYP=GP.GRADE_TYP
5 AND G.TERM_PROG_ID=17 AND GP.TERM_ID=14
6 /
CREDIT_HRS GRADE_PTS
3 4
4 3.33
3 4
3 4
3 4
3 4
6 rows selected.
SQL>
U can see in session 1, seven rows are returned while in session 2 six rows are returned. I have issued a rollback before query to be sure that data in both sessions is same. -
Issue returning a count of rows in a SELECT QUERY...
I am working in Oracle 11.2g and I have a query where I want to return the count of DAILY records as well as WEEKLY records for specific marketers.
Here is my query:
Select Mka_Mktr_No,
Case When Rat_Freq_Cd = 'D' Then Count(*) Else 0 End DailyCount,
case when Rat_Freq_Cd = 'W' then count(*) Else 0 End WeeklyCount
From Marketer_Account, Acct
Where Mka_Exp_Dt >= '01-NOV-2012'
And Mka_Eff_Dt <= '30-NOV-2012'
and rat_acct_no = mka_acct_no
And Rat_Usage_Cd = 'P'
and rat_freq_cd != 'M'
Group By Mka_Mktr_No, Rat_Freq_Cd
Order By Mka_Mktr_No;I would prefer to have the results show up on the SAME row, but instead I get the following results:
MKA_MKTR_NO DAILYCOUNT WEEKLYCOUNT
10005 68 0
10005 0 2 Note how it shows each count on a seperate row. Is there a way to tweak the query to show the counts on the same row:
MKA_MKTR_NO DAILYCOUNT WEEKLYCOUNT
10005 68 2 and not have two seperate rows?
Many thanks,
SeanDon't group by rat_freq_cd
SQL> with test_data as
2 (
3 select 1 mka_mktr_no, 'W' rat_freq_cd from dual union all
4 select 1 mka_mktr_no, 'W' rat_freq_cd from dual union all
5 select 1 mka_mktr_no, 'D' rat_freq_cd from dual union all
6 select 2 mka_mktr_no, 'D' rat_freq_cd from dual union all
7 select 2 mka_mktr_no, 'W' rat_freq_cd from dual union all
8 select 2 mka_mktr_no, 'W' rat_freq_cd from dual union all
9 select 2 mka_mktr_no, 'D' rat_freq_cd from dual union all
10 select 2 mka_mktr_no, 'D' rat_freq_cd from dual union all
11 select 2 mka_mktr_no, 'D' rat_freq_cd from dual union all
12 select 3 mka_mktr_no, 'D' rat_freq_cd from dual union all
13 select 3 mka_mktr_no, 'D' rat_freq_cd from dual union all
14 select 3 mka_mktr_no, 'W' rat_freq_cd from dual union all
15 select 3 mka_mktr_no, 'D' rat_freq_cd from dual union all
16 select 3 mka_mktr_no, 'D' rat_freq_cd from dual union all
17 select 3 mka_mktr_no, 'W' rat_freq_cd from dual
18 )
19 select
20 mka_mktr_no,
21 count(case when rat_freq_cd = 'D' then rat_freq_cd end) dailycount,
22 count(case when rat_freq_cd = 'W' then rat_freq_cd end) weeklycount
23 from
24 test_data
25 group by mka_mktr_no;
MKA_MKTR_NO DAILYCOUNT WEEKLYCOUNT
1 1 2
2 4 2
3 4 2 -
Need to return data from a query in different ways - Please help
We are using 10g R2
I have a proc as follows that has a query with over 100 values in the select clause:
proc one( input param1, input_param2,.... output_cursor )
as
begin
open cursor for
select ...about 100 values with most of them being calculated
from table1, view 1, table2, table 3, view 2 ...
where ....
and table1.col1 = input param1
and table1.col2 = input param 2
and view1.col5 = input param5...
end;
I need to return the data that comes from the above query in different formats, columns for a report would be different from columns for screen A and different for screen B. I need only certain columns for a report and different set of columns for another screen. I have wrapper procs that get different input params. From the wrapper procs I intend to call the above proc but would like only selected values.
How can I accomplish this? Since my main goal is to select different columns for each wrapper I was thinking of insert the data from the above proc into global temp table and selecting whatever columns and order I want from the wrappers.
What do you think? Any other solutions?
Thanks
Edited by: user565033 on Jan 21, 2013 7:50 PMYou need to clearly separate roles and responsibilities. The PL/SQL code that creates and supplies a cursor handle is server code tasked to supply data. The code that makes the call for server data, is responsible for formatting and rendering that data.
Thus moving data formatting into the server code needs to be question. Simple example. Cursor does not return invoice date as a date - but formats it into a string using TO_CHAR().
This works for client1 - as that is the date format expected. However, client2 has different International settings and specifies a different date format. Invoice date, formatted into a string by the server, now renders in the wrong format on client2.
Server code should not be concerned with rendering and formatting of data send to a client.
As for the idea to use a global temp table is ..., well to put it nicely, it smells. Badly.
The single most expensive operation on a database platform is I/O. And now you want to read server data and write it to temporary storage, and the read data from temporary storage to return to the client? What on earth for!? Why purposefully increase the size of the I/O workload? Why decrease performance and undermine scalability?
Provide a proper abstraction interface to the client. Enable it to specify (as simplistically as possible) what it wants ito data. There are a number of ways to design and implement this in PL/SQL. Simplistic example:
SQL> create or replace package Employees as
2
3 EMP_FULL_DETAILS constant integer := 1;
4 EMP_BASIC_DETAILS constant integer := 2;
5
6 procedure GetEmpByID(
7 cur out sys_refcursor,
8 empID in emp.empno%type,
9 template in integer default EMP_BASIC_DETAILS
10 );
11
12 procedure GetEmpByName(
13 cur out sys_refcursor,
14 empName in emp.ename%type,
15 template in integer default EMP_BASIC_DETAILS
16 );
17 end;
18 /
Package created.
SQL>
SQL> create or replace package body Employees as
2
3 type TArray is table of varchar2(32767);
4
5 TemplateList constant TArray :=
6 new TArray(
7 'EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO ',
8 'EMPNO, ENAME, JOB '
9 );
10
11 procedure GetEmpByID(
12 cur out sys_refcursor,
13 empID in emp.empno%type,
14 template in integer default EMP_BASIC_DETAILS
15 ) is
16 sqlSelect varchar2(32767);
17 begin
18 sqlSelect :=
19 'select '||TemplateList(template)||
20 'from emp where empno = :empID';
21
22 open cur for sqlSelect using empID;
23 end;
24
25 procedure GetEmpByName(
26 cur out sys_refcursor,
27 empName in emp.ename%type,
28 template in integer default EMP_BASIC_DETAILS
29 ) is
30 sqlSelect varchar2(32767);
31 begin
32 sqlSelect :=
33 'select '||TemplateList(template)||
34 'from emp where ename like :empName';
35 open cur for sqlSelect using empName;
36 end;
37
38
39 end;
40 /
Package body created.
SQL>
SQL> var c refcursor
SQL>
SQL> exec Employees.GetEmpByID( :c, 7499 );
PL/SQL procedure successfully completed.
SQL> print c
EMPNO ENAME JOB
7499 ALLEN SALESMAN
SQL>
SQL> exec Employees.GetEmpByName( :c, 'A%', Employees.EMP_FULL_DETAILS );
PL/SQL procedure successfully completed.
SQL> print c
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7499 ALLEN SALESMAN 7698 1981/02/20 00:00:00 1600 300 30
7876 ADAMS CLERK 7788 1987/05/23 00:00:00 1100 20
SQL> -
SQ02 Infoset returns different results in SQ01 Query vs. Segment Builder TG
I want to get Segment Builder to use the same InfoSet and return the same results so, that I can then make a Target Group...but, I'm sure I'm missing some step along the way.
My SQ01 query returns 79 contact persons, but my segment builder only finds 42, none of which appear to actually be the Contact Person, but are in fact the Business Partner the Contact Person has a Relationship to.
I started by copying the CRM_MKTTG_BP_ADDR InfoSet and added the BUT051 table to it, which is when the SQ01 began returning the needed results.
I suspect the problem may be in the Master Group where I am using the B2B/B2C Program, but I don't know if that's the answer or not. I am not declaring an FM as I am using the BP-GUID in the data source, so I don't 'think' that's the issue...
Here's what I'm currently trying as my Data Source settings in case that will help:
Origin Type 03 InfoSet
RFC dest.
Name of InfoSet ZCRM_MKTTG_BP_ADDR
Business Partner BUT000-PARTNER_GUID
Function Module
Sampling InfoSet ZCRM_MKTTG_BP_ADDR_SMP
Sample BUT000-PARTNER_GUID
Object
Partner Function
Description Z BP Address
I already read every Segment Builder forum thread and the Sap Library on the subject, but either I missed something or there is a step not listed.
Anyone have any suggestions?Solved!!
I was wrongly using AND/OR options to add filters in the segmentation tool...ups! -
Multi-row sub query returns ORA-00904 :invalid identifier error
I am creating a report from two tables that I am not joining. I want a single line for every row in table1 that meets a date range. Table2 can contain none or many rows for each recored in table1. I want to get up to two fields from table2.
I was using a case statement to check if there was data and then an in-line query or subquery. Once again, the idea is to have a single line on the report for each table1 record.
I get this error with the code below. It seems the nested multi-row subquery can not see the a.cr_mas_cr_no identifier.
ORA-00904: "a"."cr_mas_cr_no": invalid identifier
Any help is greatly appreciated,
Sam
select
a.cr_mas_cr_no "CRNO", a.cr_mas_type "TYPE", a.cr_mas_status "CR Status",
a.cr_mas_date_logged "Logged date", a.CR_REL_REQ_APP_DATE "RTP approved",a.CR_REL_REQ_RTP_DATE "RTP Date",
a.cr_accepted_date "Complete", a.cr_mas_submitted_by "Requester",
select doc_user FROM crrm_cr_documents WHERE doc_cr_number =a.cr_mas_cr_no and rownum = 1 and DOC_TYPE = 'BD' ) "Bus Design",
(select doc_user FROM crrm_cr_documents WHERE doc_cr_number = a.cr_mas_cr_no and rownum = 1 and DOC_TYPE = 'TD' ) "Tech Design",
(select doc_user FROM crrm_cr_documents WHERE doc_cr_number = a.cr_mas_cr_no and rownum = 1 and DOC_TYPE = 'TE' ) "User acceptance test",
case
when (select count(appr_user) from crrm_cr_approvals where appr_cr_no = a.cr_mas_cr_no and appr_type = 'RTP') > 0
then (select appr_user from (select * from crrm_cr_approvals where appr_cr_no = a.cr_mas_cr_no and appr_type = 'RTP') where rownum = 1)
end
"RTP #1",
case
when (select count(appr_user) from crrm_cr_approvals where appr_cr_no = a.cr_mas_cr_no and appr_type = 'RTP') > 1
then (select appr_user from (select * from crrm_cr_approvals where appr_cr_no = a.cr_mas_cr_no and appr_type = 'RTP') where rownum = 2)
end
"RTP #2",
a.CR_REL_REQ_RTP_BY "Released by",
a.CR_ACCEPTED_BY "Post RTP User Acceptance",
a.cr_mas_title "Title", a.cr_mas_id "ID"
from
crrm_crmaster a
where
(a.CR_REL_REQ_RTP_DATE >= :P1109_BEGDATE and (a.CR_REL_REQ_RTP_DATE <= :P1109_ENDDATE) and
(a.cr_mas_status = 'Complete' or (a.cr_mas_status = 'Release Approved'and a.CR_REL_REQ_APP_DATE < :P1109_ENDDATE))
Message was edited by:
slavanawayIceman,
Thanks for the reply I will try your suggestion.
I will try and explain why I think two subqueries (an in-line query with a subquery?) are required. I will use the creation of the column RTP #1 as the example as the RTP #2 column is only different in the rownum selected.
Looking only at the lines that fail, here is my analysis. (If I rem out the two case lines the query runs, I just don't get two columns of data I need.) I will only examine the first case as the second is changed to extract the second approval via the rownum = 2 criteria. The first statement checks there is at least one RTP approval stored for the request and then gets the user who approved the request if the test is true.
case when
(select count(appr_user) from crrm_cr_approvals where appr_cr_no =a.cr_mas_cr_no and appr_type = 'RTP') > 0
then
The above part works fine and the correct count of approvals is returned.
(select appr_user from (select * from crrm_cr_approvals where appr_cr_no=a.cr_mas_cr_no and appr_type = 'RTP') where rownum = 1)
end
"RTP #1",
I moved the parenthesis to the correct location. There can be multiple approvals for a given parent record. Some parent records need one, some need two approvals. If I replace
(select appr_user from (select * from crrm_cr_approvals where appr_cr_no =a.cr_mas_cr_no and appr_type = 'RTP') where rownum = 1)
with
(select appr_user from approvals where appr_cr_no =a.cr_mas_cr_no and appr_type = 'RTP' and rownum = 1)
The correct result is returned because it returns exactly one row as rownum=1 limits the query. When rownum = 2 then the query returns null as the rownum never gets to two as the rownum column is built via the set created by the second subquery.
The subquery builds a set of approvals for a specific "cr_no" and appr_type of "RTP". the outer query then looks at the rownum of the second query
Here is where I got the rownum information from;
http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
So here is what I think is happening;
1. Main query From and Where are processed. This should provide the "set" for the query
2.The from subqueries for RTP #1 and RTP #2 should be able to access the a.cr_mas_cr_no field and build a set from the approvals table.
3.The RTP #1/2 subquery (inline maybe a better description?) would then get the correct row from the from subquery.
The error "invalid identifier" refers to the a.cr_mas_cr_no field. I assume it means it can not resolve the table alias inside the subquery.
So maybe your grouping would help, I will try.
Sam -
Query return wrong number of records
In a function, I use a variable declare as my_var table_name1.column_name%type.
This variable is used to to a test on a foreign key when I do a query on another table than table_name1 the number of rows return by the query is different than the number of rows return if I do the test directly with the value accorded.
Ex:
declare
my_var table_name1.column_name%type;
row_num number;
begin
my_var := 10;
select count(1) into row_num
from table_name2
where column_name = my_var;
dbms_output.put_line(row_num);
select count(1) into row_num
from table_name2
where column_name = 10;
dbms_output.put_line(row_num);
end;
the first query will return 2 and the second will return 1.
Can someone explain me why I have this strange result ?
Edited by: 897304 on Nov 15, 2011 5:12 AM
Edited by: 897304 on Nov 15, 2011 5:32 AMI cannot put the description of the table :-/
The type of the field is varchar2(12)
This is the function the value past to param1 came from oracle 11 and the table tab1 is in oracle10.
The first query work and return 1 (result expected) and the second return 2.
FUNCTION getValue(param1 VARCHAR2) RETURN number AS
var1 VARCHAR2(12 BYTE);
res1 number;
BEGIN
var1 := param1;
SELECT count(1)
INTO res1
FROM tab1
WHERE col2 = var1;
SELECT count(1)
INTO res1
FROM tab1
WHERE col2 = param1;
RETURN res1;
END getValue;
Maybe you are looking for
-
Is it possible to upgrade the 10.5.8 OS on a MacBook Pro with a 2.4 GHz/Intel Core 2 Duo with 2 GB of RAm to the latest OS?
-
Shorten a script, add a repeating action to a handler
Really not sure how to do this, but I have script that processes an image. The thing is has the same action repeating over again yet there is very little difference Everytime they have a different width to be sized to dependant on the brand, so this
-
PDF/a1b file in Acrobat 9.0
I have created a PDF/a1b file in Acrobat 9.0, it validates as compliant, but when my client verifies the compliancy in Acrobat 8.0 it fails-(CIDset in subset fonts). I used Preflight to convert and verify and I received the green check mark. Are ther
-
How to create a huge interactive map?
Hello everybody I've started learning Flash two weeks ago and I have the challenge of creating an interactive political map with about 4000 cities. Some cities will have more than 600 lines defining their limits which will make this project very dema
-
32bit Oracle ODBC Driver on 64bit RHEL 5.9 system
I am using unixODBC as my driver manager and 32 bit Oracle Client/Driver on Linux RHEL 5.9 64bit OS. I cannot seem to get a successful test with isql with 32bit ODBC Driver, i get success with 64bit ODBC Driver.