how to rank 3 highest value for each date - sqlite

i am new to sqlite, trying to solve how to only rank 3 highest value for each days
Table : price
Date | Name | value1 |
21-08-2018 | A | 100
21-08-2018 | B | 90
21-08-2018 | C | 80
21-08-2018 | D | 70
21-08-2018 | E | 60
21-08-2018 | F | 50
22-08-2018 | B | 99
22-08-2018 | A | 88
22-08-2018 | D | 77
22-08-2018 | C | 66
22-08-2018 | E | 55
22-08-2018 | F | 44
23-08-2018 | D | 90
23-08-2018 | A | 80
23-08-2018 | B | 70
23-08-2018 | C | 80
23-08-2018 | F | 70
23-08-2018 | E | 60
i'am expecting the result like below
Date | Name | value1 |
21-08-2018 | A | 100
21-08-2018 | B | 90
21-08-2018 | C | 80
22-08-2018 | B | 99
22-08-2018 | A | 88
22-08-2018 | D | 77
23-08-2018 | D | 90
23-08-2018 | A | 80
23-08-2018 | B | 70
i've tried
select *, max (value1)
from price
group by (date)
but only show one line, already tried using TOP3, but only show 3 from lots of row,
thanks

If you're using Sqlite 3.25 or newer, window functions make it easy:
WITH ranked AS (SELECT date, name, value1
, rank() OVER (PARTITION BY date ORDER BY value1 DESC) AS rank
FROM price)
SELECT date, name, value1
FROM ranked
WHERE rank <= 3
ORDER BY date, rank;
gives
date name value1
---------- ---------- ----------
21-08-2018 A 100
21-08-2018 B 90
21-08-2018 C 80
22-08-2018 B 99
22-08-2018 A 88
22-08-2018 D 77
23-08-2018 D 90
23-08-2018 A 80
23-08-2018 C 80
This will potentially return more than three rows per date in case of duplicate value1 figures causing ties - use row_number() instead of rank() for only ever at most three.

Related

Calculating weighted average buy and hold return per ID in R

Thanks to #langtang, I was able to calculate Buy and Hold Return around the event date for each company (Calculating Buy and Hold return around event date per ID in R). But then now I am facing a new problem.
Below is the data I currently have.
+----+------------+-------+------------+------------+----------------------+
| ID | Date | Price | EventDate | Market Cap | BuyAndHoldIndividual |
+----+------------+-------+------------+------------+----------------------+
| 1 | 2011-03-06 | 10 | NA | 109 | NA |
| 1 | 2011-03-07 | 9 | NA | 107 | -0.10000 |
| 1 | 2011-03-08 | 12 | NA | 109 | 0.20000 |
| 1 | 2011-03-09 | 14 | NA | 107 | 0.40000 |
| 1 | 2011-03-10 | 15 | NA | 101 | 0.50000 |
| 1 | 2011-03-11 | 17 | NA | 101 | 0.70000 |
| 1 | 2011-03-12 | 12 | 2011-03-12 | 110 | 0.20000 |
| 1 | 2011-03-13 | 14 | NA | 110 | 0.40000 |
| 1 | 2011-03-14 | 17 | NA | 100 | 0.70000 |
| 1 | 2011-03-15 | 14 | NA | 101 | 0.40000 |
| 1 | 2011-03-16 | 17 | NA | 107 | 0.70000 |
| 1 | 2011-03-17 | 16 | NA | 104 | 0.60000 |
| 1 | 2011-03-18 | 15 | NA | 104 | NA |
| 1 | 2011-03-19 | 16 | NA | 102 | 0.06667 |
| 1 | 2011-03-20 | 17 | NA | 107 | 0.13333 |
| 1 | 2011-03-21 | 18 | NA | 104 | 0.20000 |
| 1 | 2011-03-22 | 11 | NA | 105 | -0.26667 |
| 1 | 2011-03-23 | 15 | NA | 100 | 0.00000 |
| 1 | 2011-03-24 | 12 | 2011-03-24 | 110 | -0.20000 |
| 1 | 2011-03-25 | 13 | NA | 110 | -0.13333 |
| 1 | 2011-03-26 | 15 | NA | 107 | 0.00000 |
| 2 | 2011-03-12 | 48 | NA | 300 | NA |
| 2 | 2011-03-13 | 49 | NA | 300 | NA |
| 2 | 2011-03-14 | 50 | NA | 290 | NA |
| 2 | 2011-03-15 | 57 | NA | 296 | 0.14000 |
| 2 | 2011-03-16 | 60 | NA | 297 | 0.20000 |
| 2 | 2011-03-17 | 49 | NA | 296 | -0.02000 |
| 2 | 2011-03-18 | 64 | NA | 299 | 0.28000 |
| 2 | 2011-03-19 | 63 | NA | 292 | 0.26000 |
| 2 | 2011-03-20 | 67 | 2011-03-20 | 290 | 0.34000 |
| 2 | 2011-03-21 | 70 | NA | 299 | 0.40000 |
| 2 | 2011-03-22 | 58 | NA | 295 | 0.16000 |
| 2 | 2011-03-23 | 65 | NA | 290 | 0.30000 |
| 2 | 2011-03-24 | 57 | NA | 296 | 0.14000 |
| 2 | 2011-03-25 | 55 | NA | 299 | 0.10000 |
| 2 | 2011-03-26 | 57 | NA | 299 | NA |
| 2 | 2011-03-27 | 60 | NA | 300 | NA |
| 3 | 2011-03-18 | 5 | NA | 54 | NA |
| 3 | 2011-03-19 | 10 | NA | 50 | NA |
| 3 | 2011-03-20 | 7 | NA | 53 | NA |
| 3 | 2011-03-21 | 8 | NA | 53 | NA |
| 3 | 2011-03-22 | 7 | NA | 50 | NA |
| 3 | 2011-03-23 | 8 | NA | 51 | 0.14286 |
| 3 | 2011-03-24 | 7 | NA | 52 | 0.00000 |
| 3 | 2011-03-25 | 6 | NA | 55 | -0.14286 |
| 3 | 2011-03-26 | 9 | NA | 54 | 0.28571 |
| 3 | 2011-03-27 | 9 | NA | 55 | 0.28571 |
| 3 | 2011-03-28 | 9 | 2011-03-28 | 50 | 0.28571 |
| 3 | 2011-03-29 | 6 | NA | 52 | -0.14286 |
| 3 | 2011-03-30 | 6 | NA | 53 | -0.14286 |
| 3 | 2011-03-31 | 4 | NA | 50 | -0.42857 |
| 3 | 2011-04-01 | 5 | NA | 50 | -0.28571 |
| 3 | 2011-04-02 | 8 | NA | 55 | 0.00000 |
| 3 | 2011-04-03 | 9 | NA | 55 | NA |
+----+------------+-------+------------+------------+----------------------+
This time, I would like to make a new column called BuyAndHoldWeightedMarket, where I calculate the weighted average (by Market cap) Buy and Hold return for each ID around -5 ~ +5 days of the event date. For example, for ID =1, starting from 2011-03-19, BuyAndHoldWeightedMarket is calculated as the sum product of (prices for each ID(t)/prices for each ID(eventdate-6)-1) and Market Caps for that day for each ID and then dividing that by the sum of the Market Caps for each ID on that day.
Please check the below picture for the details. The equations are listed for each case of colored blocks.
Please note that for the uppermost BuyAndHoldWeightedMarket, ID =2,3 is not involved because they begin later than 2011-03-06. For the third block (grey colored area), the calculation of weighted return only includes ID=1,2 because Id=3 begins later than 2011-03-14. Also, for the Last block (mixed color), the first four rows use all three IDs, Blue area uses only ID=2,3 because ID=1 ends 2011-03-26, and the yellow block uses only ID=3 because ID=1, 2 ends before 2011-03-28.
Eventually, I would like to get a nice data table that looks as below.
+----+------------+-------+------------+------------+----------------------+--------------------------+
| ID | Date | Price | EventDate | Market Cap | BuyAndHoldIndividual | BuyAndHoldWeightedMarket |
+----+------------+-------+------------+------------+----------------------+--------------------------+
| 1 | 2011-03-06 | 10 | NA | 109 | NA | NA |
| 1 | 2011-03-07 | 9 | NA | 107 | -0.10000 | -0.10000 |
| 1 | 2011-03-08 | 12 | NA | 109 | 0.20000 | 0.20000 |
| 1 | 2011-03-09 | 14 | NA | 107 | 0.40000 | 0.40000 |
| 1 | 2011-03-10 | 15 | NA | 101 | 0.50000 | 0.50000 |
| 1 | 2011-03-11 | 17 | NA | 101 | 0.70000 | 0.70000 |
| 1 | 2011-03-12 | 12 | 2011-03-12 | 110 | 0.20000 | 0.20000 |
| 1 | 2011-03-13 | 14 | NA | 110 | 0.40000 | 0.40000 |
| 1 | 2011-03-14 | 17 | NA | 100 | 0.70000 | 0.70000 |
| 1 | 2011-03-15 | 14 | NA | 101 | 0.40000 | 0.40000 |
| 1 | 2011-03-16 | 17 | NA | 107 | 0.70000 | 0.70000 |
| 1 | 2011-03-17 | 16 | NA | 104 | 0.60000 | 0.60000 |
| 1 | 2011-03-18 | 15 | NA | 104 | NA | NA |
| 1 | 2011-03-19 | 16 | NA | 102 | 0.06667 | 0.11765 |
| 1 | 2011-03-20 | 17 | NA | 107 | 0.13333 | 0.10902 |
| 1 | 2011-03-21 | 18 | NA | 104 | 0.20000 | 0.17682 |
| 1 | 2011-03-22 | 11 | NA | 105 | -0.26667 | -0.07924 |
| 1 | 2011-03-23 | 15 | NA | 100 | 0.00000 | 0.07966 |
| 1 | 2011-03-24 | 12 | 2011-03-24 | 110 | -0.20000 | -0.07331 |
| 1 | 2011-03-25 | 13 | NA | 110 | -0.13333 | -0.09852 |
| 1 | 2011-03-26 | 15 | NA | 107 | 0.00000 | 0.02282 |
| 2 | 2011-03-12 | 48 | NA | 300 | NA | NA |
| 2 | 2011-03-13 | 49 | NA | 300 | NA | NA |
| 2 | 2011-03-14 | 50 | NA | 290 | NA | NA |
| 2 | 2011-03-15 | 57 | NA | 296 | 0.14000 | 0.059487331 |
| 2 | 2011-03-16 | 60 | NA | 297 | 0.20000 | 0.147029703 |
| 2 | 2011-03-17 | 49 | NA | 296 | -0.02000 | -0.030094118 |
| 2 | 2011-03-18 | 64 | NA | 299 | 0.28000 | 0.177381404 |
| 2 | 2011-03-19 | 63 | NA | 292 | 0.26000 | 0.177461929 |
| 2 | 2011-03-20 | 67 | 2011-03-20 | 290 | 0.34000 | 0.24836272 |
| 2 | 2011-03-21 | 70 | NA | 299 | 0.40000 | 0.311954459 |
| 2 | 2011-03-22 | 58 | NA | 295 | 0.16000 | 0.025352941 |
| 2 | 2011-03-23 | 65 | NA | 290 | 0.30000 | 0.192911011 |
| 2 | 2011-03-24 | 57 | NA | 296 | 0.14000 | 0.022381918 |
| 2 | 2011-03-25 | 55 | NA | 299 | 0.10000 | 0.009823098 |
| 2 | 2011-03-26 | 57 | NA | 299 | NA | NA |
| 2 | 2011-03-27 | 60 | NA | 300 | NA | NA |
| 3 | 2011-03-18 | 5 | NA | 54 | NA | NA |
| 3 | 2011-03-19 | 10 | NA | 50 | NA | NA |
| 3 | 2011-03-20 | 7 | NA | 53 | NA | NA |
| 3 | 2011-03-21 | 8 | NA | 53 | NA | NA |
| 3 | 2011-03-22 | 7 | NA | 50 | NA | NA |
| 3 | 2011-03-23 | 8 | NA | 51 | 0.14286 | 0.178343199 |
| 3 | 2011-03-24 | 7 | NA | 52 | 0.00000 | 0.010691161 |
| 3 | 2011-03-25 | 6 | NA | 55 | -0.14286 | -0.007160905 |
| 3 | 2011-03-26 | 9 | NA | 54 | 0.28571 | 0.106918456 |
| 3 | 2011-03-27 | 9 | NA | 55 | 0.28571 | 0.073405953 |
| 3 | 2011-03-28 | 9 | 2011-03-28 | 50 | 0.28571 | 0.285714286 |
| 3 | 2011-03-29 | 6 | NA | 52 | -0.14286 | -0.142857143 |
| 3 | 2011-03-30 | 6 | NA | 53 | -0.14286 | -0.142857143 |
| 3 | 2011-03-31 | 4 | NA | 50 | -0.42857 | -0.428571429 |
| 3 | 2011-04-01 | 5 | NA | 50 | -0.28571 | -0.285714286 |
| 3 | 2011-04-02 | 8 | NA | 55 | 0.00000 | 0.142857143 |
| 3 | 2011-04-03 | 9 | NA | 55 | NA | NA |
+----+------------+-------+------------+------------+----------------------+--------------------------+
I tried so far by using the following code, with the help of the previous question, but I am having a hard time figure out how to calculate the weighted BUY AND HOLD return that begins around different event dates for each ID.
#choose rows with no NA in event date and only show ID and event date
events = unique(df[!is.na(EventDate),.(ID,EventDate)])
#helper column
#:= is defined for use in j only. It adds or updates or removes column(s) by reference.
#It makes no copies of any part of memory at all.
events[, eDate:=EventDate]
#makes new column(temporary) lower and upper boundary
df[, `:=`(s=Date-6, e=Date+6)]
#non-equi match
bhr = events[df, on=.(ID, EventDate>=s, EventDate<=e), nomatch=0]
#Generate the BuyHoldReturn column, by ID and EventDate
bhr2 = bhr[, .(Date, BuyHoldReturnM1=c(NA, (Price[-1]/Price[1] -1)*MarketCap[-1])), by = .(Date)]
#merge back to get the full data
bhr3 = bhr2[df,on=.(ID,Date),.(ID,Date,Price,EventDate=i.EventDate,BuyHoldReturn)]
I would be grateful if you could help.
Thank you very much in advance!

Mariadb: select average by hour and other column

I have a table in a Mariadb version 10.3.27 database that looks like this:
+----+------------+---------------+-----------------+
| id | channel_id | timestamp | value |
+----+------------+---------------+-----------------+
| 1 | 2 | 1623669600000 | 2882.4449252449 |
| 2 | 1 | 1623669600000 | 295.46914369742 |
| 3 | 2 | 1623669630000 | 2874.46365243 |
| 4 | 1 | 1623669630000 | 295.68124546516 |
| 5 | 2 | 1623669660000 | 2874.9638893452 |
| 6 | 1 | 1623669660000 | 295.69561247521 |
| 7 | 2 | 1623669690000 | 2878.7120274678 |
and I want to have a result like this:
+------+-------+-------+
| hour | valhh | valwp |
+------+-------+-------+
| 0 | 419 | 115 |
| 1 | 419 | 115 |
| 2 | 419 | 115 |
| 3 | 419 | 115 |
| 4 | 419 | 115 |
| 5 | 419 | 115 |
| 6 | 419 | 115 |
| 7 | 419 | 115 |
| 8 | 419 | 115 |
| 9 | 419 | 115 |
| 10 | 419 | 115 |
| 11 | 419 | 115 |
| 12 | 419 | 115 |
| 13 | 419 | 115 |
| 14 | 419 | 115 |
| 15 | 419 | 115 |
| 16 | 419 | 115 |
| 17 | 419 | 115 |
| 18 | 419 | 115 |
| 19 | 419 | 115 |
| 20 | 419 | 115 |
| 21 | 419 | 115 |
| 22 | 419 | 115 |
| 23 | 419 | 115 |
+------+-------+-------+
but with valhh (valwp) being the average of the values for the hour of the day for all days where the channel_id is 1 (2) and not the overall average. So far, I've tried:
select h.hour, hh.valhh, wp.valwp from
(select hour(from_unixtime(timestamp/1000)) as hour from data) h,
(select hour(from_unixtime(timestamp/1000)) as hour, cast(avg(value) as integer) as valhh from data where channel_id = 1) hh,
(select hour(from_unixtime(timestamp/1000)) as hour, cast(avg(value) as integer) as valwp from data where channel_id = 2) wp group by h.hour;
which gives the result above (average of all values).
I can get what I want by querying the channels separately, i.e.:
select hour(from_unixtime(timestamp/1000)) as hour, cast(avg(value) as integer) as value from data where channel_id = 1 group by hour;
gives
+------+-------+
| hour | value |
+------+-------+
| 0 | 326 |
| 1 | 145 |
| 2 | 411 |
| 3 | 142 |
| 4 | 143 |
| 5 | 171 |
| 6 | 160 |
| 7 | 487 |
| 8 | 408 |
| 9 | 186 |
| 10 | 214 |
| 11 | 199 |
| 12 | 942 |
| 13 | 521 |
| 14 | 196 |
| 15 | 247 |
| 16 | 364 |
| 17 | 252 |
| 18 | 392 |
| 19 | 916 |
| 20 | 1024 |
| 21 | 1524 |
| 22 | 561 |
| 23 | 249 |
+------+-------+
but I want to have both channels in one result set as separate columns.
How would I do that?
Thanks!
After a steep learning curve I think I figured it out:
select
hh.hour, hh.valuehh, wp.valuewp
from
(select
hour(from_unixtime(timestamp/1000)) as hour,
cast(avg(value) as integer) as valuehh
from data
where channel_id=1
group by hour) hh
inner join
(select
hour(from_unixtime(timestamp/1000)) as hour,
cast(avg(value) as integer) as valuewp
from data
where channel_id=2
group by hour) wp
on hh.hour = wp.hour;
gives
+------+---------+---------+
| hour | valuehh | valuewp |
+------+---------+---------+
| 0 | 300 | 38 |
| 1 | 162 | 275 |
| 2 | 338 | 668 |
| 3 | 166 | 38 |
| 4 | 152 | 38 |
| 5 | 176 | 37 |
| 6 | 174 | 38 |
| 7 | 488 | 36 |
| 8 | 553 | 37 |
| 9 | 198 | 36 |
| 10 | 214 | 38 |
| 11 | 199 | 612 |
| 12 | 942 | 40 |
| 13 | 521 | 99 |
| 14 | 187 | 38 |
| 15 | 209 | 38 |
| 16 | 287 | 39 |
| 17 | 667 | 37 |
| 18 | 615 | 39 |
| 19 | 854 | 199 |
| 20 | 1074 | 44 |
| 21 | 1470 | 178 |
| 22 | 665 | 37 |
| 23 | 235 | 38 |
+------+---------+---------+

Calculating date difference of subsequent rows based on each group in R

I have a sample table which looks somewhat like this:
| Date | Vendor_Id | Requisitioner | Amount |
|------------|:---------:|--------------:|--------|
| 1/17/2019 | 98 | John | 2405 |
| 4/30/2019 | 1320 | Dave | 1420 |
| 11/29/2018 | 3887 | Michele | 596 |
| 11/29/2018 | 3887 | Michele | 960 |
| 11/29/2018 | 3887 | Michele | 1158 |
| 9/21/2018 | 4919 | James | 857 |
| 10/25/2018 | 4919 | Paul | 1162 |
| 10/26/2018 | 4919 | Echo | 726 |
| 10/26/2018 | 4919 | Echo | 726 |
| 10/29/2018 | 4919 | Andrew | 532 |
| 10/29/2018 | 4919 | Andrew | 532 |
| 11/12/2018 | 4919 | Carlos | 954 |
| 5/21/2018 | 2111 | June | 3580 |
| 5/23/2018 | 7420 | Justin | 224 |
| 5/24/2018 | 1187 | Sylvia | 3442 |
| 5/25/2018 | 1187 | Sylvia | 4167 |
| 5/30/2018 | 3456 | Ama | 4580 |
Based on each requisitioner and vendor id, I need to find the difference in the date such that it should be something like this:
| Date | Vendor_Id | Requisitioner | Amount | Date_Diff |
|------------|:---------:|--------------:|--------|-----------|
| 1/17/2019 | 98 | John | 2405 | NA |
| 4/30/2019 | 1320 | Dave | 1420 | 103 |
| 11/29/2018 | 3887 | Michele | 596 | NA |
| 11/29/2018 | 3887 | Michele | 960 | 0 |
| 11/29/2018 | 3887 | Michele | 1158 | 0 |
| 9/21/2018 | 4919 | James | 857 | NA |
| 10/25/2018 | 4919 | Paul | 1162 | NA |
| 10/26/2018 | 4919 | Paul | 726 | 1 |
| 10/26/2018 | 4919 | Paul | 726 | 0 |
| 10/29/2018 | 4919 | Paul | 532 | 3 |
| 10/29/2018 | 4919 | Paul | 532 | 0 |
| 11/12/2018 | 4917 | Carlos | 954 | NA |
| 5/21/2018 | 2111 | Justin | 3580 | NA |
| 5/23/2018 | 7420 | Justin | 224 | 2 |
| 5/24/2018 | 1187 | Sylvia | 3442 | NA |
| 5/25/2018 | 1187 | Sylvia | 4167 | 1 |
| 5/30/2018 | 3456 | Ama | 4580 | NA |
Now, if the difference in the date is <=3 days within each requisitioner and vendor id, and sum of the amount is >5000, I need to create a subset of that. The final output should be something like this:
| Date | Vendor_Id | Requisitioner | Amount | Date_Diff |
|-----------|:---------:|--------------:|--------|-----------|
| 5/24/2018 | 1187 | Sylvia | 3442 | NA |
| 5/25/2018 | 1187 | Sylvia | 4167 | 1 |
Initially, when I tried working with date difference, I used the following code:
df=df %>% mutate(diffdate= difftime(Date,lag(Date,1)))
However, the difference doesn't make sense as they are huge numbers such as 86400 and some huge random numbers. I tried the above code when data type for 'Date' field was initially Posixct. Later when I changed it to 'Date' data type, the date differences were still the same huge random numbers.
Also, is it possible to group the date differences based on requisitioners and vendor id's as mentioned in the 2nd table above?
EDIT:
I'm coming across a new challenge now. In the problem set, I need to filter out the values whose date differences are less than 3 days. Let us assume that the table with date difference appears something like this:
| MasterCalendarDate | Vendor_Id | Requisitioner | Amount | diffdate |
|--------------------|:---------:|--------------:|--------|----------|
| 1/17/2019 | 98 | John | 2405 | #N/A |
| 4/30/2019 | 1320 | Dave | 1420 | 103 |
| 11/29/2018 | 3887 | Michele | 596 | #N/A |
| 11/29/2018 | 3887 | Michele | 960 | 0 |
| 11/29/2018 | 3887 | Michele | 1158 | 0 |
| 9/21/2018 | 4919 | Paul | 857 | #N/A |
| 10/25/2018 | 4919 | Paul | 1162 | 34 |
| 10/26/2018 | 4919 | Paul | 726 | 1 |
| 10/26/2018 | 4919 | Paul | 726 | 0 |
When we look at the requisitioner 'Paul', the date diff between 9/21/2018 and 10/25/2018 is 34 and between that of 10/25/2018 and 10/26/2018 is 1 day. However, when I filter the data for date difference <=3 days, I miss out on 10/25/2018 because of 34 days difference. I have multiple such occurrences. How can I fix it?
I think you need to convert your date variable using as.Date(), then you can compute the lagged time difference using difftime().
# create toy data frame
df <- data.frame(date=as.Date(paste(sample(2018:2019,100,T),
sample(1:12,100,T),
sample(1:28,100,T),sep = '-')),
req=sample(letters[1:10],100,T),
amount=sample(100:10000,100,T))
# compute lagged time difference in days -- diff output is numeric
df %>% arrange(req,date) %>% group_by(req) %>%
mutate(diff=as.numeric(difftime(date,lag(date),units='days')))
# as above plus filtering based on time difference and amount
df %>% arrange(req,date) %>% group_by(req) %>%
mutate(diff=as.numeric(difftime(date,lag(date),units='days'))) %>%
filter(diff<10 | is.na(diff), amount>5000)
# A tibble: 8 x 4
# Groups: req [7]
date req amount diff
<date> <fct> <int> <dbl>
1 2018-05-13 a 9062 NA
2 2019-05-07 b 9946 2
3 2018-02-03 e 5697 NA
4 2018-03-12 g 7093 NA
5 2019-05-16 g 5631 3
6 2018-03-06 h 7114 6
7 2018-08-12 i 5151 6
8 2018-04-03 j 7738 8

SQL getting the smallest difference between max and min

I am trying to find the symbol of the smallest difference. But I don't know what to do answer finding the difference to compare the two.
I have this set:
+------+------+-------------+-------------+--------------------+------+--------+
| clid | cust | Min | Max | Difference | Qty | symbol |
+------+------+-------------+-------------+--------------------+------+--------+
| 102 | C6 | 11.8 | 12.72 | 0.9199999999999999 | 1500 | GE |
| 110 | C3 | 44 | 48.099998 | 4.099997999999999 | 2000 | INTC |
| 115 | C4 | 1755.25 | 1889.650024 | 134.40002400000003 | 2000 | AMZN |
| 121 | C9 | 28.25 | 30.27 | 2.0199999999999996 | 1500 | BAC |
| 130 | C7 | 8.48753 | 9.096588 | 0.609058000000001 | 5000 | F |
| 175 | C3 | 6.41 | 7.71 | 1.2999999999999998 | 1500 | SBS |
| 204 | C5 | 6.41 | 7.56 | 1.1499999999999995 | 5000 | SBS |
| 208 | C2 | 1782.170044 | 2004.359985 | 222.1899410000001 | 5000 | AMZN |
| 224 | C10 | 153.350006 | 162.429993 | 9.079986999999988 | 1500 | FB |
| 269 | C6 | 355.980011 | 392.299988 | 36.319976999999994 | 2000 | BA |
+------+------+-------------+-------------+--------------------+------+--------+
so far I have this Query
select d.clid,
d.cust,
MIN(f.fillPx) as Min,
MAX(f.fillPx) as Max,
MAX(f.fillPx)-MIN(f.fillPx) as Difference,
d.Qty,
d.symbol
from orders d
inner join mp f on d.clid=f.clid
group by f.clid
having SUM(f.fillQty) < d.Qty
order by d.clid;
What am I missing so that I can compare the min and max and get the smallest different symbol?
mp table:
+------+------+--------+------+------+---------+-------------+--------+
| clid | cust | symbol | side | oQty | fillQty | fillPx | execid |
+------+------+--------+------+------+---------+-------------+--------+
| 123 | C2 | SBS | SELL | 5000 | 273 | 7.37 | 1 |
| 157 | C9 | C | SELL | 1500 | 167 | 69.709999 | 2 |
| 254 | C9 | GE | SELL | 5000 | 440 | 13.28 | 3 |
| 208 | C2 | AMZN | SELL | 5000 | 714 | 1864.420044 | 4 |
| 102 | C6 | GE | SELL | 1500 | 136 | 12.32 | 5 |
| 160 | C7 | INTC | SELL | 1500 | 267 | 44.5 | 6 |
| 145 | C10 | GE | SELL | 5000 | 330 | 13.28 | 7 |
| 208 | C2 | AMZN | SELL | 5000 | 1190 | 1788.609985 | 8 |
| 161 | C1 | C | SELL | 1500 | 135 | 72.620003 | 9 |
| 181 | C5 | FCX | BUY | 1500 | 84 | 12.721739 | 10 |
orders table:
+------+------+--------+------+------+
| cust | side | symbol | qty | clid |
+------+------+--------+------+------+
| C1 | SELL | C | 1500 | 161 |
| C9 | SELL | INTC | 2000 | 231 |
| C10 | SELL | BMY | 1500 | 215 |
| C1 | BUY | SBS | 2000 | 243 |
| C4 | BUY | AMZN | 2000 | 226 |
| C10 | BUY | C | 1500 | 211 |
If you want one symbol, you can use order by and limit:
select d.clid,
d.cust,
MIN(f.fillPx) as Min,
MAX(f.fillPx) as Max,
MAX(f.fillPx)-MIN(f.fillPx) as Difference,
d.Qty,
d.symbol
from orders d join
mp f
on d.clid = f.clid
group by d.clid, d.cust, d.Qty, d.symbol
having SUM(f.fillQty) < d.Qty
order by difference
limit 1;
Notice that I added the rest of the unaggregated columns to the group by.

Optimizing an Oracle Query

I have created a query that usually takes 7 to 11 secs to return a set and I want to make this faster. I understand that I have to make use if indexes for for this to happen so I have properly made use of them. Unfortunately, it will give out the same execution time: Here is the query that I use:
WITH trackin8080 AS
(
SELECT DISTINCT
c.containerid
, c.containername
, hml.historyid
, hml.wiptrackinggroupkeyid
, hml.qty
, hml.txndate
FROM CONTAINER c
JOIN
(
SELECT
MAX(wwl.wiptrackinggroupkeyid) OVER (PARTITION BY wwl.containerid) wiptrackiggroupkeyid
, wwl.containerid
FROM
(
SELECT
wl.wiptrackinggroupkeyid
, wl.containerid
FROM a_wiplot wl
WHERE wl.specname IN ('8080_STAB_BAKE_1_FT')
AND wl.workflowname IN ('6_2B2R-DP_WIP') AND wl.containername LIKE :lotid AND wl.containername NOT LIKE '%TEST%'
UNION ALL
SELECT
wl.wiptrackinggroupkeyid
, wl.containerid
FROM a_wiplothistory wl
WHERE wl.specname IN ('8080_STAB_BAKE_1_FT')
AND wl.workflowname IN ('6_2B2R-DP_WIP') AND wl.containername LIKE :lotid AND wl.containername NOT LIKE '%TEST%'
)wwl
GROUP BY wwl.containerid, wwl.wiptrackinggroupkeyid
)wl ON c.containerid = wl.containerid
JOIN historymainline hml ON c.containerid = hml.historyid AND hml.cdoname = 'TrackInLot' AND hml.specname IN ('8080_STAB_BAKE_1_FT') AND wl.wiptrackiggroupkeyid = hml.wiptrackinggroupkeyid --AND hml.txndate > TRUNC(SYSDATE - 7)
WHERE hml.workflowname IN ('6_2B2R-DP_WIP') AND c.containername LIKE :lotid
), trackout8080 AS
(
SELECT DISTINCT
c.containerid
, c.containername
, hml.historyid
, hml.historymainlineid
, hml.txndate
, hml.qty
FROM trackin8080 ti
INNER JOIN container c ON ti.containerid = c.containerid
INNER JOIN product p ON c.productid = p.productid
INNER JOIN historymainline hml ON c.containerid = hml.historyid AND hml.cdoname = 'TrackOutLot' AND hml.specname IN ('8080_STAB_BAKE_1_FT')
WHERE hml.workflowname IN ('6_2B2R-DP_WIP') AND c.containername LIKE :lotid
), movein8527 AS
(
SELECT DISTINCT
c.containerid
, c.containername
, hml.historyid
, hml.historymainlineid
, hml.txndate
, hml.shiftname
, hml.employeeid
, hml.qty
, p.device
, p.brandname
, hml.specname
, hml.comments
FROM trackin8080 ti
INNER JOIN container c ON ti.containerid = c.containerid
INNER JOIN product p ON c.productid = p.productid
INNER JOIN historymainline hml ON c.containerid = hml.historyid
WHERE (hml.callbycdoname IN ('TestWIPMain', 'TestWIPMoveNonStd') AND hml.cdoname = 'MoveInLot' AND hml.specname IN ('8527_STAGE_BAKE_FT'))
AND hml.workflowname IN ('6_2B2R-DP_WIP') AND c.containername LIKE :lotid
), moveout8925 AS
(
SELECT DISTINCT
c.containerid
, c.containername
, hml.historyid
, hml.historymainlineid
, hml.txndate
, hml.shiftname
, hml.employeeid
, hml.qty
, p.device
, p.brandname
, hml.specname
, hml.comments
FROM trackin8080 ti
INNER JOIN container c ON ti.containerid = c.containerid
INNER JOIN product p ON c.productid = p.productid
INNER JOIN
(
SELECT
MAX(wwl.wiptrackinggroupkeyid) OVER (PARTITION BY wwl.containerid) wiptrackiggroupkeyid
, wwl.containerid
FROM
(
SELECT
wl.wiptrackinggroupkeyid
, wl.containerid
FROM a_wiplot wl
WHERE wl.specname IN ('8925_2ND_RM_TST_SOT_2_FT')
AND wl.workflowname IN ('6_2B2R-DP_WIP') AND wl.containername LIKE :lotid AND wl.containername NOT LIKE '%TEST%'
UNION ALL
SELECT
wl.wiptrackinggroupkeyid
, wl.containerid
FROM a_wiplothistory wl
WHERE wl.specname IN ('8925_2ND_RM_TST_SOT_2_FT')
AND wl.workflowname IN ('6_2B2R-DP_WIP') AND wl.containername LIKE :lotid AND wl.containername NOT LIKE '%TEST%'
)wwl
GROUP BY wwl.containerid, wwl.wiptrackinggroupkeyid
)wl ON c.containerid = wl.containerid
INNER JOIN historymainline hml ON c.containerid = hml.historyid AND wl.wiptrackiggroupkeyid = hml.wiptrackinggroupkeyid
WHERE (hml.callbycdoname IN ('LotMoveOut', 'MoveLot', 'TestWIPMoveNonStd') AND hml.cdoname = 'CreateFirstInsertion' AND hml.specname IN ('8925_2ND_RM_TST_SOT_2_FT'))
AND hml.workflowname IN ('6_2B2R-DP_WIP') AND c.containername LIKE :lotid
), trackin8090 AS
(
SELECT DISTINCT
c.containerid
, c.containername
, hml.historyid
, hml.wiptrackinggroupkeyid
, hml.qty
, hml.txndate
FROM CONTAINER c
JOIN
(
SELECT
MAX(wwl.wiptrackinggroupkeyid) OVER (PARTITION BY wwl.containerid) wiptrackiggroupkeyid
, wwl.containerid
FROM
(
SELECT
wl.wiptrackinggroupkeyid
, wl.containerid
FROM a_wiplot wl
WHERE wl.specname IN ('8090_2ND_BAKE_IN_OVEN_SOT_FT')
AND wl.workflowname IN ('6_2B2R-DP_WIP') AND wl.containername LIKE :lotid AND wl.containername NOT LIKE '%TEST%'
UNION ALL
SELECT
wl.wiptrackinggroupkeyid
, wl.containerid
FROM a_wiplothistory wl
WHERE wl.specname IN ('8090_2ND_BAKE_IN_OVEN_SOT_FT')
AND wl.workflowname IN ('6_2B2R-DP_WIP') AND wl.containername LIKE :lotid AND wl.containername NOT LIKE '%TEST%'
)wwl
GROUP BY wwl.containerid, wwl.wiptrackinggroupkeyid
)wl ON c.containerid = wl.containerid
JOIN historymainline hml ON c.containerid = hml.historyid AND hml.cdoname = 'TrackInLot' AND hml.specname IN ('8090_2ND_BAKE_IN_OVEN_SOT_FT') AND wl.wiptrackiggroupkeyid = hml.wiptrackinggroupkeyid --AND hml.txndate > TRUNC(SYSDATE - 7)
WHERE hml.workflowname IN ('6_2B2R-DP_WIP') AND c.containername LIKE :lotid
), trackout8090 AS
(
SELECT DISTINCT
c.containerid
, c.containername
, hml.historyid
, hml.historymainlineid
, hml.txndate
, hml.qty
FROM trackin8080 ti
INNER JOIN container c ON ti.containerid = c.containerid
INNER JOIN product p ON c.productid = p.productid
INNER JOIN historymainline hml ON c.containerid = hml.historyid AND hml.cdoname = 'TrackOutLot' AND hml.specname IN ('8090_2ND_BAKE_IN_OVEN_SOT_FT')
WHERE hml.workflowname IN ('6_2B2R-DP_WIP') AND c.containername LIKE :lotid
)
SELECT
ti8080.containername
, ti8080.qty qtyatb1
, to_char(ti8080.txndate, 'MM/DD/YYYY HH:MI:SS PM') trackindateb1
, to_char(ti8080.txndate + 12/24, 'MM/DD/YYYY HH:MI:SS PM') FCSTDOvenOutB1
, NVL(to_char(to8080.txndate, 'MM/DD/YYYY HH:MI:SS PM'), '-') trackoutdateb1
, CASE WHEN to8080.txndate IS NULL THEN
'-'
ELSE
to_char(ROUND((to8080.txndate - ti8080.txndate) * 24, 4))
END AS elapsedtimebake1
, NVL(to_char(mi8527.qty), '-') qtyatstaging
, NVL(to_char(mi8527.txndate, 'MM/DD/YYYY HH:MI:SS PM'), '-') moveinto8527
, NVL(to_char(mi8527.txndate + 24/24, 'MM/DD/YYYY HH:MI:SS PM'), '-') FCSTDEndDate
, NVL(to_char(mo8925.txndate, 'MM/DD/YYYY HH:MI:SS PM'), '-') moveoutto8925
, CASE WHEN mo8925.txndate IS NULL THEN
'-'
ELSE
to_char(ROUND((mo8925.txndate - mi8527.txndate) * 24, 4))
END AS elapsedtimestage
, NVL(to_char(ti8090.qty), '-') qtyatb2
, NVL(to_char(ti8090.txndate, 'MM/DD/YYYY HH:MI:SS PM'), '-') trackindateb2
, CASE WHEN ti8090.txndate IS NULL THEN
'-'
ELSE
to_char(ti8090.txndate + 24/24, 'MM/DD/YYYY HH:MI:SS PM')
END AS FCSTDOvenOutB2
, NVL(to_char(to8090.txndate, 'MM/DD/YYYY HH:MI:SS PM'), '-') trackoutdateb2
, CASE WHEN to8090.txndate IS NULL THEN
'-'
ELSE
to_char(ROUND((to8090.txndate - ti8090.txndate) * 24, 4))
END AS elapsedtimebake2
FROM trackin8080 ti8080
LEFT JOIN trackout8080 to8080 ON ti8080.containerid = to8080.containerid AND to8080.txndate >= ti8080.txndate
LEFT JOIN movein8527 mi8527 ON ti8080.containerid = mi8527.containerid AND mi8527.txndate >= to8080.txndate
LEFT JOIN moveout8925 mo8925 ON ti8080.containerid = mo8925.containerid AND mo8925.txndate >= mi8527.txndate
LEFT JOIN trackin8090 ti8090 ON ti8080.containerid = ti8090.containerid AND ti8090.txndate >= mi8527.txndate
LEFT JOIN trackout8090 to8090 ON ti8080.containerid = to8090.containerid AND to8090.txndate >= ti8090.txndate
WHERE ti8080.containername LIKE :lotid AND ti8080.containername NOT LIKE '%TEST%' AND trunc(ti8080.txndate) > TRUNC(SYSDATE-7)
ORDER BY ti8080.containername
And the explain plan:
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 149 | 51140 (1)| 00:10:14 |
| 1 | TEMP TABLE TRANSFORMATION | | | | | |
| 2 | LOAD AS SELECT | SYS_TEMP_0FD9DE946_81931B5A | | | | |
| 3 | HASH UNIQUE | | 1 | 98 | 17000 (1)| 00:03:25 |
| 4 | VIEW | | 1 | 98 | 16999 (1)| 00:03:24 |
| 5 | NESTED LOOPS | | | | | |
| 6 | NESTED LOOPS | | 1 | 119 | 16999 (1)| 00:03:24 |
| 7 | VIEW | | 1 | 47 | 16964 (1)| 00:03:24 |
| 8 | NESTED LOOPS | | | | | |
| 9 | NESTED LOOPS | | 1 | 64 | 16964 (1)| 00:03:24 |
| 10 | VIEW | | 1 | 35 | 16962 (1)| 00:03:24 |
| 11 | WINDOW BUFFER | | 1 | 34 | 16962 (1)| 00:03:24 |
| 12 | SORT GROUP BY | | 1 | 34 | 16962 (1)| 00:03:24 |
| 13 | VIEW | | 2 | 68 | 16961 (1)| 00:03:24 |
| 14 | UNION-ALL | | | | | |
|* 15 | TABLE ACCESS FULL | A_WIPLOT | 1 | 87 | 274 (1)| 00:00:04 |
|* 16 | TABLE ACCESS FULL | A_WIPLOTHISTORY | 1 | 81 | 16688 (1)| 00:03:21 |
|* 17 | INDEX UNIQUE SCAN | CONTAINER450 | 1 | | 1 (0)| 00:00:01 |
|* 18 | TABLE ACCESS BY INDEX ROWID | CONTAINER | 1 | 29 | 2 (0)| 00:00:01 |
|* 19 | INDEX RANGE SCAN | HISTORYMAINLINE2 | 46 | | 3 (0)| 00:00:01 |
|* 20 | TABLE ACCESS BY INDEX ROWID | HISTORYMAINLINE | 1 | 72 | 36 (0)| 00:00:01 |
| 21 | SORT ORDER BY | | 1 | 149 | 34140 (1)| 00:06:50 |
|* 22 | HASH JOIN OUTER | | 1 | 149 | 34139 (1)| 00:06:50 |
| 23 | VIEW | | 1 | 123 | 34096 (1)| 00:06:50 |
|* 24 | HASH JOIN OUTER | | 1 | 160 | 34096 (1)| 00:06:50 |
|* 25 | HASH JOIN OUTER | | 1 | 120 | 17094 (1)| 00:03:26 |
| 26 | VIEW | | 1 | 93 | 88 (4)| 00:00:02 |
|* 27 | HASH JOIN OUTER | | 1 | 111 | 88 (4)| 00:00:02 |
| 28 | VIEW | | 1 | 71 | 45 (5)| 00:00:01 |
|* 29 | HASH JOIN OUTER | | 1 | 89 | 45 (5)| 00:00:01 |
|* 30 | VIEW | | 1 | 62 | 2 (0)| 00:00:01 |
| 31 | TABLE ACCESS FULL | SYS_TEMP_0FD9DE946_81931B5A | 1 | 66 | 2 (0)| 00:00:01 |
| 32 | VIEW | | 1 | 27 | 42 (3)| 00:00:01 |
| 33 | HASH UNIQUE | | 1 | 161 | 42 (3)| 00:00:01 |
| 34 | NESTED LOOPS | | | | | |
| 35 | NESTED LOOPS | | 1 | 161 | 41 (0)| 00:00:01 |
| 36 | NESTED LOOPS | | 1 | 80 | 5 (0)| 00:00:01 |
| 37 | NESTED LOOPS | | 1 | 63 | 4 (0)| 00:00:01 |
| 38 | VIEW | | 1 | 17 | 2 (0)| 00:00:01 |
| 39 | TABLE ACCESS FULL | SYS_TEMP_0FD9DE946_81931B5A | 1 | 66 | 2 (0)| 00:00:01 |
|* 40 | TABLE ACCESS BY INDEX ROWID| CONTAINER | 1 | 46 | 2 (0)| 00:00:01 |
|* 41 | INDEX UNIQUE SCAN | CONTAINER450 | 1 | | 1 (0)| 00:00:01 |
|* 42 | INDEX UNIQUE SCAN | PRODUCT502 | 1 | 17 | 1 (0)| 00:00:01 |
|* 43 | INDEX RANGE SCAN | HISTORYMAINLINE2 | 46 | | 3 (0)| 00:00:01 |
|* 44 | TABLE ACCESS BY INDEX ROWID | HISTORYMAINLINE | 1 | 81 | 36 (0)| 00:00:01 |
| 45 | VIEW | | 1 | 40 | 43 (3)| 00:00:01 |
| 46 | HASH UNIQUE | | 1 | 228 | 43 (3)| 00:00:01 |
| 47 | NESTED LOOPS | | | | | |
| 48 | NESTED LOOPS | | 1 | 228 | 42 (0)| 00:00:01 |
| 49 | NESTED LOOPS | | 1 | 99 | 6 (0)| 00:00:01 |
| 50 | NESTED LOOPS | | 1 | 63 | 4 (0)| 00:00:01 |
| 51 | VIEW | | 1 | 17 | 2 (0)| 00:00:01 |
| 52 | TABLE ACCESS FULL | SYS_TEMP_0FD9DE946_81931B5A | 1 | 66 | 2 (0)| 00:00:01 |
|* 53 | TABLE ACCESS BY INDEX ROWID | CONTAINER | 1 | 46 | 2 (0)| 00:00:01 |
|* 54 | INDEX UNIQUE SCAN | CONTAINER450 | 1 | | 1 (0)| 00:00:01 |
| 55 | TABLE ACCESS BY INDEX ROWID | PRODUCT | 1 | 36 | 2 (0)| 00:00:01 |
|* 56 | INDEX UNIQUE SCAN | PRODUCT502 | 1 | | 1 (0)| 00:00:01 |
|* 57 | INDEX RANGE SCAN | HISTORYMAINLINE2 | 46 | | 3 (0)| 00:00:01 |
|* 58 | TABLE ACCESS BY INDEX ROWID | HISTORYMAINLINE | 1 | 129 | 36 (0)| 00:00:01 |
| 59 | VIEW | | 1 | 27 | 17006 (1)| 00:03:25 |
| 60 | HASH UNIQUE | | 1 | 271 | 17006 (1)| 00:03:25 |
|* 61 | HASH JOIN | | 1 | 271 | 17005 (1)| 00:03:25 |
| 62 | NESTED LOOPS | | | | | |
| 63 | NESTED LOOPS | | 1 | 236 | 42 (0)| 00:00:01 |
| 64 | NESTED LOOPS | | 1 | 99 | 6 (0)| 00:00:01 |
| 65 | NESTED LOOPS | | 1 | 63 | 4 (0)| 00:00:01 |
| 66 | VIEW | | 1 | 17 | 2 (0)| 00:00:01 |
| 67 | TABLE ACCESS FULL | SYS_TEMP_0FD9DE946_81931B5A | 1 | 66 | 2 (0)| 00:00:01 |
|* 68 | TABLE ACCESS BY INDEX ROWID | CONTAINER | 1 | 46 | 2 (0)| 00:00:01 |
|* 69 | INDEX UNIQUE SCAN | CONTAINER450 | 1 | | 1 (0)| 00:00:01 |
| 70 | TABLE ACCESS BY INDEX ROWID | PRODUCT | 1 | 36 | 2 (0)| 00:00:01 |
|* 71 | INDEX UNIQUE SCAN | PRODUCT502 | 1 | | 1 (0)| 00:00:01 |
|* 72 | INDEX RANGE SCAN | HISTORYMAINLINE2 | 46 | | 3 (0)| 00:00:01 |
|* 73 | TABLE ACCESS BY INDEX ROWID | HISTORYMAINLINE | 1 | 137 | 36 (0)| 00:00:01 |
| 74 | VIEW | | 1 | 35 | 16962 (1)| 00:03:24 |
| 75 | WINDOW BUFFER | | 1 | 34 | 16962 (1)| 00:03:24 |
| 76 | SORT GROUP BY | | 1 | 34 | 16962 (1)| 00:03:24 |
| 77 | VIEW | | 2 | 68 | 16961 (1)| 00:03:24 |
| 78 | UNION-ALL | | | | | |
|* 79 | TABLE ACCESS FULL | A_WIPLOT | 1 | 87 | 274 (1)| 00:00:04 |
|* 80 | TABLE ACCESS FULL | A_WIPLOTHISTORY | 1 | 81 | 16688 (1)| 00:03:21 |
| 81 | VIEW | | 1 | 40 | 17001 (1)| 00:03:25 |
| 82 | HASH UNIQUE | | 1 | 136 | 17001 (1)| 00:03:25 |
| 83 | NESTED LOOPS | | | | | |
| 84 | NESTED LOOPS | | 1 | 136 | 17000 (1)| 00:03:25 |
| 85 | NESTED LOOPS | | 1 | 64 | 16964 (1)| 00:03:24 |
| 86 | VIEW | | 1 | 35 | 16962 (1)| 00:03:24 |
| 87 | WINDOW BUFFER | | 1 | 34 | 16962 (1)| 00:03:24 |
| 88 | SORT GROUP BY | | 1 | 34 | 16962 (1)| 00:03:24 |
| 89 | VIEW | | 2 | 68 | 16961 (1)| 00:03:24 |
| 90 | UNION-ALL | | | | | |
|* 91 | TABLE ACCESS FULL | A_WIPLOT | 1 | 87 | 274 (1)| 00:00:04 |
|* 92 | TABLE ACCESS FULL | A_WIPLOTHISTORY | 1 | 81 | 16688 (1)| 00:03:21 |
|* 93 | TABLE ACCESS BY INDEX ROWID | CONTAINER | 1 | 29 | 2 (0)| 00:00:01 |
|* 94 | INDEX UNIQUE SCAN | CONTAINER450 | 1 | | 1 (0)| 00:00:01 |
|* 95 | INDEX RANGE SCAN | HISTORYMAINLINE2 | 46 | | 3 (0)| 00:00:01 |
|* 96 | TABLE ACCESS BY INDEX ROWID | HISTORYMAINLINE | 1 | 72 | 36 (0)| 00:00:01 |
| 97 | VIEW | | 1 | 26 | 42 (3)| 00:00:01 |
| 98 | HASH UNIQUE | | 1 | 161 | 42 (3)| 00:00:01 |
| 99 | NESTED LOOPS | | | | | |
| 100 | NESTED LOOPS | | 1 | 161 | 41 (0)| 00:00:01 |
| 101 | NESTED LOOPS | | 1 | 80 | 5 (0)| 00:00:01 |
| 102 | NESTED LOOPS | | 1 | 63 | 4 (0)| 00:00:01 |
| 103 | VIEW | | 1 | 17 | 2 (0)| 00:00:01 |
| 104 | TABLE ACCESS FULL | SYS_TEMP_0FD9DE946_81931B5A | 1 | 66 | 2 (0)| 00:00:01 |
|*105 | TABLE ACCESS BY INDEX ROWID | CONTAINER | 1 | 46 | 2 (0)| 00:00:01 |
|*106 | INDEX UNIQUE SCAN | CONTAINER450 | 1 | | 1 (0)| 00:00:01 |
|*107 | INDEX UNIQUE SCAN | PRODUCT502 | 1 | 17 | 1 (0)| 00:00:01 |
|*108 | INDEX RANGE SCAN | HISTORYMAINLINE2 | 46 | | 3 (0)| 00:00:01 |
|*109 | TABLE ACCESS BY INDEX ROWID | HISTORYMAINLINE | 1 | 81 | 36 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
15 - filter("WL"."WORKFLOWNAME"='6_2B2R-DP_WIP' AND "WL"."SPECNAME"='8080_STAB_BAKE_1_FT' AND
"WL"."CONTAINERNAME" LIKE :LOTID AND "WL"."CONTAINERNAME" NOT LIKE '%TEST%' AND "WL"."CONTAINERNAME" IS NOT NULL)
16 - filter("WL"."WORKFLOWNAME"='6_2B2R-DP_WIP' AND "WL"."SPECNAME"='8080_STAB_BAKE_1_FT' AND
"WL"."CONTAINERNAME" LIKE :LOTID AND "WL"."CONTAINERNAME" NOT LIKE '%TEST%' AND "WL"."CONTAINERNAME" IS NOT NULL)
17 - access("C"."CONTAINERID"="WL"."CONTAINERID")
18 - filter("C"."CONTAINERNAME" LIKE :LOTID)
19 - access("C"."CONTAINERID"="HML"."HISTORYID")
20 - filter("HML"."WIPTRACKINGGROUPKEYID" IS NOT NULL AND "HML"."WORKFLOWNAME"='6_2B2R-DP_WIP' AND
"HML"."SPECNAME"='8080_STAB_BAKE_1_FT' AND "HML"."CDONAME"='TrackInLot' AND
"WL"."WIPTRACKIGGROUPKEYID"="HML"."WIPTRACKINGGROUPKEYID")
22 - access("TI8080"."CONTAINERID"="TO8090"."CONTAINERID"(+))
filter("TO8090"."TXNDATE"(+)>="TI8090"."TXNDATE")
24 - access("from$_subquery$_054"."QCSJ_C000000005200000"="TI8090"."CONTAINERID"(+))
filter("TI8090"."TXNDATE"(+)>="from$_subquery$_054"."TXNDATE")
25 - access("TI8080"."CONTAINERID"="MO8925"."CONTAINERID"(+))
filter("MO8925"."TXNDATE"(+)>="MI8527"."TXNDATE")
27 - access("TI8080"."CONTAINERID"="MI8527"."CONTAINERID"(+))
filter("MI8527"."TXNDATE"(+)>="TO8080"."TXNDATE")
29 - access("TI8080"."CONTAINERID"="TO8080"."CONTAINERID"(+))
filter("TO8080"."TXNDATE"(+)>="TI8080"."TXNDATE")
30 - filter("TI8080"."CONTAINERNAME" LIKE :LOTID AND "TI8080"."CONTAINERNAME" NOT LIKE '%TEST%' AND
"TI8080"."CONTAINERNAME" IS NOT NULL AND TRUNC(INTERNAL_FUNCTION("TI8080"."TXNDATE"))>TRUNC(SYSDATE#!-7))
40 - filter("C"."CONTAINERNAME" LIKE :LOTID)
41 - access("TI"."CONTAINERID"="C"."CONTAINERID")
42 - access("C"."PRODUCTID"="P"."PRODUCTID")
43 - access("C"."CONTAINERID"="HML"."HISTORYID")
44 - filter("HML"."WORKFLOWNAME"='6_2B2R-DP_WIP' AND "HML"."SPECNAME"='8080_STAB_BAKE_1_FT' AND
"HML"."CDONAME"='TrackOutLot')
53 - filter("C"."CONTAINERNAME" LIKE :LOTID)
54 - access("TI"."CONTAINERID"="C"."CONTAINERID")
56 - access("C"."PRODUCTID"="P"."PRODUCTID")
57 - access("C"."CONTAINERID"="HML"."HISTORYID")
58 - filter("HML"."WORKFLOWNAME"='6_2B2R-DP_WIP' AND "HML"."SPECNAME"='8527_STAGE_BAKE_FT' AND
"HML"."CDONAME"='MoveInLot' AND ("HML"."CALLBYCDONAME"='TestWIPMain' OR "HML"."CALLBYCDONAME"='TestWIPMoveNonStd'))
61 - access("WL"."WIPTRACKIGGROUPKEYID"="HML"."WIPTRACKINGGROUPKEYID" AND "C"."CONTAINERID"="WL"."CONTAINERID")
68 - filter("C"."CONTAINERNAME" LIKE :LOTID)
69 - access("TI"."CONTAINERID"="C"."CONTAINERID")
71 - access("C"."PRODUCTID"="P"."PRODUCTID")
72 - access("C"."CONTAINERID"="HML"."HISTORYID")
73 - filter("HML"."WIPTRACKINGGROUPKEYID" IS NOT NULL AND "HML"."WORKFLOWNAME"='6_2B2R-DP_WIP' AND
"HML"."SPECNAME"='8925_2ND_RM_TST_SOT_2_FT' AND "HML"."CDONAME"='CreateFirstInsertion' AND
("HML"."CALLBYCDONAME"='LotMoveOut' OR "HML"."CALLBYCDONAME"='MoveLot' OR
"HML"."CALLBYCDONAME"='TestWIPMoveNonStd'))
79 - filter("WL"."WORKFLOWNAME"='6_2B2R-DP_WIP' AND "WL"."SPECNAME"='8925_2ND_RM_TST_SOT_2_FT' AND
"WL"."CONTAINERNAME" LIKE :LOTID AND "WL"."CONTAINERNAME" NOT LIKE '%TEST%' AND "WL"."CONTAINERNAME" IS NOT NULL)
80 - filter("WL"."WORKFLOWNAME"='6_2B2R-DP_WIP' AND "WL"."SPECNAME"='8925_2ND_RM_TST_SOT_2_FT' AND
"WL"."CONTAINERNAME" LIKE :LOTID AND "WL"."CONTAINERNAME" NOT LIKE '%TEST%' AND "WL"."CONTAINERNAME" IS NOT NULL)
91 - filter("WL"."WORKFLOWNAME"='6_2B2R-DP_WIP' AND "WL"."SPECNAME"='8090_2ND_BAKE_IN_OVEN_SOT_FT' AND
"WL"."CONTAINERNAME" LIKE :LOTID AND "WL"."CONTAINERNAME" NOT LIKE '%TEST%' AND "WL"."CONTAINERNAME" IS NOT NULL)
92 - filter("WL"."WORKFLOWNAME"='6_2B2R-DP_WIP' AND "WL"."SPECNAME"='8090_2ND_BAKE_IN_OVEN_SOT_FT' AND
"WL"."CONTAINERNAME" LIKE :LOTID AND "WL"."CONTAINERNAME" NOT LIKE '%TEST%' AND "WL"."CONTAINERNAME" IS NOT NULL)
93 - filter("C"."CONTAINERNAME" LIKE :LOTID)
94 - access("C"."CONTAINERID"="WL"."CONTAINERID")
95 - access("C"."CONTAINERID"="HML"."HISTORYID")
96 - filter("HML"."WIPTRACKINGGROUPKEYID" IS NOT NULL AND "HML"."WORKFLOWNAME"='6_2B2R-DP_WIP' AND
"HML"."SPECNAME"='8090_2ND_BAKE_IN_OVEN_SOT_FT' AND "HML"."CDONAME"='TrackInLot' AND
"WL"."WIPTRACKIGGROUPKEYID"="HML"."WIPTRACKINGGROUPKEYID")
105 - filter("C"."CONTAINERNAME" LIKE :LOTID)
106 - access("TI"."CONTAINERID"="C"."CONTAINERID")
107 - access("C"."PRODUCTID"="P"."PRODUCTID")
108 - access("C"."CONTAINERID"="HML"."HISTORYID")
109 - filter("HML"."WORKFLOWNAME"='6_2B2R-DP_WIP' AND "HML"."SPECNAME"='8090_2ND_BAKE_IN_OVEN_SOT_FT' AND
"HML"."CDONAME"='TrackOutLot')
It looks like nearly all of the table have only one row. In that case a full table scan would be faster. You can achieve this by specifying the FULL hint.

Resources