JUST DO IT!

SQL ํ•™์Šต - JOIN (TIL 230511) ๋ณธ๋ฌธ

TIL

SQL ํ•™์Šต - JOIN (TIL 230511)

sunhokimDev 2023. 5. 12. 17:04

๐Ÿ“š KDT WEEK 6 DAY 4 TIL

  • JOIN

 


์ˆ™์ œ

%%sql

SELECT  
    LEFT(ts.ts, 7) as month,
    ch.channel,
    COUNT(DISTINCT ch.userid) as uniqueUsers,
    COUNT(DISTINCT CASE WHEN trans.amount > 0 THEN ch.userid END) as paidUsers,
    ROUND(paidUsers*100.0 / NULLIF(uniqueUsers,0), 2) as conversionRate,
    SUM(CASE WHEN trans.refunded is not NULL THEN trans.amount END) as grossRevenue,
    SUM(CASE WHEN trans.refunded is False THEN trans.amount END) as netRevenue
FROM raw_data.session_timestamp as ts
INNER JOIN raw_data.user_session_channel as ch ON ts.sessionid = ch.sessionid
LEFT JOIN raw_data.session_transaction as trans ON ch.sessionid = trans.sessionid
GROUP BY 1,2
ORDER BY 1,2 --CREATEํ•  ๋•Œ์—๋Š” ์˜๋ฏธ์—†์Œ, DB๊ฐ€ ์•Œ์•„์„œ ํ•จ