| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| |
| use tpch; |
| |
| |
| select element {'nation':nation,'o_year':o_year,'sum_profit':tpch.coll_sum(( |
| select element pr.amount |
| from profit as pr |
| ))} |
| from ( |
| select element {'nation':l3.n_name,'o_year':o_year,'amount':amount} |
| from Orders as o, |
| ( |
| select element {'l_extendedprice':l2.l_extendedprice,'l_discount':l2.l_discount,'l_quantity':l2.l_quantity,'l_orderkey':l2.l_orderkey,'n_name':l2.n_name,'ps_supplycost':l2.ps_supplycost} |
| from Part as p join |
| ( |
| select element {'l_extendedprice':l1.l_extendedprice,'l_discount':l1.l_discount,'l_quantity':l1.l_quantity,'l_partkey':l1.l_partkey,'l_orderkey':l1.l_orderkey,'n_name':l1.n_name,'ps_supplycost':ps.ps_supplycost} |
| from Partsupp as ps join |
| ( |
| select element {'l_suppkey':l.l_suppkey,'l_extendedprice':l.l_extendedprice,'l_discount':l.l_discount,'l_quantity':l.l_quantity,'l_partkey':l.l_partkey,'l_orderkey':l.l_orderkey,'n_name':s1.n_name} |
| from ( |
| select element {'s_suppkey':s.s_suppkey,'n_name':n.n_name} |
| from Supplier as s, |
| Nation as n |
| where (n.n_nationkey = s.s_nationkey) |
| ) as s1 join |
| LineItem as l |
| on (s1.s_suppkey = l.l_suppkey) |
| ) as l1 |
| on ((ps.ps_suppkey = l1.l_suppkey) and (ps.ps_partkey = l1.l_partkey)) |
| ) as l2 |
| on (tpch.contains(p.p_name,'green') and (p.p_partkey = l2.l_partkey)) |
| ) as l3 |
| with amount as ((l3.l_extendedprice * (1 - l3.l_discount)) - (l3.ps_supplycost * l3.l_quantity)), |
| o_year as tpch.`get-year`(o.o_orderdate) |
| where (o.o_orderkey = l3.l_orderkey) |
| ) as profit |
| group by profit.nation as nation,profit.o_year as o_year |
| order by nation,o_year desc |
| ; |