| use dataverse tpch; |
| |
| for $locn in ( |
| for $l in dataset('LineItem') |
| for $ocn in ( |
| for $o in dataset('Orders') |
| for $c in dataset('Customer') |
| where $c.c_custkey = $o.o_custkey and $o.o_orderdate >= '1993-10-01' |
| and $o.o_orderdate < '1994-01-01' |
| for $n in dataset('Nation') |
| where $c.c_nationkey = $n.n_nationkey |
| return { |
| "c_custkey": $c.c_custkey, |
| "c_name": $c.c_name, |
| "c_acctbal": $c.c_acctbal, |
| "n_name": $n.n_name, |
| "c_address": $c.c_address, |
| "c_phone": $c.c_phone, |
| "c_comment": $c.c_comment, |
| "o_orderkey": $o.o_orderkey |
| } |
| ) |
| where $l.l_orderkey = $ocn.o_orderkey and $l.l_returnflag = 'R' |
| return { |
| "c_custkey": $ocn.c_custkey, |
| "c_name": $ocn.c_name, |
| "c_acctbal": $ocn.c_acctbal, |
| "n_name": $ocn.n_name, |
| "c_address": $ocn.c_address, |
| "c_phone": $ocn.c_phone, |
| "c_comment": $ocn.c_comment, |
| "l_extendedprice": $l.l_extendedprice, |
| "l_discount": $l.l_discount |
| } |
| ) |
| group by $c_custkey:=$locn.c_custkey, |
| $c_name:=$locn.c_name, |
| $c_acctbal:=$locn.c_acctbal, $c_phone:=$locn.c_phone, |
| $n_name:=$locn.n_name, $c_address:=$locn.c_address, $c_comment:=$locn.c_comment |
| with $locn |
| let $revenue := sum(for $i in $locn return $i.l_extendedprice * (1 - $i.l_discount)) |
| order by $revenue desc |
| limit 20 |
| return { |
| "c_custkey": $c_custkey, |
| "c_name": $c_name, |
| "revenue": $revenue, |
| "c_acctbal": $c_acctbal, |
| "n_name": $n_name, |
| "c_address": $c_address, |
| "c_phone": $c_phone, |
| "c_comment": $c_comment |
| } |
| |
| |