sql item_map

 with 
 /*
 SELECT 2 as lev, 1.0 as power
 union all select     3,1.9,
 union all select     4,2.5, ...
*/
-- lev_info 这段sql 等价于上面,另一种实现而已
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, 

-- 这里是对兵的等级进行分类,黑道中,最高是11级兵
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` 

--提取最近7天的数据,然后对兵的等级进行聚合,统计频率
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(

                            --        连接字符串        切割字符串                这个mergeid 就是兵的种类 1 是暴徒,二是飞车党
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`),

-- 统计聚合每个table id 在七天之内的数量
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_info 相交
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


posted @ 2022-08-19 22:50  luoganttcc  阅读(6)  评论(0)    收藏  举报