Created
February 15, 2017 13:12
-
-
Save filipre/351d12500ff908f2ec62122efe3af487 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
-- Correlated Query | |
select sum(l_extendedprice) / 7.0 as avg_yearly | |
from lineitem, part | |
where p_partkey = l_partkey | |
and p_brand = 'Brand#23' | |
and p_container = 'MED BOX' | |
and l_quantity < ( | |
select 0.2 * avg(l_quantity) from lineitem where l_partkey = p_partkey | |
) | |
*/ | |
with my_avg(avgForPartkey, avgValue) as ( | |
select l_partkey, 0.2 * avg(l_quantity) | |
from lineitem | |
group by l_partkey | |
) | |
select sum(l_extendedprice) / 7.0 as avg_yearly | |
from lineitem, part, my_avg | |
where p_brand = 'Brand#23' | |
and p_container = 'MED BOX' | |
and p_partkey = l_partkey | |
and l_quantity < my_avg.avgValue | |
and my_avg.avgForPartkey = p_partkey | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment