
-- 按 topic 分组 + 合计
SELECT dt, COALESCE(topic, '合计') as topic,
       count(distinct machinecode) as uv, sum(1) as cnt
FROM loghubods.user_share_log
WHERE dt = '${dt}'
GROUP BY dt, topic
GROUPING SETS ((dt, topic), (dt))

UNION ALL

-- click 再分: from_mid 与 target_mid 是否相同
SELECT dt,
       CASE WHEN split(shareid, '-')[0] = machinecode
            THEN 'click_自点' ELSE 'click_他点' END as topic,
       count(distinct machinecode) as uv, sum(1) as cnt
FROM loghubods.user_share_log
WHERE dt = '${dt}' AND topic = 'click'
GROUP BY dt,
       CASE WHEN split(shareid, '-')[0] = machinecode
            THEN 'click_自点' ELSE 'click_他点' END
