sql item_map

with 
lev_info as(SELECT * from UNNEST(ARRAY<STRUCT<lev INT64, power FLOAT64>>[(1, 1),
(2, 1.4), (3, 1.9), (4, 2.5), (5, 3.2), (6, 4), (7, 4.9), (8, 5.9), (9, 7), (10, 8.2), (11, 9.5)])),
army_info as (
select army_id, freq, 
CASE WHEN army_id < 12050001 then mod(army_id, 100) else DIV((mod(army_id, 100) - 1), 3) + 1 end as lev from(
select army_id, count(*) as freq from `mafia1_ods.game_log_army` 
where timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
group by army_id
order by freq desc)),
army_freq as(
select * except(freq), sum(freq) over(partition by merge_id) as freq from(
select army_info.army_id as table_id, CONCAT('army@', SUBSTR(CAST(army_id as STRING), 0, 4)) as merge_id, freq,
  lev_info.power as exchange_val, 1 as use_log from army_info left join lev_info on army_info.lev = lev_info.lev
)),

charge_item as(
select item_id from `recommend_algorithm.v_mafia1_charge_item`),

item_freq_count as(
select table_id as item_id, count(*) as freq from mafia1_ods.game_log_item
where timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY) and table_id in (select item_id from charge_item)
group by table_id),

item_map as(select t1.*, t2.* except(id) from(
select charge_item.item_id, IFNULL(item_freq_count.freq, 0) as freq from
charge_item left join item_freq_count on charge_item.item_id = item_freq_count.item_id) as t1
left join (select id, type, type_config, para1, para2, exchange_val from mafia1_dmd.item_info) as t2 on t1.item_id = t2.id),

item_freq as(
select * except(exchange_val, min_exchange, max_exchange), exchange_val/min_exchange as exchange_val, CAST(max_exchange/min_exchange >= 10 as int64) as use_log from(
select * except(freq), sum(freq) over(partition by merge_id) as freq, 
    min(exchange_val) over(partition by merge_id) as min_exchange,
    max(exchange_val) over(partition by merge_id) as max_exchange,from(
select item_id as table_id, CASE WHEN type in (2, 3) then format('item@%d@%d@%s', type, type_config, CASE WHEN type = 2 then CAST(para1 as STRING) else '' end) 
      else format('item@%d', item_id) end as merge_id, freq,  
      CAST(CASE WHEN type in (2, 3) then exchange_val else 1 end as int64) as exchange_val, from item_map))
order by freq desc)

select *, DENSE_RANK() over(order by freq desc, merge_id) as map_id from(
select * from army_freq union all select table_id, merge_id, exchange_val, use_log, freq from item_freq)
order by map_id
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值