Can I write this query in another way (prefferably in optimized manner)
My database version._
[oracle@localhost ~]$ uname -a
Linux localhost.localdomain 2.6.18-194.17.1.0.1.el5 #1 SMP Wed Sep 29 15:40:03 EDT 2010 i686 i686 i386 GNU/Linux
[oracle@localhost ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.2.0 Production on Fri Aug 12 04:44:21 2011
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> SELECT * FROM v$version;
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - Production
PL/SQL Release 11.2.0.2.0 - Production
CORE 11.2.0.2.0 Production
TNS for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production
SQL>
Introduction to data and logic._
I have on table called inv_leg_dummy. The main columns to consider is arrival_airport and departure_airport. Say a flight starts from kolkata (KOL) -> Goes to Dellhi (DEL) -> Goes to Hongkong (HKG) -> Goes to Tiwan (TPE). So in total KOL -> DEL -> HKG -> TPE
Data will be like:
Arrival Airport Departure Airport
HKG TPE
KOL DEL
DEL HKGPlease note that the order is not as expected, that means the flight starts from kolkata can not be determined straight way from the arrangment or any kind of flag.
The main logic is, I first take Arrival Airport HKG and see if any Departure Airport exists as HKG, then I take the next KOL and see if any Departure Airport exists as KOL. You can notice KOL is only present as arrival airport, So, This is the first leg of the flight journey. By the same logic, I can determine next leg, that is DEL (because flight goes from KOL to DEL)...
I need output like :
ARRIVAL_AIRPORT DEPARTURE_AIRPORT SEQ
HKG TPE 1
DEL HKG 2
KOL DEL 3
KOL 4So, The starting point KOL has heighest sequence (arrival is null), then KOL to DEL, DEL to HKG and finally HKG to TPE (sequence 1). The sequence may look like reverse order.
Create Table and Insert Scripts._
CREATE TABLE inv_leg_dummy
carrier VARCHAR2(3) not null,
flt_num VARCHAR2(4) not null,
flt_num_suffix VARCHAR2(1) default ' ' not null,
flt_date DATE not null,
arrival_airport VARCHAR2(5),
departure_airport VARCHAR2(5) not null
alter table inv_leg_dummy
add constraint XPKINV_LEG primary key (carrier,flt_num,flt_num_suffix,flt_date,departure_airport);
TRUNCATE table inv_leg_dummy;
INSERT INTO inv_leg_dummy VALUES ('KA',1,' ',to_date('05/23/2011','mm/dd/rrrr'),'HKG','TPE');
INSERT INTO inv_leg_dummy VALUES ('KA',1,' ',to_date('05/23/2011','mm/dd/rrrr'),'KOL','DEL');
INSERT INTO inv_leg_dummy VALUES ('KA',1,' ',to_date('05/23/2011','mm/dd/rrrr'),'DEL','HKG');
INSERT INTO inv_leg_dummy VALUES ('CX',1,' ',to_date('05/22/2011','mm/dd/rrrr'),'HKG','BNE');
INSERT INTO inv_leg_dummy VALUES ('CX',1,' ',to_date('05/22/2011','mm/dd/rrrr'),'BNE','CNS');
Now, it time to show you, What I have done!_
SQL> ed
Wrote file afiedt.buf
1 SELECT Carrier,
2 Flt_Num,
3 Flt_Date,
4 Flt_num_Suffix,
5 arrival_airport,
6 departure_airport,
7 RANK() OVER(partition by Carrier, Flt_Num, Flt_Date, Flt_num_Suffix ORDER BY Carrier, Flt_Num, Flt_Date, Flt_num_Suffix, SEQ ASC NULLS LAST) SEQ,
8 /* Fetching Maximum leg Seq No excluding Dummy Leg*/
9 max(seq) over(partition by carrier, flt_num, flt_date, flt_num_suffix order by carrier, flt_num, flt_date, flt_num_suffix) max_seq
10 FROM (SELECT k.Carrier,
11 k.Flt_Num,
12 k.Flt_Date,
13 k.Flt_num_Suffix,
14 k.departure_airport,
15 k.arrival_airport,
16 level seq
17 FROM (SELECT
18 l.Carrier,
19 l.Flt_Num,
20 l.Flt_Date,
21 l.Flt_num_Suffix,
22 l.departure_airport,
23 l.arrival_airport
24 FROM inv_leg_dummy l) k
25 START WITH k.departure_airport = case when
26 (select count(*)
27 FROM inv_leg_dummy ifl
28 WHERE ifl.arrival_airport = k.departure_airport
29 AND ifl.flt_num = k.flt_num
30 AND ifl.carrier = k.carrier
31 AND ifl.flt_num_suffix = k.Flt_num_Suffix) = 0 then k.departure_airport end
32 CONNECT BY prior k.arrival_airport = k.departure_airport
33 AND prior k.carrier = k.carrier
34 AND prior k.flt_num = k.flt_num
35 AND prior TRUNC(k.flt_date) =
36 TRUNC(k.flt_date)
37 UNION ALL
38 /* Fetching Dummy Last Leg Information for Leg_Seq No*/
39 SELECT ofl.Carrier,
40 ofl.Flt_Num,
41 ofl.Flt_Date,
42 ofl.Flt_num_Suffix,
43 ofl.arrival_airport as departure_airport,
44 NULL arrival_airport,
45 NULL seq
46 FROM inv_leg_dummy ofl
47 where NOT EXISTS (SELECT 1
48 FROM inv_leg_dummy ifl
49 WHERE ofl.arrival_airport = ifl.departure_airport
50 AND ifl.flt_num = ofl.flt_num
51 AND ifl.carrier = ofl.carrier
52 AND ifl.flt_num_suffix =ofl.Flt_num_Suffix))
53* ORDER BY 1, 2, 3, 4,7
SQL> /
CAR FLT_ FLT_DATE F ARRIV DEPAR SEQ MAX_SEQ
CX 1 22-MAY-11 BNE CNS 1 2
CX 1 22-MAY-11 HKG BNE 2 2
CX 1 22-MAY-11 HKG 3 2
KA 1 23-MAY-11 HKG TPE 1 3
KA 1 23-MAY-11 DEL HKG 2 3
KA 1 23-MAY-11 KOL DEL 3 3
KA 1 23-MAY-11 KOL 4 3
7 rows selected.
SQL> The code is giving the right output, But I feel, I have done it in a hard way. Is there any easier/optimized approach to solve the problem ?
Hello
I thought I'd run run all 3 methods twice with autotrace to get an overview of the execution plans and basic performance metrics. The results are interesting.
OPs method
SQL> set autot on
SQL> SELECT Carrier,
2 Flt_Num,
3 Flt_Date,
4 Flt_num_Suffix,
5 arrival_airport,
6 departure_airport,
7 RANK() OVER(partition by Carrier, Flt_Num, Flt_Date, Flt_num_Suffix ORDER BY Carrier, Flt_Num,
53 ORDER BY 1, 2, 3, 4,7
54 /
CAR FLT_ FLT_DATE F ARRIV DEPAR SEQ MAX_SEQ
CX 1 22-MAY-11 BNE CNS 1 2
CX 1 22-MAY-11 HKG BNE 2 2
CX 1 22-MAY-11 HKG 3 2
KA 1 23-MAY-11 HKG TPE 1 3
KA 1 23-MAY-11 DEL HKG 2 3
KA 1 23-MAY-11 KOL DEL 3 3
KA 1 23-MAY-11 KOL 4 3
7 rows selected.
Execution Plan
Plan hash value: 3680289985
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | WINDOW SORT | |
| 2 | VIEW | |
| 3 | UNION-ALL | |
|* 4 | CONNECT BY WITH FILTERING | |
|* 5 | FILTER | |
|* 6 | TABLE ACCESS FULL | INV_LEG_DUMMY |
| 7 | SORT AGGREGATE | |
|* 8 | TABLE ACCESS BY INDEX ROWID| INV_LEG_DUMMY |
|* 9 | INDEX RANGE SCAN | XPKINV_LEG |
| 10 | NESTED LOOPS | |
| 11 | CONNECT BY PUMP | |
| 12 | TABLE ACCESS BY INDEX ROWID | INV_LEG_DUMMY |
|* 13 | INDEX RANGE SCAN | XPKINV_LEG |
|* 14 | FILTER | |
| 15 | TABLE ACCESS FULL | INV_LEG_DUMMY |
|* 16 | INDEX RANGE SCAN | XPKINV_LEG |
Predicate Information (identified by operation id):
4 - access("L"."DEPARTURE_AIRPORT"=PRIOR "L"."ARRIVAL_AIRPORT" AND
"L"."CARRIER"=PRIOR "L"."CARRIER" AND "L"."FLT_NUM"=PRIOR "L"."FLT
_NUM"
AND INTERNAL_FUNCTION(PRIOR TRUNC(INTERNAL_FUNCTION("L"."FLT_DATE"
)))=TR
UNC(INTERNAL_FUNCTION("L"."FLT_DATE")))
5 - filter("L"."DEPARTURE_AIRPORT"=CASE WHEN ( (SELECT COUNT(*)
FROM "INV_LEG_DUMMY" "IFL" WHERE "IFL"."FLT_NUM_SUFFIX"=:B1 AND
"IFL"."FLT_NUM"=:B2 AND "IFL"."CARRIER"=:B3 AND
"IFL"."ARRIVAL_AIRPORT"=:B4)=0) THEN "L"."DEPARTURE_AIRPORT" END )
6 - access("L"."CARRIER"=PRIOR "L"."CARRIER")
8 - filter("IFL"."ARRIVAL_AIRPORT"=:B1)
9 - access("IFL"."CARRIER"=:B1 AND "IFL"."FLT_NUM"=:B2 AND
"IFL"."FLT_NUM_SUFFIX"=:B3)
13 - access("L"."CARRIER"=PRIOR "L"."CARRIER" AND "L"."FLT_NUM"=PRIOR
"L"."FLT_NUM" AND "L"."DEPARTURE_AIRPORT"=PRIOR "L"."ARRIVAL_AIRPO
RT")
filter("L"."DEPARTURE_AIRPORT"=PRIOR "L"."ARRIVAL_AIRPORT" AND
INTERNAL_FUNCTION(PRIOR TRUNC(INTERNAL_FUNCTION("L"."FLT_DATE")))=
TRUNC(
INTERNAL_FUNCTION("L"."FLT_DATE")))
14 - filter( NOT EXISTS (SELECT 0 FROM "INV_LEG_DUMMY" "IFL" WHERE
"IFL"."FLT_NUM_SUFFIX"=:B1 AND "IFL"."FLT_NUM"=:B2 AND
"IFL"."CARRIER"=:B3 AND "IFL"."DEPARTURE_AIRPORT"=:B4))
16 - access("IFL"."CARRIER"=:B1 AND "IFL"."FLT_NUM"=:B2 AND
"IFL"."FLT_NUM_SUFFIX"=:B3 AND "IFL"."DEPARTURE_AIRPORT"=:B4)
filter("IFL"."DEPARTURE_AIRPORT"=:B1)
Note
- rule based optimizer used (consider using cbo)
Statistics
1 recursive calls
0 db block gets
33 consistent gets
0 physical reads
0 redo size
877 bytes sent via SQL*Net to client
886 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
6 sorts (memory)
0 sorts (disk)
7 rows processed
SQL> /
CAR FLT_ FLT_DATE F ARRIV DEPAR SEQ MAX_SEQ
CX 1 22-MAY-11 BNE CNS 1 2
CX 1 22-MAY-11 HKG BNE 2 2
CX 1 22-MAY-11 HKG 3 2
KA 1 23-MAY-11 HKG TPE 1 3
KA 1 23-MAY-11 DEL HKG 2 3
KA 1 23-MAY-11 KOL DEL 3 3
KA 1 23-MAY-11 KOL 4 3
7 rows selected.
Execution Plan
Plan hash value: 3680289985
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | WINDOW SORT | |
| 2 | VIEW | |
| 3 | UNION-ALL | |
|* 4 | CONNECT BY WITH FILTERING | |
|* 5 | FILTER | |
|* 6 | TABLE ACCESS FULL | INV_LEG_DUMMY |
| 7 | SORT AGGREGATE | |
|* 8 | TABLE ACCESS BY INDEX ROWID| INV_LEG_DUMMY |
|* 9 | INDEX RANGE SCAN | XPKINV_LEG |
| 10 | NESTED LOOPS | |
| 11 | CONNECT BY PUMP | |
| 12 | TABLE ACCESS BY INDEX ROWID | INV_LEG_DUMMY |
|* 13 | INDEX RANGE SCAN | XPKINV_LEG |
|* 14 | FILTER | |
| 15 | TABLE ACCESS FULL | INV_LEG_DUMMY |
|* 16 | INDEX RANGE SCAN | XPKINV_LEG |
Predicate Information (identified by operation id):
4 - access("L"."DEPARTURE_AIRPORT"=PRIOR "L"."ARRIVAL_AIRPORT" AND
"L"."CARRIER"=PRIOR "L"."CARRIER" AND "L"."FLT_NUM"=PRIOR "L"."FLT
_NUM"
AND INTERNAL_FUNCTION(PRIOR TRUNC(INTERNAL_FUNCTION("L"."FLT_DATE"
)))=TR
UNC(INTERNAL_FUNCTION("L"."FLT_DATE")))
5 - filter("L"."DEPARTURE_AIRPORT"=CASE WHEN ( (SELECT COUNT(*)
FROM "INV_LEG_DUMMY" "IFL" WHERE "IFL"."FLT_NUM_SUFFIX"=:B1 AND
"IFL"."FLT_NUM"=:B2 AND "IFL"."CARRIER"=:B3 AND
"IFL"."ARRIVAL_AIRPORT"=:B4)=0) THEN "L"."DEPARTURE_AIRPORT" END )
6 - access("L"."CARRIER"=PRIOR "L"."CARRIER")
8 - filter("IFL"."ARRIVAL_AIRPORT"=:B1)
9 - access("IFL"."CARRIER"=:B1 AND "IFL"."FLT_NUM"=:B2 AND
"IFL"."FLT_NUM_SUFFIX"=:B3)
13 - access("L"."CARRIER"=PRIOR "L"."CARRIER" AND "L"."FLT_NUM"=PRIOR
"L"."FLT_NUM" AND "L"."DEPARTURE_AIRPORT"=PRIOR "L"."ARRIVAL_AIRPO
RT")
filter("L"."DEPARTURE_AIRPORT"=PRIOR "L"."ARRIVAL_AIRPORT" AND
INTERNAL_FUNCTION(PRIOR TRUNC(INTERNAL_FUNCTION("L"."FLT_DATE")))=
TRUNC(
INTERNAL_FUNCTION("L"."FLT_DATE")))
14 - filter( NOT EXISTS (SELECT 0 FROM "INV_LEG_DUMMY" "IFL" WHERE
"IFL"."FLT_NUM_SUFFIX"=:B1 AND "IFL"."FLT_NUM"=:B2 AND
"IFL"."CARRIER"=:B3 AND "IFL"."DEPARTURE_AIRPORT"=:B4))
16 - access("IFL"."CARRIER"=:B1 AND "IFL"."FLT_NUM"=:B2 AND
"IFL"."FLT_NUM_SUFFIX"=:B3 AND "IFL"."DEPARTURE_AIRPORT"=:B4)
filter("IFL"."DEPARTURE_AIRPORT"=:B1)
Note
- rule based optimizer used (consider using cbo)
Statistics
0 recursive calls
0 db block gets
33 consistent gets
0 physical reads
0 redo size
877 bytes sent via SQL*Net to client
886 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
6 sorts (memory)
0 sorts (disk)
7 rows processedMy method
SQL> SELECT
2 carrier,
3 flt_num,
4 flt_num_suffix,
5 flt_date,
6 arrival_airport,
7 departure_airport,
8 COUNT(*) OVER(PARTITION BY carrier,
9 flt_num
10 ) - LEVEL + 1 seq,
11 COUNT(*) OVER(PARTITION BY carrier,
12 flt_num
13 ) - 1 max_seq
57 /
CAR FLT_ F FLT_DATE ARRIV DEPAR SEQ MAX_SEQ
CX 1 22-MAY-11 BNE CNS 1 2
CX 1 22-MAY-11 HKG BNE 2 2
CX 1 22-MAY-11 HKG 3 2
KA 1 23-MAY-11 HKG TPE 1 3
KA 1 23-MAY-11 DEL HKG 2 3
KA 1 23-MAY-11 KOL DEL 3 3
KA 1 23-MAY-11 KOL 4 3
7 rows selected.
Execution Plan
Plan hash value: 921778235
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | SORT ORDER BY | |
| 2 | WINDOW SORT | |
|* 3 | CONNECT BY NO FILTERING WITH START-WITH| |
| 4 | COUNT | |
| 5 | VIEW | |
| 6 | UNION-ALL | |
| 7 | TABLE ACCESS FULL | INV_LEG_DUMMY |
|* 8 | FILTER | |
| 9 | TABLE ACCESS FULL | INV_LEG_DUMMY |
|* 10 | INDEX RANGE SCAN | XPKINV_LEG |
Predicate Information (identified by operation id):
3 - access("ARRIVAL_AIRPORT"=PRIOR "DEPARTURE_AIRPORT" AND
"CARRIER"=PRIOR "CARRIER" AND "FLT_NUM"=PRIOR "FLT_NUM" AND
TRUNC(INTERNAL_FUNCTION("FLT_DATE"))=INTERNAL_FUNCTION(PRIOR
TRUNC(INTERNAL_FUNCTION("FLT_DATE"))))
filter("ARRIVAL_AIRPORT" IS NULL)
8 - filter( NOT EXISTS (SELECT 0 FROM "INV_LEG_DUMMY" "DL" WHERE
"DL"."FLT_NUM"=:B1 AND "DL"."CARRIER"=:B2 AND
"DL"."DEPARTURE_AIRPORT"=:B3 AND "DL"."FLT_DATE"=:B4))
10 - access("DL"."CARRIER"=:B1 AND "DL"."FLT_NUM"=:B2 AND
"DL"."FLT_DATE"=:B3 AND "DL"."DEPARTURE_AIRPORT"=:B4)
filter("DL"."DEPARTURE_AIRPORT"=:B1 AND "DL"."FLT_DATE"=:B2)
Note
- rule based optimizer used (consider using cbo)
Statistics
1 recursive calls
0 db block gets
19 consistent gets
0 physical reads
0 redo size
877 bytes sent via SQL*Net to client
338 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
7 rows processed
SQL> /
CAR FLT_ F FLT_DATE ARRIV DEPAR SEQ MAX_SEQ
CX 1 22-MAY-11 BNE CNS 1 2
CX 1 22-MAY-11 HKG BNE 2 2
CX 1 22-MAY-11 HKG 3 2
KA 1 23-MAY-11 HKG TPE 1 3
KA 1 23-MAY-11 DEL HKG 2 3
KA 1 23-MAY-11 KOL DEL 3 3
KA 1 23-MAY-11 KOL 4 3
7 rows selected.
Execution Plan
Plan hash value: 921778235
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | SORT ORDER BY | |
| 2 | WINDOW SORT | |
|* 3 | CONNECT BY NO FILTERING WITH START-WITH| |
| 4 | COUNT | |
| 5 | VIEW | |
| 6 | UNION-ALL | |
| 7 | TABLE ACCESS FULL | INV_LEG_DUMMY |
|* 8 | FILTER | |
| 9 | TABLE ACCESS FULL | INV_LEG_DUMMY |
|* 10 | INDEX RANGE SCAN | XPKINV_LEG |
Predicate Information (identified by operation id):
3 - access("ARRIVAL_AIRPORT"=PRIOR "DEPARTURE_AIRPORT" AND
"CARRIER"=PRIOR "CARRIER" AND "FLT_NUM"=PRIOR "FLT_NUM" AND
TRUNC(INTERNAL_FUNCTION("FLT_DATE"))=INTERNAL_FUNCTION(PRIOR
TRUNC(INTERNAL_FUNCTION("FLT_DATE"))))
filter("ARRIVAL_AIRPORT" IS NULL)
8 - filter( NOT EXISTS (SELECT 0 FROM "INV_LEG_DUMMY" "DL" WHERE
"DL"."FLT_NUM"=:B1 AND "DL"."CARRIER"=:B2 AND
"DL"."DEPARTURE_AIRPORT"=:B3 AND "DL"."FLT_DATE"=:B4))
10 - access("DL"."CARRIER"=:B1 AND "DL"."FLT_NUM"=:B2 AND
"DL"."FLT_DATE"=:B3 AND "DL"."DEPARTURE_AIRPORT"=:B4)
filter("DL"."DEPARTURE_AIRPORT"=:B1 AND "DL"."FLT_DATE"=:B2)
Note
- rule based optimizer used (consider using cbo)
Statistics
0 recursive calls
0 db block gets
19 consistent gets
0 physical reads
0 redo size
877 bytes sent via SQL*Net to client
338 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
7 rows processedSalim Chelabi's method
SQL> WITH t AS
2 (SELECT k.*, LEVEL lvl
3 FROM inv_leg_dummy k
4 CONNECT BY PRIOR k.arrival_airport = k.departure_airport
5 AND PRIOR k.flt_date = k.flt_date
6 AND PRIOR k.carrier = k.carrier
7 AND PRIOR k.flt_num = k.flt_num)
8 SELECT carrier, flt_num, flt_num_suffix, flt_date, arrival_airport,
9 departure_airport, MAX (lvl) seq,
10 MAX (MAX (lvl)) OVER (PARTITION BY carrier, flt_num, flt_num_suffix)
11 max_seq
12 FROM t
13 GROUP BY carrier,
14 flt_num,
15 flt_num_suffix,
16 flt_date,
17 arrival_airport,
18 departure_airport
19 UNION ALL
20 SELECT carrier, flt_num, flt_num_suffix, flt_date, NULL,
21 MAX (arrival_airport), MAX (lvl) + 1 seq, MAX (lvl) max_seq
22 FROM t
23 GROUP BY carrier, flt_num, flt_num_suffix, flt_date
24 ORDER BY 1, 2, 3, seq, arrival_airport NULLS LAST;
CAR FLT_ F FLT_DATE ARRIV DEPAR SEQ MAX_SEQ
CX 1 22/05/2011 00:00:00 BNE CNS 1 2
CX 1 22/05/2011 00:00:00 HKG BNE 2 2
CX 1 22/05/2011 00:00:00 HKG 3 2
KA 1 23/05/2011 00:00:00 HKG TPE 1 3
KA 1 23/05/2011 00:00:00 DEL HKG 2 3
KA 1 23/05/2011 00:00:00 KOL DEL 3 3
KA 1 23/05/2011 00:00:00 KOL 4 3
7 rows selected.
Elapsed: 00:00:00.01
Execution Plan
Plan hash value: 2360206974
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | TEMP TABLE TRANSFORMATION | |
| 2 | LOAD AS SELECT | |
|* 3 | CONNECT BY WITHOUT FILTERING| |
| 4 | TABLE ACCESS FULL | INV_LEG_DUMMY |
| 5 | SORT ORDER BY | |
| 6 | UNION-ALL | |
| 7 | WINDOW BUFFER | |
| 8 | SORT GROUP BY | |
| 9 | VIEW | |
| 10 | TABLE ACCESS FULL | SYS_TEMP_0FD9FE280_59EF9B75 |
| 11 | SORT GROUP BY | |
| 12 | VIEW | |
| 13 | TABLE ACCESS FULL | SYS_TEMP_0FD9FE280_59EF9B75 |
Predicate Information (identified by operation id):
3 - access("K"."DEPARTURE_AIRPORT"=PRIOR "K"."ARRIVAL_AIRPORT" AND
"K"."FLT_DATE"=PRIOR "K"."FLT_DATE" AND "K"."CARRIER"=PRIOR
"K"."CARRIER" AND "K"."FLT_NUM"=PRIOR "K"."FLT_NUM")
Note
- rule based optimizer used (consider using cbo)
Statistics
57 recursive calls
10 db block gets
25 consistent gets
1 physical reads
1556 redo size
877 bytes sent via SQL*Net to client
338 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
5 sorts (memory)
0 sorts (disk)
7 rows processed
SQL> /
CAR FLT_ F FLT_DATE ARRIV DEPAR SEQ MAX_SEQ
CX 1 22/05/2011 00:00:00 BNE CNS 1 2
CX 1 22/05/2011 00:00:00 HKG BNE 2 2
CX 1 22/05/2011 00:00:00 HKG 3 2
KA 1 23/05/2011 00:00:00 HKG TPE 1 3
KA 1 23/05/2011 00:00:00 DEL HKG 2 3
KA 1 23/05/2011 00:00:00 KOL DEL 3 3
KA 1 23/05/2011 00:00:00 KOL 4 3
7 rows selected.
Elapsed: 00:00:00.01
Execution Plan
Plan hash value: 4065363664
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | TEMP TABLE TRANSFORMATION | |
| 2 | LOAD AS SELECT | |
|* 3 | CONNECT BY WITHOUT FILTERING| |
| 4 | TABLE ACCESS FULL | INV_LEG_DUMMY |
| 5 | SORT ORDER BY | |
| 6 | UNION-ALL | |
| 7 | WINDOW BUFFER | |
| 8 | SORT GROUP BY | |
| 9 | VIEW | |
| 10 | TABLE ACCESS FULL | SYS_TEMP_0FD9FE281_59EF9B75 |
| 11 | SORT GROUP BY | |
| 12 | VIEW | |
| 13 | TABLE ACCESS FULL | SYS_TEMP_0FD9FE281_59EF9B75 |
Predicate Information (identified by operation id):
3 - access("K"."DEPARTURE_AIRPORT"=PRIOR "K"."ARRIVAL_AIRPORT" AND
"K"."FLT_DATE"=PRIOR "K"."FLT_DATE" AND "K"."CARRIER"=PRIOR
"K"."CARRIER" AND "K"."FLT_NUM"=PRIOR "K"."FLT_NUM")
Note
- rule based optimizer used (consider using cbo)
Statistics
2 recursive calls
8 db block gets
15 consistent gets
1 physical reads
604 redo size
877 bytes sent via SQL*Net to client
338 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
5 sorts (memory)
0 sorts (disk)
7 rows processed
SQL> Personally I think Salim's method seems very suiccinct and I had expected there would be more of a difference in terms of performance metrics between it and my attempt but it appears there's not much between the two - although Salim's method is generating redo as a result of the temp table through the subquery factoring. I'd be interested to see the results of a full trace between them.
Either way though, there are two alternatives which seem a fair bit more optimal than the original SQL so it's quids in I guess! :-)
David
Edited by: Bravid on Aug 12, 2011 3:24 PM
Edited by: Bravid on Aug 12, 2011 3:27 PM
Updated the comparison with Salims additional column
Similar Messages
-
Customer Trans Transaction Invoice
Code Date Type Amount
A001 01-JAN-2004 invoice 1000
A001 01-FEB-2004 Receipt -1500
A001 01-MAR-2004 invoice 2000
A001 01-APR-2002 invoice 2500
OUTPUT wanted to be...
Customer Trans Transaction Invoice
Code Date Type Amount Balance
A001 01-JAN-2004 invoice 1000 1000
A001 01-FEB-2004 receipt -1500 500
A001 01-MAR-2004 invoice 2000 2500
A001 01-APR-2002 invoice 2500 5000
Hi All,
Please look into the above data, If analytic function LAG(),CASE expression are possible to locate the cursor position in the single query, then any one can help me out about it. I tried but could not.
Regards,
Neel.You can do this with the SUM analytic function. Taking the emp table, for example,
1 select ename, sal, SUM(sal) OVER (order by empno)
2 from emp
3* order by empno
SQL> /
ENAME SAL SUM(SAL)OVER(ORDERBYEMPNO)
SMITH 800 800
ALLEN 1600 2400
WARD 1250 3650
JONES 2975 6625
MARTIN 1250 7875
BLAKE 2850 10725
CLARK 2450 13175
SCOTT 3000 16175
KING 5000 21175
TURNER 1500 22675
ADAMS 1100 23775
JAMES 950 24725
FORD 3000 27725
MILLER 1300 29025
14 rows selected.Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
How can I write this query In parameterize form so That I can prevent sql Injection
String strQry = "INSERT INTO tblVoucherType (VhrTypeCode,moduleCode,transCCode,"
+ "voucherType,OrderNumber,active,AccountId) "
+ " values('" + txtVhrCode.Text + "','" + ddlModule.SelectedValue.ToString() + "',"
+ "'" + ddlTrans.SelectedValue.ToString() + "','" + txtVhrName.Text + "','" + btnRadio.SelectedValue + "'"
+ ", '" + status.Checked + "', '" + txtAccount.Text + "')";Basically it will look like:
String strQry = "INSERT INTO tblVoucherType (VhrTypeCode,moduleCode,transCCode, ...)"
+ " values(@VhrCode, @moduleCode, @transCCode, ....)";
sqlCommand.Parameters.AddWithValue("@VhrCode", txtVhrCode.Text );
sqlCommand.Parameters.AddWithValue("@moduleCode", ddlModule.SelectedValue.ToString() );
sqlCommand.Parameters.AddWithValue("@transCCode", ddlTrans.SelectedValue.ToString() );
.. and so on
sqlCommand.ExecuteNonQuery();
Olaf Helper
[ Blog] [ Xing] [ MVP] -
CAn we write a query like this
CAn we write a query like this?
If not how can this be changed?
select col1,col2,col3.col4
from table 1
where col1,col2 in(select col1,col2 from table2)Or it may be identical:
SQL> CREATE TABLE t1 (c INT NOT NULL);
Table created.
SQL> CREATE TABLE t2 (c INT NOT NULL);
Table created.
SQL> INSERT ALL
2 INTO t1 VALUES (rn)
3 INTO t2 VALUES (rn)
4 SELECT rownum AS rn FROM user_tables WHERE rownum <= 15;
30 rows created.
SQL> BEGIN
2 DBMS_STATS.GATHER_TABLE_STATS(user,'T1');
3 DBMS_STATS.GATHER_TABLE_STATS(user,'T2');
4 END;
5 /
PL/SQL procedure successfully completed.
SQL> SELECT * FROM t1
2 WHERE EXISTS
3 ( SELECT 1 FROM t2 WHERE c = t1.c )
4
SQL> @xplan
| Id | Operation | Name | Rows | Bytes | Cost |
| 0 | SELECT STATEMENT | | 15 | 90 | 5 |
|* 1 | HASH JOIN SEMI | | 15 | 90 | 5 |
| 2 | TABLE ACCESS FULL | T1 | 15 | 45 | 2 |
| 3 | TABLE ACCESS FULL | T2 | 15 | 45 | 2 |
Predicate Information (identified by operation id):
1 - access("T2"."C"="T1"."C")
SQL> SELECT * FROM t1
2 WHERE c IN
3 ( SELECT c FROM t2 )
4
SQL> @xplan
| Id | Operation | Name | Rows | Bytes | Cost |
| 0 | SELECT STATEMENT | | 15 | 90 | 5 |
|* 1 | HASH JOIN SEMI | | 15 | 90 | 5 |
| 2 | TABLE ACCESS FULL | T1 | 15 | 45 | 2 |
| 3 | TABLE ACCESS FULL | T2 | 15 | 45 | 2 |
Predicate Information (identified by operation id):
1 - access("T1"."C"="T2"."C") -
My grandad has a 1st edition iPad and when he tries to download certain apps it says he needs at least iOS 5? Shouldn't this be updated automatically? Or can this be updated another way? I haven't been to see his iPad so he may not be looking properly??? Any helpers???
The iOS version is not updated automatically, though if he connects it to his computer's iTunes (if it's a recent enough version) he should be prompted as to whether he wants to update.
He should connect the iPad to his computer's iTunes and copy any purchases off the iPad to his computer via File > Transfer Purchases. He may also want to copy photos and any important documents off the iPad as well e.g. via the file sharing section at the bottom of the device's apps tab when connected to iTunes, via wifi, email, dropbox etc - they should be included in the backup, but it's best to have a copy of them outside of the backup just in case. He can then force a backup of the iPad by right-clicking the iPad 'Device' on the left-hand side of iTunes and selecting 'Backup'.
He can then start the update by selecting the iPad on the left-hand side of iTunes, and on the Summary tab on the right-hand side clicking the Check For Updates button
Updating to iOS 5+ : http://support.apple.com/kb/HT4972
His iPad should update to iOS 5.1.1 (which is the only version that is 'signed' by Apple for the iPad 1, it doesn't support iOS 6). -
How to write this query to filter combination of few values
Hi,
I have a table CHIMM which is a transaction table and contains information of the vaccines given to a child.
columns are: child_id, vacc_id, vacc_given_dt. I have to query for remaining vaccines.
HEXA is a vaccine_id which is composite vaccine of DPT1,POL1,HBV1 & HIB1 (vaccine ids).
I want to write to query if any of DPT1,POL1,HBV1 & HIB1 given then HEXA should not be displayed in the result.
OR
if HEXA is given then of course any of DPT1,POL1,HBV1 & HIB1 should not be displayed in the result.
How to write this query?
RegardsHi,
I'm still not sure what the output you want from that sample data is. Do you just want the child_ids, like this
CHILD_ID
3
4? If so, here's one way to get them:
WITH all_vacc_ids AS
SELECT c.child_id
, c.vacc_id AS child_vacc_id
, v.vacc_id
, COUNT ( CASE
WHEN c.vacc_id = 'HEXA'
THEN 1
END
) OVER ( PARTITION BY c.child_id
) AS hexa_itself
FROM vacc v
LEFT OUTER JOIN chimm c PARTITION BY (c.child_id)
ON c.vacc_id = v.vacc_id
WHERE v.vacc_desc = 'HEXA' -- See note below
SELECT child_id
FROM all_vacc_ids
WHERE child_vacc_id IS NULL
AND vacc_id != 'HEXA'
AND hexa_itself = 0
GROUP BY child_id
rha2 wrote:there are alot of vaccines, i just put 3 for example. this query gives error: invalid relational operatorAre you saying that the vacc table contains other rows, but those other rows are not needed for this problem? It would be good if you included an example in the sample data. The query above considers only the rows in vacc where vacc_desc='HEXA'. You can have other rows in the vacc table, but they won't affect the output of this query. The query above makes no assumptions about the number of rows that have vacc_desc='HEXA'; it will report all child_ids who are missing any of them, regardless of the number (assuming the child does not have the 'HEXA' vacc_id itself, like child_id=1).
You still haven't said which version of Oracle you're using. The query above will work in Oracle 10 (and higher). -
How to write this query ?
how to write this query ?
list the emp name who is working for the highest avg sal department.
I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?
the row_number version is like this
select emp.* from emp ,
select deptno, row_number() over (order by avg(sal) desc) r from emp
group by deptno
)e
where e.r = 1
and emp.deptno = e.deptnoHi,
806540 wrote:
how to write this query ?
list the emp name who is working for the highest avg sal department.
I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?ROW_NUMBER is just plain SQL, and has been since Oracle 8.1.
ROW_NUMBER (or its close relative, RANK) is the simplest and most efficient way to solve this problem. Why not do this the right way?
the row_number version is like this
select emp.* from emp ,
select deptno, row_number() over (order by avg(sal) desc) r from emp
group by deptno
)e
where e.r = 1
and emp.deptno = e.deptno
If there happens to be a tie (that is, two or more departments have the same average sal, and it is the highest), then the query above will only arbitrarily treat one of them (no telling which one) as the highest. Change ROW_NUMBER to RANK to get all departments with a claim to having the highest average sal.
You could use the ROWNUM pseudo-column instead of ROW_NUMBER, if all you want to do is avoid ROW_NUMBER.
Without using ROW_NUMBER or RANK, there are lots of ways involving other analytic functions, such as AVG and MAX.
If you really, really don't want to use analytic functions at all, you can do this:
SELECT *
FROM scott.emp
WHERE deptno IN (
SELECT deptno
FROM scott.emp
GROUP BY deptno
HAVING AVG (sal) = (
SELECT MAX (AVG (sal))
FROM scott.emp
GROUP BY deptno
; -
How can i write this C-Code in G-Code
hallo
how can I write this C-Code in LabVIew ,
for a=0; a=<10; a++
for b=0; b=5 ; b+2
X= 3+b;
Y=1+a;
please see the attachment and tell me where is the problem
Attachments:
Unbenannt 11.vi 43 KBWell, at least you tried and got some of it right.
I think this should do what you want, but my C is rusty. Is the increment performed before or after the loop executes? If it's after, then I believe the loop should iterate 11 times, not 10.
In any case, you should note that for a literal translation, you would need to add a sequence structure to guarantee that Y was written to only after the inner loop finished because of the way data-flow works.. Also, note that controls and indicators in LabVIEW are not equivalent to variables. They can be used as such, but they usually should not be.
Another point about this is that you probably want to use the correct data type - the orange terminals are floating point indicators (of double precision, in this case) and you want integers.
To learn more about LabVIEW, I suggest you try looking at some of these tutorials.
Try to take over the world!
Attachments:
C.png 4 KB -
i lose my iphone but i turn off icloud >> how i can use ( find my phone ) or another way or another sofware to find my iphone
please help meYou have to have iCloud enabled and Find My iPhone set to On on your iPhone in order to find it. If you're saying you turned this off before losing it I'm afraid you're out of luck.
-
Crystal Report XI: Can you write your query in SQL instead of using the GUI
Hello
In crystal report version XI, can you write your query in sqlplus and then use the crystal report designer to build your report instead of using the GUI . I would like to be able to go database and show sql query and open that query and make changes directly there, is that possible on this version, if yes is there a setting somewhere? Please let me know.
Thank you
alphaMoved to Database forum.
No you can no longer modify the SQL directly. Use a Command Object to enter the SQL directly.
Thank you
Don -
How can i improve this query.
Hi guys i am beginner , just wanted to know some info , how can i improve this query ..
select *
from tableA A, viewB B,
where A.key = B.key
and a.criteria1 = '111'
and a.criteria2 = some_funtion(a.key)
one more thing should function should be on left side of equal sign.
will a join make it better or something else is needed more than that .952936 wrote:
Hi guys i am beginner , just wanted to know some info , how can i improve this query ..
select *
from tableA A, viewB B,
where A.key = B.key
and a.criteria1 = '111'
and a.criteria2 = some_funtion(a.key)
one more thing should function should be on left side of equal sign.
will a join make it better or something else is needed more than that .If you are a beginner try to learn the ANSI Syntax. This will help you a lot to write better queries.
Your select would look like this in ANSI.
select *
from tableA A
JOIN viewB B ON A.key = B.key
WHERE a.criteria1 = '111'
and a.criteria2 = some_function(a.key);The good thing here is that this separates the typical joining part of the select from the typical filter criteria.
The other syntax very often let you forget one join. Just because there are so many tables and so many filters, that you just don't notice correctly anymore what was join and what not.
If you notice that the number of column is not what you expect, you can easiely modify the query and compare the results.
example A
Remove View B from the query (temporarily comment it out).
select *
from tableA A
--JOIN viewB B ON A.key = B.key
WHERE a.criteria1 = '111'
and a.criteria2 = some_funtion(a.key)
example B
You notice, that values from A are missing. Maybe because there is no matching key in ViewB? Then change the join to an outer join.
select *
from tableA A
LEFT OUTER JOIN viewB B ON A.key = B.key
WHERE a.criteria1 = '111'
and a.criteria2 = some_funtion(a.key)(The outer keyword is optional, left join would be enough). -
List v = new Vector() how can i write this ?
List v = new Vector() how can i write this ?
vector does not 'extends' List rather it 'implements' only ......so how can write this way ? ( polymorphism applies only for 'extends' ).my question in a simple way is :
List some_list extends Vector
No, List is an interface; Vector is a concrete class. Read the javadocs.
Vector implements List
>
AND
List some_list implements Vector
are these two same in behaviour
our apart from theoretical differences ?thanks
Interfaces don't have any implementation; they're pure method signatures. When a concrete class implements an interface, it makes a promise that says "I will provide implementations for exactly these methods. Any client can call a method from the interface and I will do something 'sensible' if I'm written correctly."
From the point of view of static and dynamic types, there's no difference between interfaces and classes.
C++ has interfaces, too - they're classes with all pure virtual methods. If you understand how C++ works, the concept shouldn't be hard.
ArrayList is preferred precisely because it isn't synchronized by default. (You can always make it so using java.util.Collections if you wish later on.) If you don't need synchronization, why pay the performance penalty? -
In the development center we use Labview 6.01, in the testcenter we use labview 5.
Additionally, in the development center we use a unix version of Labview and in the testcenter a Windows version.You can Save with Option to save VIs as version 5.
Unless your VIs deal with system files in UNIX or Windows (ie. Registry...)
then you have to develop your VI specificly on that OS, since UNIX and
Windows have different ways to deal with system files. Mostly, LabVIEW is OS
independent. I used to have VIs developed from LabVIEW v.3 on UNIX, I can
still open these files from LV v.4 on Windows. Now I can still work with
these VIs on LV 6 on Windows.
Good luck,
Nam.
roybra wrote in message
news:[email protected]..
> I created an application on a machine with Labview 6.01, can I run
> this application on another machine that has labview 5?
>
> In the development center we use Labview 6.01, in the testcenter we
> use l
abview 5.
>
> Additionally, in the development center we use a unix version of
> Labview and in the testcenter a Windows version. -
I want to disable or remove Game center from my iphone. It's always pop up during i play hay day game. So i can not solve this problem. Any way i try to log in game center my several Apple id account but it does' not work. I am still stuck in log in page
The Game Center app is pre installed and cannot be removed from the device.
Best thing to do is move it to the last available screen.
Hold down the Game Center app until all the apps jiggle then sliide the Game Center app to the right from one screen to the next. This requires a bit of finesse, but it's doable.
Press the Home button one time to stop the apps from jiggling.
For the Apple ID issue, try resetting.
Tap Settings > General > Reset > Reset All Settings
Then try your Apple ID and password for the Game Center app. -
i lost my iphone and now icloud dosent download the photos !!! how can i get my photos in another way ??
You will need to get another phone to restore from you icloud backup http://support.apple.com/kb/HT4859
FAQ photo stream http://support.apple.com/kb/HT4486
Maybe you are looking for
-
How many users on Mac Mini Server?
Good morning, Apple's recent changes to the Mac Mini line have thrown a wrench in a project plan I've been working. One part of the project needs a quad-core computer (FileMaker Server) and so I'm having to either search for an older model or switch
-
How do I use jstl to insert links that passes sql info?
Hi all, I'm using JSTL with sql and I want to create a link for every row so that the link would also contain the item in the first column of every row. But I keep getting stuck at saving the value. This is what I've tried: <c:set var="myName" value=
-
AppliedCellStyle not giving the whole answer
I am trying to return the cell style for a cell in a table. Normally appliedCellStyle would do this for me. But, I have applied a table style to the table which has the 'left column' attribute setting the first column cell style to 'left' When I use
-
Hi All, I have downloaded printer driver for Lexmark T644 , in spad from menu utilities->device type-->import i have selected radio button as device type under object i have provided the driver file name (print-drivers-hpux11.11.pkg.g
-
Is there a way to split the iPad screen without an app?
I was trying the split the iPad like you can on a PC. I saw some apps to do but I was wondering if there was a way to do it without an app. Any suggestions?