@@ -1107,6 +1107,7 @@ tmp_fact AS MATERIALIZED (
11071107 ff .fulfillment_state ,
11081108 ff .account_state ,
11091109 ff .item_bucket ,
1110+ ff .line_key ,
11101111 ff .extended_amount ,
11111112 ff .customer_balance ,
11121113 ff .stock_quantity ,
@@ -1129,6 +1130,7 @@ tmp_customer_window AS (
11291130 tf .fulfillment_state ,
11301131 tf .account_state ,
11311132 tf .item_bucket ,
1133+ tf .line_key ,
11321134 tf .extended_amount ,
11331135 tf .customer_balance ,
11341136 tf .stock_quantity ,
@@ -1137,11 +1139,11 @@ tmp_customer_window AS (
11371139 tf .order_line_count ,
11381140 ROW_NUMBER() OVER (
11391141 PARTITION BY tf .warehouse_id , tf .district_id , tf .customer_id
1140- ORDER BY tf .extended_amount DESC , tf .order_id ASC , tf .line_number ASC
1142+ ORDER BY tf .extended_amount DESC , tf .order_id ASC , tf .line_number ASC , tf . item_id ASC , tf . line_key ASC
11411143 ) AS customer_line_rank,
1142- SUM (tf .extended_amount ) OVER (
1144+ CAST( SUM (CAST(ROUND( tf .extended_amount , 4 ) AS DECIMAL ( 38 , 4 )) ) OVER (
11431145 PARTITION BY tf .warehouse_id , tf .district_id , tf .customer_id
1144- ) AS customer_total_amount,
1146+ ) AS DOUBLE) AS customer_total_amount,
11451147 MAX (tf .stock_quantity ) OVER (
11461148 PARTITION BY tf .warehouse_id , tf .item_id
11471149 ) AS warehouse_item_peak
@@ -1155,12 +1157,13 @@ tmp_order_window AS (
11551157 tf .order_id ,
11561158 tf .line_number ,
11571159 tf .item_id ,
1160+ tf .line_key ,
11581161 COUNT (* ) OVER (
11591162 PARTITION BY tf .warehouse_id , tf .district_id , tf .order_id
11601163 ) AS order_rows_window,
1161- SUM (tf .extended_amount ) OVER (
1164+ CAST( SUM (CAST(ROUND( tf .extended_amount , 4 ) AS DECIMAL ( 38 , 4 )) ) OVER (
11621165 PARTITION BY tf .warehouse_id , tf .district_id , tf .order_id
1163- ) AS order_amount_window,
1166+ ) AS DOUBLE) AS order_amount_window,
11641167 MIN (tf .line_number ) OVER (
11651168 PARTITION BY tf .warehouse_id , tf .district_id , tf .order_id
11661169 ) AS first_line_window
@@ -1174,6 +1177,7 @@ tmp_stock_window AS (
11741177 tf .credit_code ,
11751178 tf .fulfillment_state ,
11761179 tf .account_state ,
1180+ tf .line_key ,
11771181 tf .stock_quantity ,
11781182 DENSE_RANK() OVER (
11791183 PARTITION BY tf .warehouse_id , tf .credit_code
@@ -1197,6 +1201,8 @@ inner_join_chain AS (
11971201 cw .fulfillment_state ,
11981202 cw .account_state ,
11991203 cw .item_bucket ,
1204+ cw .line_key ,
1205+ sw .line_key AS stock_line_key,
12001206 cw .extended_amount ,
12011207 cw .customer_balance ,
12021208 cw .stock_quantity ,
@@ -1218,6 +1224,7 @@ inner_join_chain AS (
12181224 AND ow .order_id = cw .order_id
12191225 AND ow .line_number = cw .line_number
12201226 AND ow .item_id = cw .item_id
1227+ AND ow .line_key = cw .line_key
12211228 INNER JOIN tmp_stock_window sw
12221229 ON sw .warehouse_id = cw .warehouse_id
12231230 AND sw .item_id = cw .item_id
@@ -1232,7 +1239,7 @@ credit_summary AS (
12321239 district_id,
12331240 credit_code,
12341241 COUNT (* ) AS credit_rows,
1235- SUM (extended_amount) AS credit_amount,
1242+ CAST( SUM (CAST(ROUND( extended_amount, 4 ) AS DECIMAL ( 38 , 4 ))) AS DOUBLE ) AS credit_amount,
12361243 MAX (customer_total_amount) AS max_customer_total_amount,
12371244 COUNT (* ) FILTER (WHERE payment_history_rows > 0 ) AS paid_history_rows
12381245 FROM inner_join_chain
@@ -1246,7 +1253,9 @@ fulfillment_summary AS (
12461253 fulfillment_state,
12471254 account_state,
12481255 COUNT (* ) AS fulfillment_rows,
1249- SUM (extended_amount) AS fulfillment_amount,
1256+ CAST(SUM (CAST(ROUND(extended_amount, 4 ) AS DECIMAL (38 , 4 ))) AS DOUBLE) AS fulfillment_amount,
1257+ MIN (line_key) AS summary_line_key,
1258+ MIN (line_key) AS summary_stock_line_key,
12501259 MAX (stock_quantity) AS max_fulfillment_stock_window
12511260 FROM tmp_fact
12521261 GROUP BY warehouse_id, district_id, fulfillment_state, account_state
@@ -1264,6 +1273,8 @@ left_join_chain AS (
12641273 ijc .fulfillment_state ,
12651274 ijc .account_state ,
12661275 ijc .item_bucket ,
1276+ ijc .line_key ,
1277+ ijc .stock_line_key ,
12671278 ijc .extended_amount ,
12681279 ijc .customer_balance ,
12691280 ijc .stock_quantity ,
@@ -1298,6 +1309,8 @@ full_outer_chain AS (
12981309 COALESCE(ljc .fulfillment_state , fs .fulfillment_state , ' NO_FULFILLMENT' ) AS fulfillment_state,
12991310 COALESCE(ljc .account_state , fs .account_state , ' NO_ACCOUNT' ) AS account_state,
13001311 COALESCE(ljc .item_bucket , ' NO_BUCKET' ) AS item_bucket,
1312+ COALESCE(ljc .line_key , fs .summary_line_key ) AS line_key,
1313+ COALESCE(ljc .stock_line_key , fs .summary_stock_line_key ) AS stock_line_key,
13011314 COALESCE(ljc .extended_amount , 0 ) AS extended_amount,
13021315 COALESCE(ljc .customer_balance , 0 ) AS customer_balance,
13031316 COALESCE(ljc .stock_quantity , 0 ) AS stock_quantity,
@@ -1336,6 +1349,8 @@ semi_join_chain AS (
13361349 foc .fulfillment_state ,
13371350 foc .account_state ,
13381351 foc .item_bucket ,
1352+ foc .line_key ,
1353+ foc .stock_line_key ,
13391354 foc .extended_amount ,
13401355 foc .customer_balance ,
13411356 foc .stock_quantity ,
@@ -1377,6 +1392,8 @@ anti_join_chain AS (
13771392 sjc .fulfillment_state ,
13781393 sjc .account_state ,
13791394 sjc .item_bucket ,
1395+ sjc .line_key ,
1396+ sjc .stock_line_key ,
13801397 sjc .extended_amount ,
13811398 sjc .customer_balance ,
13821399 sjc .stock_quantity ,
@@ -1419,6 +1436,8 @@ join_window_pass_1 AS (
14191436 ajc .fulfillment_state ,
14201437 ajc .account_state ,
14211438 ajc .item_bucket ,
1439+ ajc .line_key ,
1440+ ajc .stock_line_key ,
14221441 ajc .extended_amount ,
14231442 ajc .customer_balance ,
14241443 ajc .stock_quantity ,
@@ -1439,11 +1458,13 @@ join_window_pass_1 AS (
14391458 ajc .max_fulfillment_stock_window ,
14401459 ROW_NUMBER() OVER (
14411460 PARTITION BY ajc .warehouse_id , ajc .district_id
1442- ORDER BY ajc .extended_amount DESC , ajc .order_id ASC , ajc .line_number ASC
1461+ ORDER BY ajc .extended_amount DESC , ajc .order_id ASC , ajc .line_number ASC , ajc .item_id ASC ,
1462+ ajc .credit_code ASC , ajc .fulfillment_state ASC , ajc .account_state ASC , ajc .line_key ASC ,
1463+ ajc .stock_line_key ASC
14431464 ) AS chain_rank,
1444- SUM (ajc .extended_amount + ajc .credit_amount + ajc .fulfillment_amount ) OVER (
1465+ CAST( SUM (CAST(ROUND( ajc .extended_amount + ajc .credit_amount + ajc .fulfillment_amount , 4 ) AS DECIMAL ( 38 , 4 )) ) OVER (
14451466 PARTITION BY ajc .warehouse_id
1446- ) AS warehouse_chain_amount
1467+ ) AS DOUBLE) AS warehouse_chain_amount
14471468 FROM anti_join_chain ajc
14481469
14491470),
@@ -1459,6 +1480,8 @@ join_window_pass_2 AS (
14591480 jwp1 .fulfillment_state ,
14601481 jwp1 .account_state ,
14611482 jwp1 .item_bucket ,
1483+ jwp1 .line_key ,
1484+ jwp1 .stock_line_key ,
14621485 jwp1 .extended_amount ,
14631486 jwp1 .customer_balance ,
14641487 jwp1 .stock_quantity ,
@@ -1481,7 +1504,7 @@ join_window_pass_2 AS (
14811504 jwp1 .warehouse_chain_amount ,
14821505 DENSE_RANK() OVER (
14831506 PARTITION BY jwp1 .warehouse_id , jwp1 .credit_code
1484- ORDER BY jwp1 .warehouse_chain_amount DESC , jwp1 .district_id ASC
1507+ ORDER BY ROUND( jwp1 .warehouse_chain_amount , 4 ) DESC , jwp1 .district_id ASC
14851508 ) AS credit_chain_rank,
14861509 COUNT (* ) OVER (
14871510 PARTITION BY jwp1 .warehouse_id , jwp1 .fulfillment_state , jwp1 .account_state
@@ -1498,10 +1521,10 @@ join_chain_rollup AS (
14981521 account_state,
14991522 item_bucket,
15001523 COUNT (* ) AS chain_rows,
1501- SUM (extended_amount) AS chain_amount,
1502- SUM (credit_amount) AS chain_credit_amount,
1503- SUM (fulfillment_amount) AS chain_fulfillment_amount,
1504- MAX (warehouse_chain_amount) AS max_warehouse_chain_amount,
1524+ CAST( SUM (CAST(ROUND( extended_amount, 4 ) AS DECIMAL ( 38 , 4 ))) AS DOUBLE ) AS chain_amount,
1525+ CAST( SUM (CAST(ROUND( credit_amount, 4 ) AS DECIMAL ( 38 , 4 ))) AS DOUBLE ) AS chain_credit_amount,
1526+ CAST( SUM (CAST(ROUND( fulfillment_amount, 4 ) AS DECIMAL ( 38 , 4 ))) AS DOUBLE ) AS chain_fulfillment_amount,
1527+ CAST( MAX (CAST(ROUND( warehouse_chain_amount, 4 ) AS DECIMAL ( 38 , 4 ))) AS DOUBLE ) AS max_warehouse_chain_amount,
15051528 MAX (chain_rank) AS max_chain_rank,
15061529 MAX (credit_chain_rank) AS max_credit_chain_rank,
15071530 MAX (fulfillment_account_rows) AS max_fulfillment_account_rows,
0 commit comments