Query editor
WITH CastsAggregation AS (SELECT fid, COUNT(*) AS total_casts FROM dune.neynar.dataset_farcaster_casts GROUP BY fid),GivenReactionsAggregation AS (SELECT fid, SUM(CASE WHEN reaction_type = 1 THEN 1 ELSE 0 END) AS total_likes_given, SUM(CASE WHEN reaction_type = 2 THEN 1 ELSE 0 END) AS total_recasts_given FROM dune.neynar.dataset_farcaster_reactions GROUP BY fid),ReceivedReactionsAggregation AS (SELECT target_fid AS fid, SUM(CASE WHEN reaction_type = 1 THEN 1 ELSE 0 END) AS total_likes_received, SUM(CASE WHEN reaction_type = 2 THEN 1 ELSE 0 END) AS total_recasts_received FROM dune.neynar.dataset_farcaster_reactions GROUP BY target_fid),LinksAggregation AS (SELECT fid, COUNT(*) AS total_following FROM dune.neynar.dataset_farcaster_links WHERE type = 'follow' GROUP BY fid),FollowersAggregation AS (SELECT target_fid AS fid, COUNT(*) AS total_followers FROM dune.neynar.dataset_farcaster_links WHERE type = 'follow' GROUP BY target_fid),UnfollowersAggregation AS (SELECT target_fid AS fid, COUNT(*) AS total_unfollowers FROM dune.neynar.dataset_farcaster_links WHERE type = 'follow' AND deleted_at IS NOT NULL AND (updated_at IS NULL OR updated_at <= deleted_at) GROUP BY target_fid),UserDataAggregation AS (SELECT fid, MAX(CASE WHEN type = 6 THEN value END) AS fname, MAX(CASE WHEN type = 2 THEN value END) AS display_name FROM dune.neynar.dataset_farcaster_user_data GROUP BY fid),IntermediateQuery AS (SELECTu.fname, u.display_name, c.total_casts,COALESCE(g.total_likes_given, 0) AS total_likes_given,COALESCE(r.total_likes_received, 0) AS total_likes_received,COALESCE(g.total_recasts_given, 0) AS total_recasts_given,COALESCE(r.total_recasts_received, 0) AS total_recasts_received,COALESCE(l.total_following, 0) AS total_following,COALESCE(f.total_followers, 0) AS total_followers,COALESCE(un.total_unfollowers, 0) AS total_unfollowers,CASEWHEN COALESCE(f.total_followers, 1) >= 5000 THEN ROUND((COALESCE(un.total_unfollowers, 0) / CAST(COALESCE(f.total_followers, 1) AS DOUBLE PRECISION)) + ((1 - CAST(c.total_casts AS DOUBLE PRECISION) / CAST(COALESCE(f.total_followers, 1) AS DOUBLE PRECISION)) * (COALESCE(un.total_unfollowers, 0) / CAST(COALESCE(f.total_followers, 1) AS DOUBLE PRECISION))), 5)ELSE ROUND(COALESCE(un.total_unfollowers, 0) / CAST(COALESCE(f.total_followers, 1) AS DOUBLE PRECISION), 5)END AS follower_drop_rate,CASEWHEN c.total_casts > 0 THEN CEIL(CAST((COALESCE(r.total_likes_received, 0) + 5 * COALESCE(r.total_recasts_received, 0)) AS DOUBLE PRECISION) / CAST(c.total_casts AS DOUBLE PRECISION))ELSE 0END AS engagement_score,CASEWHEN (f.total_followers + 1) > 0 AND c.total_casts > 0 THEN CEIL(CAST((COALESCE(r.total_likes_received, 0) + 5 * COALESCE(r.total_recasts_received, 0)) AS DOUBLE PRECISION) / CAST(c.total_casts AS DOUBLE PRECISION) * CAST((f.total_followers + 1) AS DOUBLE PRECISION) / 100)ELSE 0END AS reach_score,CASEWHEN COALESCE(f.total_followers, 1) > 0 THEN CEIL(0.4 * CEIL(CAST((COALESCE(r.total_likes_received, 0) + 5 * COALESCE(r.total_recasts_received, 0)) AS DOUBLE PRECISION) / CAST(c.total_casts AS DOUBLE PRECISION)) + 0.4 * CEIL(CAST((COALESCE(r.total_likes_received, 0) + 5 * COALESCE(r.total_recasts_received, 0)) AS DOUBLE PRECISION) / CAST(c.total_casts AS DOUBLE PRECISION) * CAST((f.total_followers + 1) AS DOUBLE PRECISION) / 100) + 0.2 * (1 - COALESCE(un.total_unfollowers, 0) / CAST(COALESCE(f.total_followers, 1) AS DOUBLE PRECISION)) * 100)ELSE 0END AS social_health_score,ROUND((CAST((SELECT COUNT(*) FROM dune.neynar.dataset_farcaster_reactions WHERE target_fid = c.fid AND timestamp >= NOW() - interval '30' day)AS DOUBLE) +CAST((SELECT COUNT(*) FROM dune.neynar.dataset_farcaster_casts WHERE parent_fid = c.fid AND timestamp >= NOW() - interval '30' day)AS DOUBLE)) /CAST((SELECT COUNT(*) FROM dune.neynar.dataset_farcaster_casts WHERE fid = c.fid AND timestamp >= NOW() - interval '30' day)