-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.sql
More file actions
40 lines (38 loc) · 1014 Bytes
/
analysis.sql
File metadata and controls
40 lines (38 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
with _base as (
select * from
my_table
), base as (
SELECT
"order id" as order_id,
string_agg(distinct "sku") as sku,
string_agg(distinct "type") as types,
max("quantity") as quantity,
sum(cast(replace("total",',','') as DOUBLE)) as sum_total,
FROM _base
WHERE "order id" is not null
GROUP BY "order id"
order by "order id"
), base2 as (
select
order_id,
sku,
types,
quantity,
sum_total,
case when types like '%Refund%' then 0 else quantity end as real_quantity,
from base
)
select
base2.sku,
sum(sum_total) as total_paid_by_amazon,
sum(real_quantity) as orders,
sum(quantity)-sum(real_quantity) as returns,
CASE
WHEN SUM(real_quantity) = 0 THEN 0 -- Handle division by zero
ELSE SUM(sum_total) / SUM(real_quantity)
END AS average_per_pc,
name_sku.PP
from base2
left join name_sku
on base2.sku = name_sku.sku
group by base2.sku, name_sku.PP