| /* |
| * 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 dataverse tpch; |
| |
| declare function q22_customer_tmp() { |
| for $c in dataset('Customer') |
| let $phone_substr := substring($c.c_phone, 1, 2) |
| where $phone_substr = '13' |
| or $phone_substr = '31' |
| or $phone_substr = '23' |
| or $phone_substr = '29' |
| or $phone_substr = '30' |
| or $phone_substr = '18' |
| or $phone_substr = '17' |
| return { |
| "c_acctbal": $c.c_acctbal, |
| "c_custkey": $c.c_custkey, |
| "cntrycode": $phone_substr |
| } |
| } |
| |
| let $avg := avg( |
| for $c in q22_customer_tmp() |
| where $c.c_acctbal > 0.00 |
| return $c.c_acctbal |
| ) |
| for $ct in q22_customer_tmp() |
| where $ct.c_acctbal > $avg |
| group by $cntrycode := $ct.cntrycode with $ct |
| order by $cntrycode |
| return { |
| "cntrycode": $cntrycode, |
| "numcust": count($ct), |
| "totacctbal": sum(for $i in $ct return $i.c_acctbal) |
| } |