sql item_map
生活随笔
收集整理的這篇文章主要介紹了
sql item_map
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
with /*SELECT 2 as lev, 1.0 as powerunion 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
總結
以上是生活随笔為你收集整理的sql item_map的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sql substr切割字符串
- 下一篇: 数据预处理 参考sql