Did I misuse 'DISTINCT' in this SQL statement?
I have a table TI_ORDER only contains following data:
O_ID E_CODE M_SEQ B_SEQ
VARCHAR NUMBER NUMBER NUMBER
CE013 1 1 1
CE013 1 2 1
CE013 1 3 1
CE013 1 4 1
CE013 1 5 1
CE013 1 6 1
CE013 1 6 2
CE013 1 7 1
CE013 1 8 1
CE013 1 8 2
CE013 1 9 1
CE013 1 10 1
CE013 1 10 2
CE013 2 1 1
CE013 2 2 1
CE013 2 3 1
CE013 2 4 1
CE013 2 5 1
CE013 2 6 1
CE013 2 6 2
CE013 2 7 1
CE013 2 8 1
CE013 2 8 2
CE013 2 9 1
CE013 2 10 1
CE013 2 10 2
If I execute this SQL:
==============================================
SELECT a.o_id, a.e_code,
COUNT(a.o_id) OVER (PARTITION BY a.o_id, a.e_code) AS cnt
FROM ( SELECT DISTINCT o_id, e_code, m_seq FROM ti_order ) a
WHERE a.o_id = 'CE013'
==============================================
It will show:
==============================================
CE013 1 10
CE013 1 10
CE013 1 10
CE013 1 10
CE013 1 10
CE013 1 10
CE013 1 10
CE013 1 10
CE013 1 10
CE013 1 10
CE013 2 10
CE013 2 10
CE013 2 10
CE013 2 10
CE013 2 10
CE013 2 10
CE013 2 10
CE013 2 10
CE013 2 10
CE013 2 10
=============================================
If I add 'DISTINCT' to previous SQL statement:
============================================
SELECT DISTINCT a.o_id, a.e_code,
COUNT(a.o_id) OVER (PARTITION BY a.o_id, a.e_code) AS cnt
FROM ( SELECT DISTINCT o_id, e_code, m_seq FROM ti_order ) a
WHERE a.o_id = 'CE013'
============================================
It displays:
============================================
CE013 1 13
CE013 2 13
============================================
Why does it not show following output as I want ?
============================================
CE013 1 10
CE013 2 10
============================================
Looks like you have stumbled across a bug. Below output indicates that 9i (example 2) gets the correct answer here while 8i (example 1) does not. You ARE on 8i right?
In any case, there is no need to use analytic functions here, good old COUNT is fine to get the correct answer (example 3).
--------------------------- example 1 -----------------------------
Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production
With the Partitioning option
JServer Release 8.1.7.4.0 - Production
SQL> CREATE TABLE table_name (
2 o_id VARCHAR2 (5),
3 e_code NUMBER (2),
4 m_seq NUMBER (2),
5 b_seq NUMBER (2));
Table created.
SQL> INSERT INTO TABLE_NAME VALUES ('CE013','1','1','1');
1 row created.
SQL> INSERT INTO TABLE_NAME VALUES ('CE013','1','2','1');
(snip)
SQL> INSERT INTO TABLE_NAME VALUES ('CE013','2','10','2');
1 row created.
SQL> SELECT DISTINCT a.o_id, a.e_code,
2 COUNT (a.o_id) OVER (PARTITION BY a.o_id, a.e_code) AS cnt
3 FROM (SELECT DISTINCT o_id, e_code, m_seq
4 FROM table_name) a
5 WHERE a.o_id = 'CE013';
O_ID E_CODE CNT
CE013 1 13
CE013 2 13
SQL>
--------------------------- example 2 -----------------------------
Oracle9i Enterprise Edition Release 9.2.0.4.0 - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.4.0 - Production
SQL> CREATE TABLE table_name (
2 o_id VARCHAR2 (5),
3 e_code NUMBER (2),
4 m_seq NUMBER (2),
5 b_seq NUMBER (2));
SQL> INSERT INTO TABLE_NAME VALUES ('CE013','1','1','1');
1 row created.
SQL> INSERT INTO TABLE_NAME VALUES ('CE013','1','2','1');
1 row created.
(snip)
SQL> INSERT INTO TABLE_NAME VALUES ('CE013','2','10','2');
1 row created.
SQL> SELECT DISTINCT a.o_id, a.e_code,
2 COUNT (a.o_id) OVER (PARTITION BY a.o_id, a.e_code) AS cnt
3 FROM (SELECT DISTINCT o_id, e_code, m_seq
4 FROM table_name) a
5 WHERE a.o_id = 'CE013';
O_ID E_CODE CNT
CE013 1 10
CE013 2 10
SQL>
--------------------------- example 3 -----------------------------
Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production
With the Partitioning option
JServer Release 8.1.7.4.0 - Production
SQL> SELECT a.o_id, a.e_code, COUNT (*)
2 FROM (SELECT DISTINCT o_id, e_code, m_seq
3 FROM table_name) a
4 WHERE a.o_id = 'CE013'
5 GROUP BY a.o_id, a.e_code;
O_ID E_CODE COUNT(*)
CE013 1 10
CE013 2 10
SQL>
Padders
Similar Messages
-
Whats wrong with this sql statement ??
Hello all, I am trying to run the below query out of persheet(tanel poder) performance excel chart...but i get below error...db is on 9.2
what is wrong with this sql statement ?
http://blog.tanelpoder.com/2008/12/28/performance-visualization-made-easy-perfsheet-20-beta/
select * from (
with fsq as (
select /*+ materialize */
i.dbid
, i.instance_name
, i.instance_number
-- , trunc(s.snap_time, 'DD') DAY
-- , to_number(to_char(s.snap_time, 'HH24')) HOUR
-- -- , to_char(s.snap_time, 'MI') MINUTE
-- , 0 MINUTE
, trunc(
lag(s.snap_time, 1)
over(
partition by
v.dbid
, i.instance_name
, v.instance_number
, v.event
order by
s.snap_time
, 'HH24'
) SNAP_TIME
, v.event_type EVENT_TYPE
, v.event EVENT_NAME
, nvl(
decode(
greatest(
time_waited_micro,
nvl(
lag(time_waited_micro,1,0)
over(
partition by
v.dbid
, i.instance_name
, v.instance_number
, v.event
order by v.snap_id
, time_waited_micro
time_waited_micro,
time_waited_micro - lag(time_waited_micro,1,0)
over (
partition by
v.dbid
, i.instance_name
, v.instance_number
, v.event
order by v.snap_id
time_waited_micro
, time_waited_micro
) / 1000000 SECONDS_SPENT
, total_waits WAIT_COUNT
from
(select distinct dbid, instance_name, instance_number from stats$database_instance) i
, stats$snapshot s
, ( select
snap_id, dbid, instance_number, 'WAIT' event_type, event, time_waited_micro, total_waits
from
stats$system_event
where
event not in (select event from stats$idle_event)
union all
select
snap_id, dbid, instance_number,
case
when name in ('CPU used by this session', 'parse time cpu', 'recursive cpu usage') then 'CPU'
when name like 'OS % time' then 'OS'
else 'STAT'
end,
name , value, 1
from
stats$sysstat
-- where name in ('CPU used by this session', 'parse time cpu', 'recursive cpu usage')
-- or name like('OS % time')
-- or 1 = 2 -- this will be a bind variable controlling whether all stats need to be returned
) v
where
i.dbid = s.dbid
and i.dbid = v.dbid
and s.dbid = v.dbid
and s.snap_id = v.snap_id
and s.snap_time between '%FROM_DATE%' and '%TO_DATE%'
and i.instance_name = '%INSTANCE%'
select * from (
select
instance_name
, instance_number
, snap_time
, trunc(snap_time, 'DD') DAY
, to_char(snap_time, 'HH24') HOUR
, to_char(snap_time, 'MI') MINUTE
, event_type
, event_name
, seconds_spent
, wait_count
, ratio_to_report(seconds_spent) over (
-- partition by (to_char(day, 'YYYYMMDD')||to_char(hour,'09')||to_char(minute, '09'))
partition by (snap_time)
) ratio
from fsq
where
snap_time is not null -- lag(s.snap_time, 1) function above will leave time NULL for first snapshot
-- to_char(day, 'YYYYMMDD')||to_char(hour,'09')||to_char(minute, '09')
-- > ( select min(to_char(day, 'YYYYMMDD')||to_char(hour,'09')||to_char(minute, '09')) from fsq)
where ratio > 0
order by
instance_name
, instance_number
, day
, hour
, minute
, event_type
, seconds_spent desc
, wait_count desc
Error at line 6
ORA-00604: error occurred at recursive SQL level 1
ORA-00972: identifier is too longHi Alex,
Subquery factoring a.k.a. the with-clause should be possible on 9.2:
http://download.oracle.com/docs/cd/B10501_01/server.920/a96540/statements_103a.htm#2075888
(used it myself as well on 9.2)
@OP
I recall having problems myself using PL/SQL Developer and trying to get the with clause to work on 9.2 some years ago.
A workaround might be to create a view based on the query.
Also, your error message is "ORA-00972: identifier is too long"...
http://download.oracle.com/docs/cd/B19306_01/server.102/b14219/e900.htm#sthref419
Can't test things currently, no 9.2 available at the moment, but perhaps tomorrow I'll have a chance. -
Hi
Please can you help me to create this SQL statement ?
I have two tables: table1 consist of data (AAA,BBB), table2 consist of data(AAA)
And there is relation between table1 and table2 for example (Invoice_id) .
My question is ( How can create SQL statement to display only data in table1(BBB) but not available In table2 ?
Thanks and best regards .
KhaledTry this:
SQL> ed
Wrote file afiedt.buf
1 with t as (select 'AAA' col1,1 inv_id from dual
2 UNION select 'BBB',1 from dual)
3 , s as (select 'AAA' col1,1 inv_id from dual)
4 SELECT col1,inv_id FROM t
5* WHERE NOT EXISTS (SELECT 1 FROM s WHERE inv_id = t.inv_id AND col1 = t.col1)
SQL> /
COL INV_ID
BBB 1
SQL> -
Can someone help me correct this sql statement in a jsp page?
ive been getting the java.sql.SQLException: Incorrect syntax error for one of my sql nested statements. i cant seem to find similar egs online, so reckon if anyone here could help, really appreciate it.
as im putting the nested sql in jsp page, it has to be with lots of " " n crap. very confusing if there are nested.
heres the sql statement without those "" that i want to use:
select top 5 * from(
select top+"'"+offset+"'"+" * from prod where cat=" +"'" cat "'"+"
)order by prodID desc
when i put this in my jsp pg, i had to add "" to become:
String sql = "select top 5 * from("+"select top"+"'"+offset+"'"+" * from prod where cat=" +"'" +cat+ "'"+")order by prodID desc";cat=" +"'" cat "'"+")order by prodID desc";
all those "" are confusing me to no end, so i cant figure out what should be the correct syntax. the error says the syntax error is near the offset.If offset is, say, 10, and cat is, say, "new", then it looks like you're going to produce the SQL:
select top 5 * from(
select top '10' * from prod where cat='new'
)order by prodID descThat looks exactly like incorrect syntax to me... top almost certainly can't handle a string literal as its operand... you almost certainly would want "top 10" instead of "top '10'"...
If you use PreparedStatement, you don't have to remember what you quote and what you don't and you can have your SQL in a single static final string to boot... -
What's wrong with this SQL Statement?
I hope somebody can help explain to me what is wrong wiht the
following SQL statement in my Recordest. It does not return an
error, but it will only filter records from the first variable
listed, 'varFirstName%'. If I try to use any other variables on my
search form,for example LastName, it returns all records. Why is it
doing this?
Here is the SQL statement:
SELECT *
FROM [Sysco Food Show Contacts]
WHERE FirstName LIKE 'varFirstName%' AND LastName LIKE
'varLastName%' AND OrganizationName LIKE 'varOrganizationName%' AND
Address LIKE 'varAddress%' AND City LIKE 'varCity%' AND State LIKE
'varState' AND PostalCode LIKE 'varPostalCode%'
The variables are defined as below:
Name Default Value Run-Time Value
varFirstName % Request.Form("FirstName")
varLastName % Request.Form("LastName")
...and such with all variables defined the same way.
Any help would be much appreciated. I am pulling my hair out
trying to make this Search Form work.
Thanks, mparsons2000PLEASE IGONRE THIS QUESTION!
There was nothing wrong with the statement. I had made
another STUDIP mistake! -
Need help on how to code this SQL statement! (one key has leading zeros)
Good day, everyone!
First of all, I apologize if this isn't the best forum. I thought of putting it in the SAP Oracle database forum, but the messages there seemed to be geared outside of ABAP SELECTs and programming. Here's my question:
I would like to join the tables FMIFIIT and AUFK. The INNER JOIN will be done between FMIFIIT's MEASURE (Funded Program) field, which is char(24), and AUFK's AUFNR (Order Number) field, which is char(12).
The problem I'm having is this: All of the values in AUFNR are preceeded by two zeros. For example, if I have a MEASURE value of '5200000017', the corresponding value in AUFNR is '005200000017'. Because I have my SQL statement coded to just match the two fields, I obviously get no records returned because, I assume, of those leading zeros.
Unfortunately, I don't have a lot of experience coding SQL, so I'm not sure how to resolve this.
Please help! As always, I will award points to ALL helpful responses!
Thanks!!
Dave>
Dave Packard wrote:
> Good day, everyone!
> I would like to join the tables FMIFIIT and AUFK. The INNER JOIN will be done between FMIFIIT's MEASURE (Funded Program) field, which is char(24), and AUFK's AUFNR (Order Number) field, which is char(12).
>
> The problem I'm having is this: All of the values in AUFNR are preceeded by two zeros. For example, if I have a MEASURE value of '5200000017', the corresponding value in AUFNR is '005200000017'. Because I have my SQL statement coded to just match the two fields, I obviously get no records returned because, I assume, of those leading zeros.
> Dave
You can't do a join like this in SAP's open SQL. You could do it in real SQL ie EXEC.... ENDEXEC by using SUSBTR to strip off the leading zeros from AUFNR but this would not be a good idea because a) modifying a column in the WHERE clause will stop any index on that column being used and b) using real SQL rather than open SQL is really not something that should be encouraged for database portability reasons etc.
Forget about a database join and do it in two stages; get your AUFK data into an itab, strip off the leading zeros, and then use FAE to get the FMIFIIT data (or do it the other way round).
I do hope you've got an index on your FMIFIIT MEASURE field (we don't have one here); otherwise your SELECT could be slow if the table holds a lot of data. -
This SQL statement always in Top Activity, with PX Deq Credit: send blkd
Hi gurus,
The following SQL statement is always among the Top Activity. I can see the details in Enerprise manager that it suffers from PX Deq Credit: send blkd
This is the statement:
SELECT S.Product, S.WH_CODE, S.RACK, S.BATCH, S.EXP_DATE, FLOOR(Qty_Beg) QtyBeg_B,
ROUND(f_convert_qty(S.PRODUCT, Qty_Beg-FLOOR(Qty_Beg), P.UOM_K ), 0) QtyBeg_K,
FLOOR(Qty_In) QtyIn_B, ROUND(f_convert_qty(S.PRODUCT, Qty_In-FLOOR(Qty_In), P.UOM_K), 0) QtyIn_K,
FLOOR(Qty_Out) QtyOut_B, ROUND(f_convert_qty(S.PRODUCT, Qty_Out-FLOOR(Qty_Out), P.UOM_K ), 0) QtyOut_K,
FLOOR(Qty_Adj) QtyAdj_B, ROUND(f_convert_qty(S.PRODUCT, Qty_Adj-FLOOR(Qty_Adj), P.UOM_K ), 0) QtyAdj_K,
FLOOR(Qty_End) QtyEnd_B, ROUND(f_convert_qty(S.PRODUCT, Qty_End-FLOOR(Qty_End), P.UOM_K ), 0) QtyEnd_K,
S.LOC_CODE
FROM V_STOCK_DETAIL S
JOIN PRODUCTS P ON P.PRODUCT = S.PRODUCT
WHERE S.Product = :pProduct AND S.WH_CODE = :pWhCode AND S.LOC_CODE = :pLocCode;The statement is invoked by our front end (web based app) for a browse table displayed on a web page. The result can be 10 to 8000. It is used to display the current stock availability for a particular product in a particular warehouse. The stock availability it self is kept in a View : V_Stock_Detail
These are the parameters relevant to the optimizer:
SQL> show parameter user_dump_dest
user_dump_dest string /u01/app/oracle/admin/ITTDB/udump
SQL> show parameter optimizer
_optimizer_cost_based_transformation string OFF
optimizer_dynamic_sampling integer 2
optimizer_features_enable string 10.2.0.3
optimizer_index_caching integer 0
optimizer_index_cost_adj integer 100
optimizer_mode string ALL_ROWS
optimizer_secure_view_merging boolean TRUE
SQL> show parameter db_file_multi
db_file_multiblock_read_count integer 16
SQL> show parameter db_block_size column sname format a20 column pname format a20
db_block_size integer 8192Here is the output of EXPLAIN PLAN:
SQL> explain plan for
SELECT S.Product, S.WH_CODE, S.RACK, S.BATCH, S.EXP_DATE, FLOOR(Qty_Beg) QtyBeg_B,
ROUND(f_convert_qty(S.PRODUCT, Qty_Beg-FLOOR(Qty_Beg), P.UOM_K ), 0) QtyBeg_K,
FLOOR(Qty_In) QtyIn_B, ROUND(f_convert_qty(S.PRODUCT, Qty_In-FLOOR(Qty_In), P.UOM_K), 0) QtyIn_K,
FLOOR(Qty_Out) QtyOut_B, ROUND(f_convert_qty(S.PRODUCT, Qty_Out-FLOOR(Qty_Out), P.UOM_K ), 0) QtyOut_K,
FLOOR(Qty_Adj) QtyAdj_B, ROUND(f_convert_qty(S.PRODUCT, Qty_Adj-FLOOR(Qty_Adj), P.UOM_K ), 0) QtyAdj_K,
FLOOR(Qty_End) QtyEnd_B, ROUND(f_convert_qty(S.PRODUCT, Qty_End-FLOOR(Qty_End), P.UOM_K ), 0) QtyEnd_K,
S.LOC_CODE
FROM V_STOCK_DETAIL S
JOIN PRODUCTS P ON P.PRODUCT = S.PRODUCT
WHERE S.Product = :pProduct AND S.WH_CODE = :pWhCode AND S.LOC_CODE = :pLocCode
Explain complete.
Elapsed: 00:00:00:31
SQL> select * from table(dbms_xplan.display)
PLAN_TABLE_OUTPUT
Plan hash value: 3252950027
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | TQ |IN-OUT| PQ
Distrib |
| 0 | SELECT STATEMENT | | 1 | 169 | 6 (17)| 00:00:01 | | |
|
| 1 | PX COORDINATOR | | | | | | | |
|
| 2 | PX SEND QC (RANDOM) | :TQ10003 | 1 | 169 | 6 (17)| 00:00:01 | Q1,03 | P->S | QC
(RAND) |
| 3 | HASH GROUP BY | | 1 | 169 | 6 (17)| 00:00:01 | Q1,03 | PCWP |
|
| 4 | PX RECEIVE | | 1 | 169 | 6 (17)| 00:00:01 | Q1,03 | PCWP |
|
| 5 | PX SEND HASH | :TQ10002 | 1 | 169 | 6 (17)| 00:00:01 | Q1,02 | P->P | HA
SH |
| 6 | HASH GROUP BY | | 1 | 169 | 6 (17)| 00:00:01 | Q1,02 | PCWP |
|
| 7 | NESTED LOOPS OUTER | | 1 | 169 | 5 (0)| 00:00:01 | Q1,02 | PCWP |
|
| 8 | MERGE JOIN CARTESIAN | | 1 | 119 | 4 (0)| 00:00:01 | Q1,02 | PCWP |
|
| 9 | SORT JOIN | | | | | | Q1,02 | PCWP |
|
| 10 | NESTED LOOPS | | 1 | 49 | 4 (0)| 00:00:01 | Q1,02 | PCWP |
|
| 11 | BUFFER SORT | | | | | | Q1,02 | PCWC |
|
| 12 | PX RECEIVE | | | | | | Q1,02 | PCWP |
|
| 13 | PX SEND BROADCAST | :TQ10000 | | | | | | S->P | BR
OADCAST |
|* 14 | INDEX RANGE SCAN | PRODUCTS_IDX2 | 1 | 25 | 2 (0)| 00:00:01 | | |
|
| 15 | PX BLOCK ITERATOR | | 1 | 24 | 2 (0)| 00:00:01 | Q1,02 | PCWC |
|
|* 16 | MAT_VIEW ACCESS FULL | MV_CONVERT_UOM | 1 | 24 | 2 (0)| 00:00:01 | Q1,02 | PCWP |
|
| 17 | BUFFER SORT | | 1 | 70 | 2 (0)| 00:00:01 | Q1,02 | PCWP |
|
| 18 | BUFFER SORT | | | | | | Q1,02 | PCWC |
|
| 19 | PX RECEIVE | | 1 | 70 | 4 (0)| 00:00:01 | Q1,02 | PCWP |
|
| 20 | PX SEND BROADCAST | :TQ10001 | 1 | 70 | 4 (0)| 00:00:01 | | S->P | BR
OADCAST |
|* 21 | TABLE ACCESS BY INDEX ROWID| STOCK | 1 | 70 | 4 (0)| 00:00:01 | | |
|
|* 22 | INDEX RANGE SCAN | STOCK_PK | 1 | | 2 (0)| 00:00:01 | | |
|
|* 23 | TABLE ACCESS BY INDEX ROWID | MV_TRANS_STOCK | 1 | 50 | 3 (0)| 00:00:01 | Q1,02 | PCWP |
|
|* 24 | INDEX RANGE SCAN | MV_TRANS_STOCK_IDX1 | 1 | | 2 (0)| 00:00:01 | Q1,02 | PCWP |
|
Predicate Information (identified by operation id):
14 - access("P"."PRODUCT"=:PPRODUCT)
16 - filter("CON"."PRODUCT"=:PPRODUCT)
21 - filter("STOCK"."LOC_CODE"=:PLOCCODE)
22 - access("STOCK"."PRODUCT"=:PPRODUCT AND "STOCK"."WH_CODE"=:PWHCODE)
23 - filter("STS"(+)='N')
24 - access("PRODUCT"(+)=:PPRODUCT AND "WH_CODE"(+)=:PWHCODE AND "LOC_CODE"(+)=:PLOCCODE AND "RACK"(+)="STOCK"."RACK" AND
"BATCH"(+)="STOCK"."BATCH" AND "EXP_DATE"(+)="STOCK"."EXP_DATE")
42 rows selected.
Elapsed: 00:00:00:06Here is the output of SQL*Plus AUTOTRACE including the TIMING information:
SQL> SELECT S.Product, S.WH_CODE, S.RACK, S.BATCH, S.EXP_DATE, FLOOR(Qty_Beg) QtyBeg_B,
ROUND(f_convert_qty(S.PRODUCT, Qty_Beg-FLOOR(Qty_Beg), P.UOM_K ), 0) QtyBeg_K,
FLOOR(Qty_In) QtyIn_B, ROUND(f_convert_qty(S.PRODUCT, Qty_In-FLOOR(Qty_In), P.UOM_K), 0) QtyIn_K,
FLOOR(Qty_Out) QtyOut_B, ROUND(f_convert_qty(S.PRODUCT, Qty_Out-FLOOR(Qty_Out), P.UOM_K ), 0) QtyOut_K,
FLOOR(Qty_Adj) QtyAdj_B, ROUND(f_convert_qty(S.PRODUCT, Qty_Adj-FLOOR(Qty_Adj), P.UOM_K ), 0) QtyAdj_K,
FLOOR(Qty_End) QtyEnd_B, ROUND(f_convert_qty(S.PRODUCT, Qty_End-FLOOR(Qty_End), P.UOM_K ), 0) QtyEnd_K,
S.LOC_CODE
FROM V_STOCK_DETAIL S
JOIN PRODUCTS P ON P.PRODUCT = S.PRODUCT
WHERE S.Product = :pProduct AND S.WH_CODE = :pWhCode AND S.LOC_CODE = :pLocCode
Execution Plan
0 SELECT STATEMENT Optimizer Mode=ALL_ROWS 1 169 6
1 0 PX COORDINATOR
2 1 PX SEND QC (RANDOM) SYS.:TQ10003 1 169 6 :Q1003 P->S QC (RANDOM)
3 2 HASH GROUP BY 1 169 6 :Q1003 PCWP
4 3 PX RECEIVE 1 169 6 :Q1003 PCWP
5 4 PX SEND HASH SYS.:TQ10002 1 169 6 :Q1002 P->P HASH
6 5 HASH GROUP BY 1 169 6 :Q1002 PCWP
7 6 NESTED LOOPS OUTER 1 169 5 :Q1002 PCWP
8 7 MERGE JOIN CARTESIAN 1 119 4 :Q1002 PCWP
9 8 SORT JOIN :Q1002 PCWP
10 9 NESTED LOOPS 1 49 4 :Q1002 PCWP
11 10 BUFFER SORT :Q1002 PCWC
12 11 PX RECEIVE :Q1002 PCWP
13 12 PX SEND BROADCAST SYS.:TQ10000 S->P BROADCAST
14 13 INDEX RANGE SCAN ITT_NEW.PRODUCTS_IDX2 1 25 2
15 10 PX BLOCK ITERATOR 1 24 2 :Q1002 PCWC
16 15 MAT_VIEW ACCESS FULL ITT_NEW.MV_CONVERT_UOM 1 24 2 :Q1002 PCWP
17 8 BUFFER SORT 1 70 2 :Q1002 PCWP
18 17 BUFFER SORT :Q1002 PCWC
19 18 PX RECEIVE 1 70 4 :Q1002 PCWP
20 19 PX SEND BROADCAST SYS.:TQ10001 1 70 4 S->P BROADCAST
21 20 TABLE ACCESS BY INDEX ROWID ITT_NEW.STOCK 1 70 4
22 21 INDEX RANGE SCAN ITT_NEW.STOCK_PK 1 2
23 7 TABLE ACCESS BY INDEX ROWID ITT_NEW.MV_TRANS_STOCK 1 50 3 :Q1002 PCWP
24 23 INDEX RANGE SCAN ITT_NEW.MV_TRANS_STOCK_IDX1 1 2 :Q1002 PCWP
Statistics
570 recursive calls
0 physical write total IO requests
0 physical write total multi block requests
0 physical write total bytes
0 physical writes direct temporary tablespace
0 java session heap live size max
0 java session heap object count
0 java session heap object count max
0 java session heap collected count
0 java session heap collected bytes
83 rows processed
Elapsed: 00:00:03:24
SQL> disconnect
Commit complete
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, OLAP and Data Mining optionsThe TKPROF output for this statement looks like the following:
TKPROF: Release 10.2.0.3.0 - Production on Thu Apr 23 12:39:29 2009
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Trace file: ittdb_ora_9566_mytrace1.trc
Sort options: default
count = number of times OCI procedure was executed
cpu = cpu time in seconds executing
elapsed = elapsed time in seconds executing
disk = number of physical reads of buffers from disk
query = number of buffers gotten for consistent read
current = number of buffers gotten in current mode (usually for update)
rows = number of rows processed by the fetch or execute call
SELECT S.Product, S.WH_CODE, S.RACK, S.BATCH, S.EXP_DATE, FLOOR(Qty_Beg) QtyBeg_B,
ROUND(f_convert_qty(S.PRODUCT, Qty_Beg-FLOOR(Qty_Beg), P.UOM_K ), 0) QtyBeg_K,
FLOOR(Qty_In) QtyIn_B, ROUND(f_convert_qty(S.PRODUCT, Qty_In-FLOOR(Qty_In), P.UOM_K), 0) QtyIn_K,
FLOOR(Qty_Out) QtyOut_B, ROUND(f_convert_qty(S.PRODUCT, Qty_Out-FLOOR(Qty_Out), P.UOM_K ), 0) QtyOut_K,
FLOOR(Qty_Adj) QtyAdj_B, ROUND(f_convert_qty(S.PRODUCT, Qty_Adj-FLOOR(Qty_Adj), P.UOM_K ), 0) QtyAdj_K,
FLOOR(Qty_End) QtyEnd_B, ROUND(f_convert_qty(S.PRODUCT, Qty_End-FLOOR(Qty_End), P.UOM_K ), 0) QtyEnd_K,
S.LOC_CODE
FROM V_STOCK_DETAIL S
JOIN PRODUCTS P ON P.PRODUCT = S.PRODUCT
WHERE S.Product = :pProduct AND S.WH_CODE = :pWhCode AND S.LOC_CODE = :pLocCode
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.04 0.12 0 10 4 0
Fetch 43 0.05 2.02 0 73 0 83
total 45 0.10 2.15 0 83 4 83
Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 164
Rows Row Source Operation
83 PX COORDINATOR (cr=83 pr=0 pw=0 time=2086576 us)
0 PX SEND QC (RANDOM) :TQ10003 (cr=0 pr=0 pw=0 time=0 us)
0 HASH GROUP BY (cr=0 pr=0 pw=0 time=0 us)
0 PX RECEIVE (cr=0 pr=0 pw=0 time=0 us)
0 PX SEND HASH :TQ10002 (cr=0 pr=0 pw=0 time=0 us)
0 HASH GROUP BY (cr=0 pr=0 pw=0 time=0 us)
0 NESTED LOOPS OUTER (cr=0 pr=0 pw=0 time=0 us)
0 MERGE JOIN CARTESIAN (cr=0 pr=0 pw=0 time=0 us)
0 SORT JOIN (cr=0 pr=0 pw=0 time=0 us)
0 NESTED LOOPS (cr=0 pr=0 pw=0 time=0 us)
0 BUFFER SORT (cr=0 pr=0 pw=0 time=0 us)
0 PX RECEIVE (cr=0 pr=0 pw=0 time=0 us)
0 PX SEND BROADCAST :TQ10000 (cr=0 pr=0 pw=0 time=0 us)
1 INDEX RANGE SCAN PRODUCTS_IDX2 (cr=2 pr=0 pw=0 time=62 us)(object id 135097)
0 PX BLOCK ITERATOR (cr=0 pr=0 pw=0 time=0 us)
0 MAT_VIEW ACCESS FULL MV_CONVERT_UOM (cr=0 pr=0 pw=0 time=0 us)
0 BUFFER SORT (cr=0 pr=0 pw=0 time=0 us)
0 BUFFER SORT (cr=0 pr=0 pw=0 time=0 us)
0 PX RECEIVE (cr=0 pr=0 pw=0 time=0 us)
0 PX SEND BROADCAST :TQ10001 (cr=0 pr=0 pw=0 time=0 us)
83 TABLE ACCESS BY INDEX ROWID STOCK (cr=78 pr=0 pw=0 time=1635 us)
83 INDEX RANGE SCAN STOCK_PK (cr=4 pr=0 pw=0 time=458 us)(object id 135252)
0 TABLE ACCESS BY INDEX ROWID MV_TRANS_STOCK (cr=0 pr=0 pw=0 time=0 us)
0 INDEX RANGE SCAN MV_TRANS_STOCK_IDX1 (cr=0 pr=0 pw=0 time=0 us)(object id 143537)
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
PX Deq: Join ACK 17 0.00 0.00
PX qref latch 2 0.00 0.00
PX Deq Credit: send blkd 72 1.95 2.00
PX Deq: Parse Reply 26 0.01 0.01
SQL*Net message to client 43 0.00 0.00
PX Deq: Execute Reply 19 0.00 0.01
SQL*Net message from client 43 0.00 0.04
PX Deq: Signal ACK 12 0.00 0.00
enq: PS - contention 1 0.00 0.00
********************************************************************************The DBMS_XPLAN.DISPLAY_CURSOR output:
SQL> select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'))
PLAN_TABLE_OUTPUT
SQL_ID 402b8st7vt6ku, child number 2
SELECT /*+ gather_plan_statistics */ S.Product, S.WH_CODE, S.RACK, S.BATCH, S.EXP_DATE, FLOOR(Qty_Beg) QtyBeg_B,
ROUND(f_convert_qty(S.PRODUCT, Qty_Beg-FLOOR(Qty_Beg), P.UOM_K ), 0) QtyBeg_K, FLOOR(Qty_In) QtyIn_B, ROUND(f_convert_qty(S.P
RODUCT,
Qty_In-FLOOR(Qty_In), P.UOM_K), 0) QtyIn_K, FLOOR(Qty_Out) QtyOut_B, ROUND(f_convert_qty(S.PRODUCT, Qty_Out-FLOOR(Qty_Out), P
.UOM_K ),
0) QtyOut_K, FLOOR(Qty_Adj) QtyAdj_B, ROUND(f_convert_qty(S.PRODUCT, Qty_Adj-FLOOR(Qty_Adj), P.UOM_K ), 0) QtyAdj_K,
FLOOR(Qty_End) QtyEnd_B, ROUND(f_convert_qty(S.PRODUCT, Qty_End-FLOOR(Qty_End), P.UOM_K ), 0) QtyEnd_K, S.LOC_CODE FROM
V_STOCK_DETAIL S JOIN PRODUCTS P ON P.PRODUCT = S.PRODUCT WHERE S.Product = :pProduct AND S.WH_CODE = :pWhCode AND S.LOC
_CODE =
:pLocCode
Plan hash value: 3252950027
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | OMem |
1Mem | Used-Mem |
| 1 | PX COORDINATOR | | 1 | | 83 |00:00:02.25 | 83 | |
| |
| 2 | PX SEND QC (RANDOM) | :TQ10003 | 0 | 21 | 0 |00:00:00.01 | 0 | |
| |
| 3 | HASH GROUP BY | | 0 | 21 | 0 |00:00:00.01 | 0 | |
| |
| 4 | PX RECEIVE | | 0 | 21 | 0 |00:00:00.01 | 0 | |
| |
| 5 | PX SEND HASH | :TQ10002 | 0 | 21 | 0 |00:00:00.01 | 0 | |
| |
| 6 | HASH GROUP BY | | 0 | 21 | 0 |00:00:00.01 | 0 | |
| |
| 7 | NESTED LOOPS OUTER | | 0 | 21 | 0 |00:00:00.01 | 0 | |
| |
| 8 | MERGE JOIN CARTESIAN | | 0 | 21 | 0 |00:00:00.01 | 0 | |
| |
| 9 | SORT JOIN | | 0 | | 0 |00:00:00.01 | 0 | 73728 |
73728 | |
| 10 | NESTED LOOPS | | 0 | 1 | 0 |00:00:00.01 | 0 | |
| |
| 11 | BUFFER SORT | | 0 | | 0 |00:00:00.01 | 0 | 73728 |
73728 | |
| 12 | PX RECEIVE | | 0 | | 0 |00:00:00.01 | 0 | |
| |
| 13 | PX SEND BROADCAST | :TQ10000 | 0 | | 0 |00:00:00.01 | 0 | |
| |
|* 14 | INDEX RANGE SCAN | PRODUCTS_IDX2 | 1 | 1 | 1 |00:00:00.01 | 2 | |
| |
| 15 | PX BLOCK ITERATOR | | 0 | 1 | 0 |00:00:00.01 | 0 | |
| |
|* 16 | MAT_VIEW ACCESS FULL | MV_CONVERT_UOM | 0 | 1 | 0 |00:00:00.01 | 0 | |
| |
| 17 | BUFFER SORT | | 0 | 21 | 0 |00:00:00.01 | 0 | 73728 |
73728 | |
| 18 | BUFFER SORT | | 0 | | 0 |00:00:00.01 | 0 | 73728 |
73728 | |
| 19 | PX RECEIVE | | 0 | 21 | 0 |00:00:00.01 | 0 | |
| |
| 20 | PX SEND BROADCAST | :TQ10001 | 0 | 21 | 0 |00:00:00.01 | 0 | |
| |
|* 21 | TABLE ACCESS BY INDEX ROWID| STOCK | 1 | 21 | 83 |00:00:00.01 | 78 | |
| |
|* 22 | INDEX RANGE SCAN | STOCK_PK | 1 | 91 | 83 |00:00:00.01 | 4 | |
| |
|* 23 | TABLE ACCESS BY INDEX ROWID | MV_TRANS_STOCK | 0 | 1 | 0 |00:00:00.01 | 0 | |
| |
|* 24 | INDEX RANGE SCAN | MV_TRANS_STOCK_IDX1 | 0 | 1 | 0 |00:00:00.01 | 0 | |
| |
Predicate Information (identified by operation id):
14 - access("P"."PRODUCT"=:PPRODUCT)
16 - access(:Z>=:Z AND :Z<=:Z)
filter("CON"."PRODUCT"=:PPRODUCT)
21 - filter("STOCK"."LOC_CODE"=:PLOCCODE)
22 - access("STOCK"."PRODUCT"=:PPRODUCT AND "STOCK"."WH_CODE"=:PWHCODE)
23 - filter("STS"='N')
24 - access("PRODUCT"=:PPRODUCT AND "WH_CODE"=:PWHCODE AND "LOC_CODE"=:PLOCCODE AND "RACK"="STOCK"."RACK" AND "BATCH"="STOCK"."B
ATCH" AND
"EXP_DATE"="STOCK"."EXP_DATE")
53 rows selected.
Elapsed: 00:00:00:12I'm looking forward for suggestions how to improve the performance of this statement.
Thank you very much,
xtantoxtanto wrote:
Hi sir,
How to prevent the query from doing parallel query ?
Because as you see actually I am not issuing any Parallel hints in the query.
Thank you,
xtantoKristanto,
there are a couple of points to consider:
1. Your SQL*Plus version seems to be outdated. Please use a SQL*Plus version that corresponds to your database version. E.g. the AUTOTRACE output is odd.
2. I would suggest to repeat your exercise using serial execution (the plan, the autotrace, the tracing). You can disable parallel queries by issuing this in your session:
ALTER SESSION DISABLE PARALLEL QUERY;
This way the output of the tools is much more meaningful, however you might get a different execution plan, therefore the results might not be representative for your parallel execution.
3. The function calls might pose a problem. If they are, one possible damage limitation has been provided by hoek. Even better would be then to replace the PL/SQL function with equivalent plain SQL. However since you say that it generates not too many rows it might not harm here too much. You can check the impact of the functions by running a similar query but omitting the function calls.
4. The parallel execution plan contains a MERGE JOIN CARTESIAN operation which could be an issue if the estimates of the optimizer are incorrect. If the serial execution still uses this operation the TKPROF and DBMS_XPLAN.DISPLAY_CURSOR output will reveal whether this is a problem or not.
5. The execution of the statement seems to take on 2-3 seconds in your tests. Is this in the right ballpark? If yes, why should this statement then be problematic? How often does it get executed?
6. The statement uses bind variables, so you might have executions that use different execution plans depending on the bind values passed when the statement got optimized. You can use DBMS_XPLAN.DISPLAY_CURSOR using NULL as "child_number" parameter or DBMS_XPLAN.DISPLAY_AWR (if you have a AWR license) to check if you have multiple execution plans for the statement. Please note that older versions might have already been aged out of the shared pool, so the AWR repository might be a more reliable source (but only if the statement has been sampled).
7. You have disabled cost based transformations: "_optimizer_cost_based_transformation" = OFF. Why?
Regards,
Randolf
Oracle related stuff blog:
http://oracle-randolf.blogspot.com/
SQLTools++ for Oracle (Open source Oracle GUI for Windows):
http://www.sqltools-plusplus.org:7676/
http://sourceforge.net/projects/sqlt-pp/ -
Why this SQL statement is wrong?
When I trying update my database using following statement:
String query = "UPDATE Flights" +
"SET AircraftType ='" + inputPanel.type.getText() +
"', EnterPoint = '" + inputPanel.point.getText() +
"',GroundSpeed = " + Integer.parseInt(inputPanel.speed.getText()) +
",Altitude =" + Integer.parseInt(inputPanel.altitude.getText()) +
"WHERE FlightNumber =" + inputPanel.flight.getText();
//where speep and altitude are ints
it always say:Syntax error in UPDATE statement.
I cannot understand why because this is a standard SQL statement I think.
Please help me!
Thanks in advancethis part is causing you problems:
"WHERE FlightNumber =" + inputPanel.flight.getText();
if FlightNumber is an integer then you should convert the text to an int:
"WHERE FlightNumber =" + Integer.parseInt(inputPanel.flight.getText());
or if it is a String then:
"WHERE FlightNumber ='" + inputPanel.flight.getText()+ "'";
Jamie -
Is this SQL Statement possible?
String PreStatement="Insert into Vehicle (Threshold) VALUES (?) AND where ID='" + id + "'" ;
Prepared=connection.prepareStatement(PreStatement);
Prepared.setDouble(1, percent);
Prepared.executeUpdate();I'm trying to add a percentage value to that one column where the ID in the database is equal to my id variable. If I do this
PreStatement="Insert into Vehicle (Threshold) VALUES (?); AND where ID='" + id + "'" ;
Prepared=connection.prepareStatement(PreStatement);
Prepared.setDouble(1, percent);
Prepared.executeUpdate();It does add the percentage value to the database, but not in the right row. Does anyone know what I'm doing wrong?Ok...when I add a new entry, I default the threshold to 0.0.....and I can see that in my database....so I do what you said.....
statement.executeQuery("Update Vehicle SET VALUE = '" + percent + " where ID= '" + id + "'" );run my program...and I get the following
com.mckoi.database.jdbc.MSQLException: Encountered "1" at line 1, column 45.
Was expecting one of:
<EOF>
"where" ...
"limit" ...
"=" ...
"==" ...
">" ...
"<" ...
">=" ...
"<=" ...
<NOTEQ> ...
"is" ...
"like" ...
"not" ...
"and" ...
"or" ...
"+" ...
"||" ...
"regex" ...
<REGEX_LITERAL> ...
"in" ...
"between" ...
at com.mckoi.database.jdbcserver.AbstractJDBCDatabaseInterface.handleExecuteThrowable(AbstractJDBCDatabaseInterface.java:265)
at com.mckoi.database.jdbcserver.AbstractJDBCDatabaseInterface.execQuery(AbstractJDBCDatabaseInterface.java:479)
at com.mckoi.database.jdbcserver.JDBCDatabaseInterface.execQuery(JDBCDatabaseInterface.java:251)
at com.mckoi.database.jdbc.MConnection.executeQuery(MConnection.java:453)
at com.mckoi.database.jdbc.MConnection.executeQueries(MConnection.java:436)
at com.mckoi.database.jdbc.MStatement.executeQueries(MStatement.java:193)
at com.mckoi.database.jdbc.MStatement.executeQuery(MStatement.java:167)
at com.mckoi.database.jdbc.MStatement.executeQuery(MStatement.java:222)
at gov.sandia.emc.ema.bdrs.Database.computePercentage(Database.java:981)
at gov.sandia.emc.ema.bdrs.Database.search(Database.java:936)
at gov.sandia.emc.ema.bdrs.CombinedInterface$5.actionPerformed(CombinedInterface.java:610)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
CAUSE: com.mckoi.database.sql.ParseException: Encountered "1" at line 1, column 45.
Was expecting one of:
<EOF>
"where" ...
"limit" ...
"=" ...
"==" ...
">" ...
"<" ...
">=" ...
"<=" ...
<NOTEQ> ...
"is" ...
"like" ...
"not" ...
"and" ...
"or" ...
"+" ...
"||" ...
"regex" ...
<REGEX_LITERAL> ...
"in" ...
"between" ...
at com.mckoi.database.sql.SQL.generateParseException(SQL.java:6070)
at com.mckoi.database.sql.SQL.jj_consume_token(SQL.java:5939)
at com.mckoi.database.sql.SQL.Statement(SQL.java:224)
at com.mckoi.database.interpret.SQLQueryExecutor.execute(SQLQueryExecutor.java:94)
at com.mckoi.database.jdbcserver.AbstractJDBCDatabaseInterface.execQuery(AbstractJDBCDatabaseInterface.java:461)
at com.mckoi.database.jdbcserver.JDBCDatabaseInterface.execQuery(JDBCDatabaseInterface.java:251)
at com.mckoi.database.jdbc.MConnection.executeQuery(MConnection.java:453)
at com.mckoi.database.jdbc.MConnection.executeQueries(MConnection.java:436)
at com.mckoi.database.jdbc.MStatement.executeQueries(MStatement.java:193)
at com.mckoi.database.jdbc.MStatement.executeQuery(MStatement.java:167)
at com.mckoi.database.jdbc.MStatement.executeQuery(MStatement.java:222)
at gov.sandia.emc.ema.bdrs.Database.computePercentage(Database.java:981)
at gov.sandia.emc.ema.bdrs.Database.search(Database.java:936)
at gov.sandia.emc.ema.bdrs.CombinedInterface$5.actionPerformed(CombinedInterface.java:610)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)I know there's something wrong with my SQL Statement, what do I need to add? -
How to rewrite this SQL statement
I have tableA and tableB as below. The following query gets Max(Process_date) during month of january from the
two tables tableA and tableB with different criteria
I want to expand the following query to return Max(Process_date) for BOTH current month(txn_date=entire jan2013) and prior month(txn_date= entire dec2013)
Is it possible? If so how can I modify this query to return MAX(PROCESS_DATE) for current and prior months in single query.
SELECT MAX(process_date) AS curr_month_amount FROM
SELECT process_date AS process_date,
amount1 AS amount
FROM tableA
WHERE id = 1 AND
process_date = (SELECT MAX(process_date)
FROM tableA
WHERE id = 1 and txn_date between TO_DATE('01-JAN-2013','dd-mon-yyyy') and TO_DATE('31-JAN-2013', 'dd-mon-yyyy')
AND amount1 = 0
UNION
SELECT MAX(process_date) AS process_date,
0 AS amount
FROM tableB
WHERE id = 1 AND txn_code = 'B' and txn_date between TO_DATE('01-JAN-2013','dd-mon-yyyy') and TO_DATE('31-JAN-2013', 'dd-mon-yyyy')
future state of the sql
Single sql statement a) should look at txn_date between 1/1/2013 - 1/31/2013 to return max(process_date) for current month
b) should look at txn_date between 12/1/2012 - 12/31/2012 to return max(process_date) for prior month
NOTE:-( i want to pass current_month_end date 1/31/2013 to this sql so it will calculate for current and prior months)
expected output
***************: For id=1 in the modified query,
prior month max(process_date) should be 1/19/2013
current month max(process_date) should be 1/15/2012
For id=5 in the modified query
prior month max(process_date) should be NULL
current month max(process_date) should be 1/16/2013
SQL to create TableA and TableB with insert statements( txn_date column not included)
CREATE table tableA
id NUMBER,
process_date DATE,
amount1 NUMBER,
txn_code VARCHAR2(1),
txn_date DATE
Create table tableB
( id NUMBER,
process_date DATE,
amount2 NUMBER,
txn_code VARCHAR2(1),
txn_date DATE
INSERT INTO tableA
( id, process_date, amount1, txn_code, txn_date )
values
( 1, to_date('01/12/2013','mm/dd/yyyy'), 500, 'A', to_date('01/15/2013','mm/dd/yyyy') );
INSERT INTO tableA
( id, process_date, amount1, txn_code, txn_date )
values
( 1, to_date('01/13/2013','mm/dd/yyyy'), 100, 'A', to_date('01/14/2013','mm/dd/yyyy'));
INSERT INTO tableA
( id, process_date, amount1, txn_code , txn_date)
values
( 1, to_date('01/14/2013','mm/dd/yyyy'), 0, 'A', to_date('01/15/2013','mm/dd/yyyy'));
INSERT INTO tableB
( id, process_date, amount2, txn_code, txn_date)
values
( 1, to_date('01/15/2013','mm/dd/yyyy'), 0, 'B', to_date('01/31/2013','mm/dd/yyyy'));
INSERT INTO tableA
( id, process_date, amount1, txn_code, txn_date )
values
( 1, to_date('12/01/2012','mm/dd/yyyy'), 500, 'A', to_date('12/31/2012','mm/dd/yyyy') );
INSERT INTO tableA
( id, process_date, amount1, txn_code , txn_date)
values
( 1, to_date('12/23/2012','mm/dd/yyyy'), 100, 'A', to_date('12/14/2012','mm/dd/yyyy'));
INSERT INTO tableA
( id, process_date, amount1, txn_code, txn_date )
values
( 1, to_date('12/19/2012','mm/dd/yyyy'), 0, 'A', to_date('12/15/2012','mm/dd/yyyy'));
INSERT INTO tableB
( id, process_date, amount2, txn_code, txn_date)
values
( 1, to_date('12/15/2012','mm/dd/yyyy'), 0, 'C', to_date('12/31/2012','mm/dd/yyyy'));
INSERT INTO tableA
( id, process_date, amount1, txn_code, txn_date )
values
( 5, to_date('01/11/2013','mm/dd/yyyy'), 500, 'A', to_date('01/09/2013','mm/dd/yyyy') );
INSERT INTO tableA
( id, process_date, amount1, txn_code, txn_date )
values
(5, to_date('01/12/2013','mm/dd/yyyy'), 0, 'A', to_date('01/19/2013','mm/dd/yyyy'))
INSERT INTO tableA
( id, process_date, amount1, txn_code, txn_date )
values
( 5, to_date('01/15/2013','mm/dd/yyyy'), 10 , 'A', to_date('01/09/2013','mm/dd/yyyy'));
INSERT INTO tableB
( id, process_date, amount2, txn_code, txn_date)
values
( 5, to_date('01/16/2013','mm/dd/yyyy'), 1, 'B', to_date('01/09/2013','mm/dd/yyyy'));
INSERT INTO tableA
( id, process_date, amount1, txn_code, txn_date )
values
( 5, to_date('12/11/2012','mm/dd/yyyy'), 500, 'A', to_date('12/09/2012','mm/dd/yyyy') );
INSERT INTO tableA
( id, process_date, amount1, txn_code, txn_date )
values
(5, to_date('12/12/2012','mm/dd/yyyy'), 0, 'A', to_date('12/19/2012','mm/dd/yyyy'))
INSERT INTO tableA
( id, process_date, amount1, txn_code, txn_date )
values
( 5, to_date('12/15/2012','mm/dd/yyyy'), 10 , 'A', to_date('12/09/2012','mm/dd/yyyy'));
INSERT INTO tableB
( id, process_date, amount2, txn_code, txn_date)
values
( 5, to_date('12/16/2012','mm/dd/yyyy'), 1, 'C', to_date('12/09/2012','mm/dd/yyyy'));
commit;Maybe
SELECT id,
month,
MAX(process_date) AS curr_month_amount
FROM (SELECT id,
process_date AS process_date,
amount1 AS amount,
TO_CHAR(txn_date,'mm') AS month
FROM tableA
WHERE (process_date,TO_CHAR(txn_date,'mm')) in
(SELECT MAX(process_date),TO_CHAR(txn_date,'mm')
FROM tableA
WHERE txn_date between ADD_MONTHS(TO_DATE('01-JAN-2013','dd-mon-yyyy'),-1)
AND TO_DATE('31-JAN-2013','dd-mon-yyyy')
AND amount1 = 0
GROUP BY TO_CHAR(txn_date,'mm')
AND amount1 = 0
UNION
SELECT id,
MAX(process_date) AS process_date,
0 AS amount,
TO_CHAR(txn_date,'mm') AS month
FROM tableB
WHERE txn_code = 'B'
AND txn_date between ADD_MONTHS(TO_DATE('01-JAN-2013','dd-mon-yyyy'),-1)
AND TO_DATE('31-JAN-2013','dd-mon-yyyy')
GROUP BY id,TO_CHAR(txn_date,'mm')
GROUP BY id,month
ID
MONTH
CURR_MONTH_AMOUNT
5
01
01/16/2013
1
12
12/19/2012
1
01
01/15/2013
Regards
Etbin -
What this SQL statement is doing?
Hi ABAPers,
Can somebody help me in understanding what the below SQL statement is doing..
SELECT * FROM /BIC/ADSO00 AS tb1
INNER JOIN /BIC/PMASTER as tbl2
ON tbl1~field1 = tbl2~field1
AND tbl12~field2 = tbl2~field2
INTO CORRESPONDING FIELDS OF TABLE gt_itab
FOR ALL ENTRIES IN SOURCE_PACKAGE
WHERE tbl1~field3 = SOURCE_PACKAGE-field3
AND tbl1~field4 BETWEEN lv_minper AND lv_maxper
AND tbl1~field5 = SOURCE_PACKAGE-field5
AND tbl1~field6 = '0100'
AND tbl2~OBJVERS = 'A'.
thanks in advance !!
Bharath SHi Bharath,
tb1 is your /BIC/ADSO00 table
tbl2 is your /BIC/PMASTER second table.
It is selecting all contents from /BIC/ADSO00 table available and is selecting respective contents from /BIC/PMASTER second table using field 1 and field2 as key(unique in two tables) and moving into an internal table(structures used in program for holding values similar to DB and can hold values till program execution ends) and selection is valid only for entries in source_package another internal table.
Conditions for the selections are
tbl1~field3 = SOURCE_PACKAGE-field3
AND tbl1~field4 BETWEEN lv_minper AND lv_maxper
AND tbl1~field5 = SOURCE_PACKAGE-field5
AND tbl1~field6 = '0100'
AND tbl2~OBJVERS = 'A'.
Hope it will be helpful.
Regards,
Kannan -
Can anybody see what is wrong with this SQL statement?
Hey guys, just a quick question. Can anybody tell me what is wrong with this line of SQL? I keep getting a syntax error message. I've been trying for ages and I can't see any problem at all!"
{code}prepStat = connection.prepareStatement("INSERT INTO WeatherHistory (Date, Location, Overview, Temperature, WindDirection, WindSpeed, Pressure) VALUES ('"+date+"','"+location+"','"+temp+"','"+windDir+"','"+windSpd+"','"+pressure+"')");{code}
All the field names and variables definitely exist so I can't see what the problem is!DHD wrote:
Thanks for the replies.
I've matched the correct number of column names and variables, but still no luck.
And how exactly am I misusing Prepared Statements here?As noted above, not according to the code you posted. I didn't just pluck something out of my @ss and throw it out there. There was a reason behind what I said. And, if you mean you changed it, and you still got an exception, then post that exception (completely), and your new code, which is, hopefully, using PreparedStatement, (properly). -
Again the SUM and DISTINCT Problem (With SQL Statement)
Here is the actual SQL,
select
NVL(l.pr_cust_prod,'zzzzz') A,
NVL(l.pr_prod_code,'zzzzz') B,
NVL(l.pr_lot_no,'zzzzz') C,
NVL(l.db_waf_qty,'99999') lotQty,
NVL(t.db_cust_po,'zzzzz') poNum,
NVL(p.oe_po_line,'99999') poLineNum,
i.db_shpmt_no invNum,
from
db_invoice i, db_line_lot l, db_inv_item t, db_prod_line p
where
l.db_shpmt_no like 'U388%'
and i.db_invo_date > sysdate - 500
and l.pr_run_type = 'PR'
and l.db_shpmt_no = i.db_shpmt_no
and l.db_shpmt_no = t.db_shpmt_no
and l.db_item_no = t.db_item_no
and p.db_ref_numa1 = t.db_tsmc_po
and l.db_shpmt_no not in
(select distinct db_shpmt_no from ftp_cust_ship
where DB_MAST_ACCT='U388' and sys_name='SENT')
and l.pr_prod_code like '%Z_'
group by l.pr_lot_no, l.db_line_numb, l.pr_cust_prod, l.pr_prod_code,
t.db_cust_po, i.db_delv_cod2, l.db_lot_desc, i.db_shpmt_no,
p.oe_po_line
order by lotNum
the problem is that if I dont use SELECT DISTINCT at beginning
the result is like
F , LOTQTY , PONUM , POLINENUM
DP8568.00,25 ,BVI10080 ,1
DP8568.00,25 ,BVI10080 ,2
DP8568.00,25 ,BVI10080 ,1
DP8568.00,25 ,BVI10080 ,1
DP8568.00,25 ,BVI10080 ,2
DP8568.00,25 ,BVI10080 ,1
DP8568.00,25 ,BVI10080 ,1
DP8568.00,25 ,BVI10080 ,2 //pleae note the QTY and POLINENUM
Then if Add SELECT DISTINCT, the result will be
F ,LOTQTY , PONUM , POLINENUM
DP8568.00,25 ,BVI10080,1
DP8568.00,25 ,BVI10080,2
but I change the NVL(l.db_waf_qty,'99999') lotQty, statement to NVL((SUM( l.db_waf_qty)),'99999') lotQty,
this will give me the result
F ,LOTQTY, PONUM , POLINENUM
DP8568.00,75 , BVI10080 ,2
DP8568.00,125 , BVI10080 ,1
Which I excepted the result as
F ,LOTQTY, PONUM , POLINENUM
DP8568.00,25 , BVI10080 ,2
DP8568.00,25 , BVI10080 ,1
And Advise??
Thanks a lot
Jonbased on the data set you given
SQL>
SQL> With t As
2 (
3 Select 'DP8568' c1 ,0.25 c2,'BVI10080' c3,1 c4 From dual Union All
4 Select 'DP8568' ,0.25 ,'BVI10080' ,2 From dual Union All
5 Select 'DP8568' ,0.25 ,'BVI10080' ,1 From dual Union All
6 Select 'DP8568' ,0.25 ,'BVI10080' ,1 From dual Union All
7 Select 'DP8568' ,0.25 ,'BVI10080' ,2 From dual Union All
8 Select 'DP8568' ,0.25 ,'BVI10080' ,1 From dual Union All
9 Select 'DP8568' ,0.25 ,'BVI10080' ,1 From dual Union All
10 Select 'DP8568' ,0.25 ,'BVI10080' ,2 From dual
11 )
12 Select c1,
13 sum(c2) lot_qty,
14 c3,
15 c4
16 From
17 (
18 Select c1, --Sum(c2) over (Partition By c1,c4 Order By c4)
19 row_number() over (Partition By c1,c4 Order By c4) rn,
20 c2,
21 c3,
22 c4
23 From t
24 ) Where rn = 1
25 Group By c1, c3, c4;
C1 LOT_QTY C3 C4
DP8568 0.25 BVI10080 2
DP8568 0.25 BVI10080 1
SQL> -
Wats wrong wit this sql statement?
I know the format is right but i'm getting this error message:
select acnum,givename,famname,instname from
academic ac,department dep where
ac.deptnum=dep.deptnum AND acnum in (
select acnum
from interest
group by acnum
having count(*) >=1
order by count(*) desc
ERROR at line 9:
ORA-00907: missing right parenthesisI seem to recall that an ORDER BY in a subquery like this wasn't allowed at one point.It still is not allowed.
You can inline the order by query though, but as you said, it actually doesn't really make sense:
SQL> select * from v$version where rownum=1
BANNER
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
1 row selected.
SQL> select * from dual where dummy in (select * from dual order by 1)
Error at line 1
ORA-00907: missing right parenthesis
SQL> select * from dual where dummy in (select * from (select * from dual order by 1))
D
X
1 row selected. -
How to Tuning this sql statement
hi my friend:
SELECT ie.ImportEntityAutoID,
ie.ImportRawDataAutoID,
ie.Amount,
s.NAME AS StatusName,
ir.SourceLocation AS SourceLocation,
ie.ErrorMessage AS ErrorMessage,
famis.PROJECT_CODE,
famis.PROJECT_TITLE
FROM dbo.ImportEntity ie
INNER JOIN tblBatch B ON ie.BatchGuid = B.BatchGuid
INNER JOIN dbo.ImportRawData ir ON ie.ImportRawDataAutoID
= ir.ImportRawDataAutoID
INNER JOIN dbo.ImportRawData_Famis famis ON ie.ImportRawDataAutoID
= famis.ImportRawDataAutoID
LEFT OUTER JOIN dbo.Status s ON ( ie.StatusID
= s.StatusID
AND s.StatusTypeID = 35 --Import Entity Status
WHERE ie.EntityType <> 'Unknown'
AND ie.[StatusID] IN ( 3, 5, 6 ) ----3: Pending; 5: Failed; 6: Cancel
AND ( 0 = 0
OR ie.StatusID
= 0
AND ie.Amount >= -99999999999999.9999 AND ie.Amount
<= 99999999999999.9999 ;
the ImportEntity TABLE has 5 milion record AND the value of entitytype COLUMN equal 'Unknown' IS more THEN 3 milion,
statusid equal (3,5,6) Probably 2 milion. I had following index:
table ImportEntity:
clustered_index:ImportRawDataAutoID
non-CLUSTERED INDEX IX_batchguid ON BatchGuid
non-CLUSTERED INDEX IX_ImportRawDataAutoID ON ImportRawDataAutoID
non-CLUSTERED INDEX IX_StatusID ON StatusID
non-CLUSTERED INDEX IX_EntityType ON EntityType
AND tblBatch has ONLY two records
ImportRawData_Famis has 5 milion recors AND ImportRawDataAutoID IS its clustered index COLUMN
ImportRawData has 5 milion recors AND ImportRawDataAutoID IS its clustered index COLUMN
the query require 180 SECOND.how can i tuning this statment.hi
Visakh16
I do just as you say. but I understand the execution plan and can you give me some advice about procedure
tuning and look the execution plan、clean execution plan and so on.
thank you very much!
ming
Maybe you are looking for
-
I cannot get the latest drivers for the Audigy Gamer (E400) to work correctly with my card no matter what I do. I'm running off a clean WinXP install with SP2. I installed AUD_EAX4DRV_0303.exe, then updated my AudioHQ, then installed AUD_WEBUP_PCWDRV
-
HT1711 Have bought music, but, can't play it, my screen goes black
Have bought music o iTunes, but, it won't play, screen goes black when I click on play.. Help?
-
File content conversion simple scenario
Hi friends, I am trying FCC for first time. I had created one simple file scenario without using FCC to which I had to provide input in XML format. like this: <?xml version="1.0" encoding="UTF-8"?> <ns0:mt_sender xmlns:ns0="http:/soni.com"> <ponum
-
How can i get cfselect to repopulate after database update?
I have a form with a cfselect generating a dropdown of Program Directors. I have a link near the dropdown to a page for adding Program Directors. I want to be able to click the link, add a Program Director, then have the cfselect-generated dropdown i
-
VERY IMP --- OBJECTS MISSING FROM APPLICATION
APPLICATION NAME - BPSIDEV_APP SCHEMA NAME - BPSIDEV we have a application BPSIDEV_APP with more than 100 modules (Forms, Reports, LOVs etc). somebody accidentaly tried to RENAME the application while others were still working on the modules. now the