计算下一周的交易金额

时间:2019-08-06 06:00:12

标签: google-bigquery

交易->交易ID,买方ID,卖方ID,对象ID,发运ID,价格,数量,站点ID,交易日期,预期交货日期,结帐退出状态                             leaf_category_id,defect_id

买方->买方ID,名称,国家/地区

卖方->卖方ID,名称,国家/地区,细分,标准

列表-> object_id,seller_id,auction_start_date                Auction_end_date,listing_site_id,leaf_category_id                数量

对于在12月第二周(2015年12月6日至2015年12月12日)进行交易的英国卖家,请找到卖家数量 他们在下周的总交易金额(数量*价格)至少翻了一番。

我在下面的查询中尝试获取在12月2日进行交易但在计算下周交易量是该交易量两倍的卖方时遇到错误的卖方。

With trans_dec_uk as
(
select s.seller_id,t.transaction_date, sum(t.Qty * Price) trans_amount
from transaction t join seller s
on t.seller_id =s.seller_id
where s.country ='UK'
and t.transaction_date between '12-05-2015' and '12-18-2015'
group by s.seller_id,t.transaction_date
)
select count(seller) from  trans_dec_uk
where trans_amount =  2 * to_char(sysdate+7,'DD-MM')

1 个答案:

答案 0 :(得分:1)

with uk_sellers as (
 select * from <dataset>.Seller where country = 'UK'
),
first_week_uk as (
  select seller_id, sum(Price*Quantity) as first_week_total
  from <dataset>.Transaction
  inner join uk_sellers using(seller_id)
  where transaction_date between '2015-12-05' and '2015-12-11'
  group by 1
),
second_week_uk as (
  select seller_id, sum(Price*Quantity) as second_week_total
  from <dataset>.Transaction
  inner join uk_sellers using(seller_id)
  where transaction_date between '2015-12-12' and '2015-12-18'
  group by 1
)
select count(distinct seller_id) as the_answer
from first_week_uk
inner join second_week_uk using(seller_id)
where second_week_total >= 2*first_week_total