select week(cd.date_time,4) as week, year(cd.date_time) as year, p.product_name as product_name, p.asin as asin,
round(avg(cd.bsr_lv1)) as bsr_lv1, round(avg(cd.bsr_lv2)) as bsr_lv2, cd.stock_status as stock_status, COALESCE(NULLIF(round(avg(cd.price)),''),NULLIF(round(avg(cd.sale_price)),'' ),NULLIF(round(avg(cd.deal_price)),''), 'OOS') as price, round(avg(cd.review_total)) as reviews, round(avg(cd.rating),1) as rating,
(case when (round(sum(sr.unit_sold)) in ('','N/A',NULL)) THEN 0 ELSE round(sum(sr.unit_sold)) END) as unit_sold
from crawl_data cd
left join products p on p.id=cd.product_id
left join sale_report sr on sr.product_id=cd.product_id
where (date(cd.date_time) BETWEEN CURRENT_DATE - INTERVAL '6' WEEK AND CURRENT_DATE) and
p.asin='B07H66KQ1B' and week(cd.date_time,4)=week(sr.date_time,4)
group by week(cd.date_time,4), year(cd.date_time)
上面的查询产生了unit_sold的错误总和。
这里有人可以帮助我查询吗?我会非常感激。
答案 0 :(得分:0)
问题出在案例陈述中。
(case when (round(sum(sr.unit_sold)) in ('','N/A',NULL)) THEN 0 ELSE round(sum(sr.unit_sold)) END) as unit_sold.
整数和和应该是最外面的函数。案例说明应检查单个列,而不是汇总和四舍五入。
答案 1 :(得分:-1)
这是您的查询:
select week(cd.date_time, 4) as week,
year(cd.date_time) as year,
p.product_name as product_name,
p.asin as asin,
round(avg(cd.bsr_lv1)) as bsr_lv1,
round(avg(cd.bsr_lv2)) as bsr_lv2,
cd.stock_status as stock_status,
coalesce(nullif(round(avg(cd.price)), ''),
nullif(round(avg(cd.sale_price)), '' ),
nullif(round(avg(cd.deal_price)), ''),
'OOS'
) as price,
round(avg(cd.review_total)) as reviews,
round(avg(cd.rating), 1) as rating,
(case when (round(sum(sr.unit_sold)) in ('', 'N/A', NULL)) then 0 else round(sum(sr.unit_sold)) end) as unit_sold
from crawl_data cd left join
products p
on p.id = cd.product_id left join
sale_report sr
on sr.product_id = cd.product_id
where date(cd.date_time) BETWEEN CURRENT_DATE - INTERVAL '6' WEEK AND CURRENT_DATE and
p.asin = 'B07H66KQ1B' and
week(cd.date_time, 4) = week(sr.date_time, 4)
group by week(cd.date_time, 4), year(cd.date_time);
它有几个问题:
left join
。 WHERE
子句将其转换为内部联接。select
中有多个列,group by
中没有。 。 。例如product_name
,asin
和stock_status
。price
的计算是没有意义的,因为它是将数字和字符串组合在一起的。units_sold
的计算没有意义,因为它正在将数字函数的输出与字符串进行比较。IN (. . . NULL)
是没有用的。它将永远不会返回true
。鉴于所有这些问题,没有任何真正的解释,我只能推测一个有用的查询是什么。以下是我的最佳猜测:
select week(cd.date_time, 4) as week,
year(cd.date_time) as year,
p.product_name as product_name,
p.asin as asin,
round(avg(cd.bsr_lv1)) as bsr_lv1,
round(avg(cd.bsr_lv2)) as bsr_lv2,
cd.stock_status as stock_status,
coalesce(nullif(round(avg(cd.price)), 0),
nullif(round(avg(cd.sale_price)), 0),
nullif(round(avg(cd.deal_price)),
) as price, -- just return `NULL` if there is no price
round(avg(cd.review_total)) as reviews,
round(avg(cd.rating), 1) as rating,
round(sum(case when sr.unit_sold in ('', 'N/A') then 0 else 0 + sr.unit_sold end), 1) as unit_sold -- is units_sold really a string???
from crawl_data cd join
products p
on p.id = cd.product_id left join -- this could probably be an inner join as well
sale_report sr
on sr.product_id = cd.product_id
where date(cd.date_time) BETWEEN CURRENT_DATE - INTERVAL '6' WEEK AND CURRENT_DATE and
p.asin = 'B07H66KQ1B' and
week(cd.date_time, 4) = week(sr.date_time, 4)
group by week(cd.date_time, 4), year(cd.date_time),
p.product_name, p.asin, cd.stock_status;