Query editor
WITH display_source AS (WITH report_clock AS (SELECT CASE WHEN '{{as_of_date}}'='latest'THEN CAST(DATE_TRUNC('week',CURRENT_DATE-INTERVAL '3' DAY)+INTERVAL '3' DAY AS DATE)ELSE TRY_CAST('{{as_of_date}}' AS DATE) END cutoff), d AS (SELECT period,symbol,amount,traders,txns FROM dune.bnbchain.bstock_daily_volumeWHERE period>=(SELECT cutoff FROM report_clock)-INTERVAL '14' DAY AND period<(SELECT cutoff FROM report_clock)), a AS (SELECT symbol,SUM(IF(period>=(SELECT cutoff FROM report_clock)-INTERVAL '7' DAY,amount,0)) current_usd,SUM(IF(period<(SELECT cutoff FROM report_clock)-INTERVAL '7' DAY,amount,0)) prior_usd,MAX_BY(period,IF(period>=(SELECT cutoff FROM report_clock)-INTERVAL '7' DAY,amount,-1)) largest_current_day,MAX(IF(period>=(SELECT cutoff FROM report_clock)-INTERVAL '7' DAY,amount,0)) largest_current_day_usd,MAX_BY(traders,IF(period>=(SELECT cutoff FROM report_clock)-INTERVAL '7' DAY,amount,-1)) largest_day_traders,MAX_BY(txns,IF(period>=(SELECT cutoff FROM report_clock)-INTERVAL '7' DAY,amount,-1)) largest_day_transactions,COUNT(DISTINCT IF(period>=(SELECT cutoff FROM report_clock)-INTERVAL '7' DAY,period,NULL)) active_daysFROM d GROUP BY 1)SELECT CAST((SELECT cutoff FROM report_clock)-INTERVAL '7' DAY AS DATE) week_start,CAST((SELECT cutoff FROM report_clock)-INTERVAL '1' DAY AS DATE) week_end,*,current_usd-prior_usd change_usd,100.0*(current_usd-prior_usd)/NULLIF(prior_usd,0) change_pct,SUM(current_usd) OVER() total_current_usd,SUM(prior_usd) OVER() total_prior_usd,SUM(current_usd-prior_usd) OVER() total_change_usd,100.0*(SUM(current_usd) OVER()/NULLIF(SUM(prior_usd) OVER(),0)-1) total_change_pct,100.0*current_usd/NULLIF(SUM(current_usd) OVER(),0) constituent_share_pct,CASE WHEN largest_current_day_usd>=1000000 AND largest_day_transactions<=5 THEN 'Concentrated print: inspect source' ELSE '' END concentration_noteFROM a ORDER BY current_usd DESC)SELECT display_source.*,total_current_usd/1000000 total_current_m FROM display_sourceORDER BY current_usd DESC