github.com/apache/beam/sdks/v2@v2.48.2/java/testing/tpcds/src/main/resources/queries/query49.sql (about) 1 -- Licensed to the Apache Software Foundation (ASF) under one 2 -- or more contributor license agreements. See the NOTICE file 3 -- distributed with this work for additional information 4 -- regarding copyright ownership. The ASF licenses this file 5 -- to you under the Apache License, Version 2.0 (the 6 -- "License"); you may not use this file except in compliance 7 -- with the License. You may obtain a copy of the License at 8 -- 9 -- http://www.apache.org/licenses/LICENSE-2.0 10 -- 11 -- Unless required by applicable law or agreed to in writing, software 12 -- distributed under the License is distributed on an "AS IS" BASIS, 13 -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 -- See the License for the specific language governing permissions and 15 -- limitations under the License. 16 17 select channel, item, return_ratio, return_rank, currency_rank from 18 (select 19 'web' as channel 20 ,web.item 21 ,web.return_ratio 22 ,web.return_rank 23 ,web.currency_rank 24 from ( 25 select 26 item 27 ,return_ratio 28 ,currency_ratio 29 ,rank() over (order by return_ratio) as return_rank 30 ,rank() over (order by currency_ratio) as currency_rank 31 from 32 ( select ws.ws_item_sk as item 33 ,(cast(sum(coalesce(wr.wr_return_quantity,0)) as decimal(15,4))/ 34 cast(sum(coalesce(ws.ws_quantity,0)) as decimal(15,4) )) as return_ratio 35 ,(cast(sum(coalesce(wr.wr_return_amt,0)) as decimal(15,4))/ 36 cast(sum(coalesce(ws.ws_net_paid,0)) as decimal(15,4) )) as currency_ratio 37 from 38 web_sales ws left outer join web_returns wr 39 on (ws.ws_order_number = wr.wr_order_number and 40 ws.ws_item_sk = wr.wr_item_sk) 41 ,date_dim 42 where 43 wr.wr_return_amt > 10000 44 and ws.ws_net_profit > 1 45 and ws.ws_net_paid > 0 46 and ws.ws_quantity > 0 47 and ws_sold_date_sk = d_date_sk 48 and d_year = 2000 49 and d_moy = 12 50 group by ws.ws_item_sk 51 ) in_web 52 ) web 53 where 54 ( 55 web.return_rank <= 10 56 or 57 web.currency_rank <= 10 58 ) 59 union 60 select 61 'catalog' as channel 62 ,catalog.item 63 ,catalog.return_ratio 64 ,catalog.return_rank 65 ,catalog.currency_rank 66 from ( 67 select 68 item 69 ,return_ratio 70 ,currency_ratio 71 ,rank() over (order by return_ratio) as return_rank 72 ,rank() over (order by currency_ratio) as currency_rank 73 from 74 ( select 75 cs.cs_item_sk as item 76 ,(cast(sum(coalesce(cr.cr_return_quantity,0)) as decimal(15,4))/ 77 cast(sum(coalesce(cs.cs_quantity,0)) as decimal(15,4) )) as return_ratio 78 ,(cast(sum(coalesce(cr.cr_return_amount,0)) as decimal(15,4))/ 79 cast(sum(coalesce(cs.cs_net_paid,0)) as decimal(15,4) )) as currency_ratio 80 from 81 catalog_sales cs left outer join catalog_returns cr 82 on (cs.cs_order_number = cr.cr_order_number and 83 cs.cs_item_sk = cr.cr_item_sk) 84 ,date_dim 85 where 86 cr.cr_return_amount > 10000 87 and cs.cs_net_profit > 1 88 and cs.cs_net_paid > 0 89 and cs.cs_quantity > 0 90 and cs_sold_date_sk = d_date_sk 91 and d_year = 2000 92 and d_moy = 12 93 group by cs.cs_item_sk 94 ) in_cat 95 ) catalog 96 where 97 ( 98 catalog.return_rank <= 10 99 or 100 catalog.currency_rank <=10 101 ) 102 union 103 select 104 'store' as channel 105 ,store.item 106 ,store.return_ratio 107 ,store.return_rank 108 ,store.currency_rank 109 from ( 110 select 111 item 112 ,return_ratio 113 ,currency_ratio 114 ,rank() over (order by return_ratio) as return_rank 115 ,rank() over (order by currency_ratio) as currency_rank 116 from 117 ( select sts.ss_item_sk as item 118 ,(cast(sum(coalesce(sr.sr_return_quantity,0)) as decimal(15,4))/cast(sum(coalesce(sts.ss_quantity,0)) as decimal(15,4) )) as return_ratio 119 ,(cast(sum(coalesce(sr.sr_return_amt,0)) as decimal(15,4))/cast(sum(coalesce(sts.ss_net_paid,0)) as decimal(15,4) )) as currency_ratio 120 from 121 store_sales sts left outer join store_returns sr 122 on (sts.ss_ticket_number = sr.sr_ticket_number and sts.ss_item_sk = sr.sr_item_sk) 123 ,date_dim 124 where 125 sr.sr_return_amt > 10000 126 and sts.ss_net_profit > 1 127 and sts.ss_net_paid > 0 128 and sts.ss_quantity > 0 129 and ss_sold_date_sk = d_date_sk 130 and d_year = 2000 131 and d_moy = 12 132 group by sts.ss_item_sk 133 ) in_store 134 ) store 135 where ( 136 store.return_rank <= 10 137 or 138 store.currency_rank <= 10 139 ) 140 ) 141 order by 1,4,5,2 142 limit 100