[{"data":1,"prerenderedAt":7652},["ShallowReactive",2],{"blog-sql-101-for-salesforce-users":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"author":11,"tags":14,"image":19,"body":20,"_type":7646,"_id":7647,"_source":7648,"_file":7649,"_stem":7650,"_extension":7651},"\u002Fblog\u002Fsql-101-for-salesforce-users","blog",false,"","SQL 101 for Salesforce Users","A practical intro to SQL for Salesforce admins and ops folks: the basics, a few advanced topics like UNION, CTEs and window functions, and ready-to-run examples for pipeline, bookings, win rate and lead conversion.","2026-11-10",{"name":12,"title":13},"Joe Fusaro","Founder, Datachef",[15,16,17,18],"sql","salesforce","sql for salesforce","pipeline reporting","\u002Fblog\u002Fsql-101-for-salesforce-users.png",{"type":21,"children":22,"toc":7615},"root",[23,31,36,43,268,272,277,342,421,434,439,460,501,531,537,542,601,636,713,718,723,728,782,807,828,833,922,964,977,982,1081,1108,1113,1259,1302,1328,1339,1476,1496,1501,1627,1666,1671,1681,1947,1959,1964,2132,2158,2182,2202,2207,2212,2217,2230,2520,2541,2569,2574,2587,2868,2910,2915,2920,2925,3593,3613,3641,3653,3658,3670,4115,4141,4162,4167,4172,4374,4386,4391,4582,4593,4598,4935,4940,4945,5485,5506,5511,5523,6356,6389,6423,6428,6433,6438,6443,7450,7455,7525,7550,7562,7568,7587,7609],{"type":24,"tag":25,"props":26,"children":27},"element","p",{},[28],{"type":29,"value":30},"text","This post is for people who spend their day in Salesforce (admins, sales and marketing ops, RevOps analysts) and have never written a line of SQL. We'll start with the basics, move on to a few things reports and SOQL just can't do, and finish with example queries you can copy and run against your own data.",{"type":24,"tag":25,"props":32,"children":33},{},[34],{"type":29,"value":35},"Here's the good news. If you've ever built a Salesforce report, you already know more SQL than you think. A report is just a question: which records, which fields, which filters, grouped how. SQL is another way to write that question down, minus the report-type limits.",{"type":24,"tag":37,"props":38,"children":40},"h2",{"id":39},"table-of-contents",[41],{"type":29,"value":42},"Table of Contents",{"type":24,"tag":44,"props":45,"children":46},"ul",{},[47,58,79,154,202],{"type":24,"tag":48,"props":49,"children":50},"li",{},[51],{"type":24,"tag":52,"props":53,"children":55},"a",{"href":54},"#how-does-sql-compare-with-soql",[56],{"type":29,"value":57},"How does SQL compare with SOQL?",{"type":24,"tag":48,"props":59,"children":60},{},[61,67],{"type":24,"tag":52,"props":62,"children":64},{"href":63},"#where-does-the-salesforce-data-live",[65],{"type":29,"value":66},"Where does the Salesforce data live?",{"type":24,"tag":44,"props":68,"children":69},{},[70],{"type":24,"tag":48,"props":71,"children":72},{},[73],{"type":24,"tag":52,"props":74,"children":76},{"href":75},"#other-ways-to-run-sql-on-salesforce-data",[77],{"type":29,"value":78},"Other ways to run SQL on Salesforce data",{"type":24,"tag":48,"props":80,"children":81},{},[82,88],{"type":24,"tag":52,"props":83,"children":85},{"href":84},"#the-basics",[86],{"type":29,"value":87},"The basics",{"type":24,"tag":44,"props":89,"children":90},{},[91,100,109,118,127,136,145],{"type":24,"tag":48,"props":92,"children":93},{},[94],{"type":24,"tag":52,"props":95,"children":97},{"href":96},"#selecting-columns",[98],{"type":29,"value":99},"Selecting columns",{"type":24,"tag":48,"props":101,"children":102},{},[103],{"type":24,"tag":52,"props":104,"children":106},{"href":105},"#filtering-with-where",[107],{"type":29,"value":108},"Filtering with WHERE",{"type":24,"tag":48,"props":110,"children":111},{},[112],{"type":24,"tag":52,"props":113,"children":115},{"href":114},"#sorting-and-limiting",[116],{"type":29,"value":117},"Sorting and limiting",{"type":24,"tag":48,"props":119,"children":120},{},[121],{"type":24,"tag":52,"props":122,"children":124},{"href":123},"#aggregate-functions",[125],{"type":29,"value":126},"Aggregate functions",{"type":24,"tag":48,"props":128,"children":129},{},[130],{"type":24,"tag":52,"props":131,"children":133},{"href":132},"#grouping-with-group-by",[134],{"type":29,"value":135},"Grouping with GROUP BY",{"type":24,"tag":48,"props":137,"children":138},{},[139],{"type":24,"tag":52,"props":140,"children":142},{"href":141},"#labeling-rows-with-case-when",[143],{"type":29,"value":144},"Labeling rows with CASE WHEN",{"type":24,"tag":48,"props":146,"children":147},{},[148],{"type":24,"tag":52,"props":149,"children":151},{"href":150},"#joining-tables",[152],{"type":29,"value":153},"Joining tables",{"type":24,"tag":48,"props":155,"children":156},{},[157,163],{"type":24,"tag":52,"props":158,"children":160},{"href":159},"#advanced-topics",[161],{"type":29,"value":162},"Advanced topics",{"type":24,"tag":44,"props":164,"children":165},{},[166,175,184,193],{"type":24,"tag":48,"props":167,"children":168},{},[169],{"type":24,"tag":52,"props":170,"children":172},{"href":171},"#combining-leads-and-contacts-with-union",[173],{"type":29,"value":174},"Combining leads and contacts with UNION",{"type":24,"tag":48,"props":176,"children":177},{},[178],{"type":24,"tag":52,"props":179,"children":181},{"href":180},"#breaking-up-a-query-with-ctes",[182],{"type":29,"value":183},"Breaking up a query with CTEs",{"type":24,"tag":48,"props":185,"children":186},{},[187],{"type":24,"tag":52,"props":188,"children":190},{"href":189},"#joining-more-than-two-tables",[191],{"type":29,"value":192},"Joining more than two tables",{"type":24,"tag":48,"props":194,"children":195},{},[196],{"type":24,"tag":52,"props":197,"children":199},{"href":198},"#picking-one-row-per-group-with-row_number",[200],{"type":29,"value":201},"Picking one row per group with ROW_NUMBER",{"type":24,"tag":48,"props":203,"children":204},{},[205,211],{"type":24,"tag":52,"props":206,"children":208},{"href":207},"#examples",[209],{"type":29,"value":210},"Examples",{"type":24,"tag":44,"props":212,"children":213},{},[214,223,232,241,250,259],{"type":24,"tag":48,"props":215,"children":216},{},[217],{"type":24,"tag":52,"props":218,"children":220},{"href":219},"#open-pipeline-by-stage",[221],{"type":29,"value":222},"Open pipeline by stage",{"type":24,"tag":48,"props":224,"children":225},{},[226],{"type":24,"tag":52,"props":227,"children":229},{"href":228},"#bookings-by-month",[230],{"type":29,"value":231},"Bookings by month",{"type":24,"tag":48,"props":233,"children":234},{},[235],{"type":24,"tag":52,"props":236,"children":238},{"href":237},"#accounts-with-the-most-open-pipeline",[239],{"type":29,"value":240},"Accounts with the most open pipeline",{"type":24,"tag":48,"props":242,"children":243},{},[244],{"type":24,"tag":52,"props":245,"children":247},{"href":246},"#win-rate-by-owner",[248],{"type":29,"value":249},"Win rate by owner",{"type":24,"tag":48,"props":251,"children":252},{},[253],{"type":24,"tag":52,"props":254,"children":256},{"href":255},"#lead-engagement-and-conversion-by-source",[257],{"type":29,"value":258},"Lead engagement and conversion by source",{"type":24,"tag":48,"props":260,"children":261},{},[262],{"type":24,"tag":52,"props":263,"children":265},{"href":264},"#lead-follow-up-velocity-and-conversion",[266],{"type":29,"value":267},"Lead follow-up velocity and conversion",{"type":24,"tag":269,"props":270,"children":271},"hr",{},[],{"type":24,"tag":37,"props":273,"children":275},{"id":274},"how-does-sql-compare-with-soql",[276],{"type":29,"value":57},{"type":24,"tag":25,"props":278,"children":279},{},[280,282,289,291,297,298,304,305,311,312,318,319,325,326,332,334,340],{"type":29,"value":281},"SOQL (Salesforce Object Query Language) is what you use to query Salesforce directly, in the Developer Console, Workbench or Apex. It looks a lot like SQL, and plenty carries over: ",{"type":24,"tag":283,"props":284,"children":286},"code",{"className":285},[],[287],{"type":29,"value":288},"SELECT",{"type":29,"value":290},", ",{"type":24,"tag":283,"props":292,"children":294},{"className":293},[],[295],{"type":29,"value":296},"FROM",{"type":29,"value":290},{"type":24,"tag":283,"props":299,"children":301},{"className":300},[],[302],{"type":29,"value":303},"WHERE",{"type":29,"value":290},{"type":24,"tag":283,"props":306,"children":308},{"className":307},[],[309],{"type":29,"value":310},"ORDER BY",{"type":29,"value":290},{"type":24,"tag":283,"props":313,"children":315},{"className":314},[],[316],{"type":29,"value":317},"LIMIT",{"type":29,"value":290},{"type":24,"tag":283,"props":320,"children":322},{"className":321},[],[323],{"type":29,"value":324},"GROUP BY",{"type":29,"value":290},{"type":24,"tag":283,"props":327,"children":329},{"className":328},[],[330],{"type":29,"value":331},"COUNT",{"type":29,"value":333}," and ",{"type":24,"tag":283,"props":335,"children":337},{"className":336},[],[338],{"type":29,"value":339},"SUM",{"type":29,"value":341}," all work. But SOQL was made for fetching records, not analyzing them. Here's what you give up:",{"type":24,"tag":343,"props":344,"children":345},"ol",{},[346,357,367,385,395],{"type":24,"tag":48,"props":347,"children":348},{},[349,355],{"type":24,"tag":350,"props":351,"children":352},"strong",{},[353],{"type":29,"value":354},"Joins only through lookup fields.",{"type":29,"value":356}," In SOQL, two objects can only be connected if there's a lookup or master-detail field between them, like an opportunity and its account. SQL will join any two tables on any value they have in common. Leads and contacts on email address, for instance.",{"type":24,"tag":48,"props":358,"children":359},{},[360,365],{"type":24,"tag":350,"props":361,"children":362},{},[363],{"type":29,"value":364},"No UNION.",{"type":29,"value":366}," There's no way to get leads and contacts into one list with a single query.",{"type":24,"tag":48,"props":368,"children":369},{},[370,375,377,383],{"type":24,"tag":350,"props":371,"children":372},{},[373],{"type":29,"value":374},"No CTEs, and no subqueries in FROM.",{"type":29,"value":376}," You get subqueries in a few narrow spots, like ",{"type":24,"tag":283,"props":378,"children":380},{"className":379},[],[381],{"type":29,"value":382},"WHERE Id IN (SELECT AccountId FROM Opportunity)",{"type":29,"value":384}," to filter on another object's ids, and that's about it. Building a query in steps, or querying the output of another query, isn't possible.",{"type":24,"tag":48,"props":386,"children":387},{},[388,393],{"type":24,"tag":350,"props":389,"children":390},{},[391],{"type":29,"value":392},"No window functions.",{"type":29,"value":394}," SOQL will give you each account's biggest open deal as a nested list. What it won't do is rank every deal within its account, number a rep's wins in order or keep a running total of bookings.",{"type":24,"tag":48,"props":396,"children":397},{},[398,403,405,411,413,419],{"type":24,"tag":350,"props":399,"children":400},{},[401],{"type":29,"value":402},"Limited math and logic.",{"type":29,"value":404}," There's no ",{"type":24,"tag":283,"props":406,"children":408},{"className":407},[],[409],{"type":29,"value":410},"CASE WHEN",{"type":29,"value":412},", and you can't calculate something like ",{"type":24,"tag":283,"props":414,"children":416},{"className":415},[],[417],{"type":29,"value":418},"amount * probability",{"type":29,"value":420}," inside the query.",{"type":24,"tag":25,"props":422,"children":423},{},[424,426,432],{"type":29,"value":425},"I go deeper on this in ",{"type":24,"tag":52,"props":427,"children":429},{"href":428},"\u002Fblog\u002Fovercoming-soql-limits",[430],{"type":29,"value":431},"Overcoming the limits of SOQL",{"type":29,"value":433},". Several examples further down (the UNION, the CTE, the join on email) are things SOQL simply can't do.",{"type":24,"tag":37,"props":435,"children":437},{"id":436},"where-does-the-salesforce-data-live",[438],{"type":29,"value":66},{"type":24,"tag":25,"props":440,"children":441},{},[442,444,450,452,458],{"type":29,"value":443},"SQL needs the data to sit somewhere that can run SQL. Once Salesforce is synced, each object turns into a table: Opportunity is ",{"type":24,"tag":283,"props":445,"children":447},{"className":446},[],[448],{"type":29,"value":449},"salesforce.opportunity",{"type":29,"value":451},", Account is ",{"type":24,"tag":283,"props":453,"children":455},{"className":454},[],[456],{"type":29,"value":457},"salesforce.account",{"type":29,"value":459},", and so on.",{"type":24,"tag":25,"props":461,"children":462},{},[463,465,471,472,478,479,485,487,493,494,499],{"type":29,"value":464},"Columns keep the API names you already know from Setup (",{"type":24,"tag":283,"props":466,"children":468},{"className":467},[],[469],{"type":29,"value":470},"stagename",{"type":29,"value":290},{"type":24,"tag":283,"props":473,"children":475},{"className":474},[],[476],{"type":29,"value":477},"amount",{"type":29,"value":290},{"type":24,"tag":283,"props":480,"children":482},{"className":481},[],[483],{"type":29,"value":484},"ownerid",{"type":29,"value":486},"). Case doesn't matter, so ",{"type":24,"tag":283,"props":488,"children":490},{"className":489},[],[491],{"type":29,"value":492},"StageName",{"type":29,"value":333},{"type":24,"tag":283,"props":495,"children":497},{"className":496},[],[498],{"type":29,"value":470},{"type":29,"value":500}," mean the same thing.",{"type":24,"tag":25,"props":502,"children":503},{},[504],{"type":24,"tag":505,"props":506,"children":507},"em",{},[508,513,515,521,523,529],{"type":24,"tag":283,"props":509,"children":511},{"className":510},[],[512],{"type":29,"value":16},{"type":29,"value":514}," is just the name of my Salesforce connection. If yours is named something else, say ",{"type":24,"tag":283,"props":516,"children":518},{"className":517},[],[519],{"type":29,"value":520},"salesforce_dev",{"type":29,"value":522},", the table becomes ",{"type":24,"tag":283,"props":524,"children":526},{"className":525},[],[527],{"type":29,"value":528},"salesforce_dev.opportunity",{"type":29,"value":530},". Not sure? The table list in the SQL editor shows the exact names.",{"type":24,"tag":532,"props":533,"children":535},"h3",{"id":534},"other-ways-to-run-sql-on-salesforce-data",[536],{"type":29,"value":78},{"type":24,"tag":25,"props":538,"children":539},{},[540],{"type":29,"value":541},"There are a few common routes for getting Salesforce data into something you can query with SQL:",{"type":24,"tag":343,"props":543,"children":544},{},[545,555,565,591],{"type":24,"tag":48,"props":546,"children":547},{},[548,553],{"type":24,"tag":350,"props":549,"children":550},{},[551],{"type":29,"value":552},"A cloud data warehouse.",{"type":29,"value":554}," Copy Salesforce into Snowflake, BigQuery or Redshift, normally with an ELT tool like Fivetran or Airbyte keeping it in sync. Most data teams run some version of this. It's powerful, but it's also several tools to run (the pipeline, the warehouse, usually a BI tool on top) and several bills to pay.",{"type":24,"tag":48,"props":556,"children":557},{},[558,563],{"type":24,"tag":350,"props":559,"children":560},{},[561],{"type":29,"value":562},"A Postgres or MySQL database.",{"type":29,"value":564}," Load Salesforce data into a database, either on your laptop or hosted, from Data Loader CSV exports or a script that hits the Salesforce API. It's cheap, and for a one-off analysis it's fine. Just keep in mind that you need to periodically refresh (i.e. upsert) your local copy, manage schema changes, etc.",{"type":24,"tag":48,"props":566,"children":567},{},[568,573,575,581,583,589],{"type":24,"tag":350,"props":569,"children":570},{},[571],{"type":29,"value":572},"Data 360.",{"type":29,"value":574}," Data 360 (formerly Data Cloud) is Salesforce's own data platform. It pulls your CRM data in next to your other sources, and you can query it with SQL from its Query Editor or the Query API. It can also share data with Snowflake, Databricks and others without making a copy. One caveat: the SQL in this post won't run there as-is, because Data 360 renames every object and field. ",{"type":24,"tag":283,"props":576,"children":578},{"className":577},[],[579],{"type":29,"value":580},"SELECT name, amount FROM salesforce.opportunity",{"type":29,"value":582}," becomes something like ",{"type":24,"tag":283,"props":584,"children":586},{"className":585},[],[587],{"type":29,"value":588},"SELECT \"Name__c\", \"Amount__c\" FROM \"Opportunity_Home__dll\"",{"type":29,"value":590},", depending on how your data streams are set up.",{"type":24,"tag":48,"props":592,"children":593},{},[594,599],{"type":24,"tag":350,"props":595,"children":596},{},[597],{"type":29,"value":598},"A full-stack data platform like Datachef.",{"type":29,"value":600}," It syncs Salesforce for you, keeps the tables up to date and gives you a SQL editor to query them, with no pipeline or warehouse to run.",{"type":24,"tag":25,"props":602,"children":603},{},[604,606,612,613,619,621,626,628,634],{"type":29,"value":605},"The queries in this post are written in generic SQL that runs as-is in Datachef, Postgres and Snowflake. To keep them that way, they skip two handy shortcuts only some databases have, ",{"type":24,"tag":283,"props":607,"children":609},{"className":608},[],[610],{"type":29,"value":611},"FILTER (WHERE ...)",{"type":29,"value":333},{"type":24,"tag":283,"props":614,"children":616},{"className":615},[],[617],{"type":29,"value":618},"QUALIFY",{"type":29,"value":620},", and use a ",{"type":24,"tag":283,"props":622,"children":624},{"className":623},[],[625],{"type":29,"value":410},{"type":29,"value":627}," and a ",{"type":24,"tag":283,"props":629,"children":631},{"className":630},[],[632],{"type":29,"value":633},"ROW_NUMBER()",{"type":29,"value":635}," CTE instead. MySQL needs a few edits:",{"type":24,"tag":343,"props":637,"children":638},{},[639,664],{"type":24,"tag":48,"props":640,"children":641},{},[642,655,657,663],{"type":24,"tag":350,"props":643,"children":644},{},[645,647,653],{"type":29,"value":646},"Drop ",{"type":24,"tag":283,"props":648,"children":650},{"className":649},[],[651],{"type":29,"value":652},"NULLS LAST",{"type":29,"value":654},".",{"type":29,"value":656}," MySQL doesn't support it, and already puts blanks last when you sort ",{"type":24,"tag":283,"props":658,"children":660},{"className":659},[],[661],{"type":29,"value":662},"DESC",{"type":29,"value":654},{"type":24,"tag":48,"props":665,"children":666},{},[667,672,674,680,682,688,690,696,698,704,706,712],{"type":24,"tag":350,"props":668,"children":669},{},[670],{"type":29,"value":671},"Swap the date functions.",{"type":29,"value":673}," ",{"type":24,"tag":283,"props":675,"children":677},{"className":676},[],[678],{"type":29,"value":679},"date_trunc",{"type":29,"value":681}," becomes ",{"type":24,"tag":283,"props":683,"children":685},{"className":684},[],[686],{"type":29,"value":687},"DATE_FORMAT",{"type":29,"value":689},", the minute arithmetic becomes ",{"type":24,"tag":283,"props":691,"children":693},{"className":692},[],[694],{"type":29,"value":695},"TIMESTAMPDIFF",{"type":29,"value":697},", and casts go to ",{"type":24,"tag":283,"props":699,"children":701},{"className":700},[],[702],{"type":29,"value":703},"DATETIME",{"type":29,"value":705}," rather than ",{"type":24,"tag":283,"props":707,"children":709},{"className":708},[],[710],{"type":29,"value":711},"TIMESTAMP",{"type":29,"value":654},{"type":24,"tag":37,"props":714,"children":716},{"id":715},"the-basics",[717],{"type":29,"value":87},{"type":24,"tag":25,"props":719,"children":720},{},[721],{"type":29,"value":722},"You'll use these in pretty much every query you write.",{"type":24,"tag":532,"props":724,"children":726},{"id":725},"selecting-columns",[727],{"type":29,"value":99},{"type":24,"tag":729,"props":730,"children":733},"pre",{"className":731,"code":732,"language":15,"meta":7,"style":7},"language-sql shiki shiki-themes github-light github-dark","SELECT name, stagename, amount, closedate\nFROM salesforce.opportunity\n",[734],{"type":24,"tag":283,"props":735,"children":736},{"__ignoreMap":7},[737,759],{"type":24,"tag":738,"props":739,"children":742},"span",{"class":740,"line":741},"line",1,[743,748,753],{"type":24,"tag":738,"props":744,"children":746},{"style":745},"--shiki-default:#D73A49;--shiki-dark:#F97583",[747],{"type":29,"value":288},{"type":24,"tag":738,"props":749,"children":750},{"style":745},[751],{"type":29,"value":752}," name",{"type":24,"tag":738,"props":754,"children":756},{"style":755},"--shiki-default:#24292E;--shiki-dark:#E1E4E8",[757],{"type":29,"value":758},", stagename, amount, closedate\n",{"type":24,"tag":738,"props":760,"children":762},{"class":740,"line":761},2,[763,767,773,777],{"type":24,"tag":738,"props":764,"children":765},{"style":745},[766],{"type":29,"value":296},{"type":24,"tag":738,"props":768,"children":770},{"style":769},"--shiki-default:#005CC5;--shiki-dark:#79B8FF",[771],{"type":29,"value":772}," salesforce",{"type":24,"tag":738,"props":774,"children":775},{"style":755},[776],{"type":29,"value":654},{"type":24,"tag":738,"props":778,"children":779},{"style":769},[780],{"type":29,"value":781},"opportunity\n",{"type":24,"tag":25,"props":783,"children":784},{},[785,790,792,797,799,805],{"type":24,"tag":283,"props":786,"children":788},{"className":787},[],[789],{"type":29,"value":288},{"type":29,"value":791}," lists the columns you want; ",{"type":24,"tag":283,"props":793,"children":795},{"className":794},[],[796],{"type":29,"value":296},{"type":29,"value":798}," says which table. ",{"type":24,"tag":283,"props":800,"children":802},{"className":801},[],[803],{"type":29,"value":804},"SELECT *",{"type":29,"value":806}," gets you every column (the asterisk means \"all\"). Handy when you're poking around a table. But an opportunity has dozens of columns, so in a real query you'll usually want to name just the ones you need.",{"type":24,"tag":25,"props":808,"children":809},{},[810,812,818,820,826],{"type":29,"value":811},"Rename a column with ",{"type":24,"tag":283,"props":813,"children":815},{"className":814},[],[816],{"type":29,"value":817},"AS",{"type":29,"value":819},", like ",{"type":24,"tag":283,"props":821,"children":823},{"className":822},[],[824],{"type":29,"value":825},"SELECT stagename AS stage",{"type":29,"value":827},". That shows up a lot below.",{"type":24,"tag":532,"props":829,"children":831},{"id":830},"filtering-with-where",[832],{"type":29,"value":108},{"type":24,"tag":729,"props":834,"children":836},{"className":731,"code":835,"language":15,"meta":7,"style":7},"SELECT name, stagename, amount\nFROM salesforce.opportunity\nWHERE isclosed = false\n  AND amount > 50000\n",[837],{"type":24,"tag":283,"props":838,"children":839},{"__ignoreMap":7},[840,856,875,898],{"type":24,"tag":738,"props":841,"children":842},{"class":740,"line":741},[843,847,851],{"type":24,"tag":738,"props":844,"children":845},{"style":745},[846],{"type":29,"value":288},{"type":24,"tag":738,"props":848,"children":849},{"style":745},[850],{"type":29,"value":752},{"type":24,"tag":738,"props":852,"children":853},{"style":755},[854],{"type":29,"value":855},", stagename, amount\n",{"type":24,"tag":738,"props":857,"children":858},{"class":740,"line":761},[859,863,867,871],{"type":24,"tag":738,"props":860,"children":861},{"style":745},[862],{"type":29,"value":296},{"type":24,"tag":738,"props":864,"children":865},{"style":769},[866],{"type":29,"value":772},{"type":24,"tag":738,"props":868,"children":869},{"style":755},[870],{"type":29,"value":654},{"type":24,"tag":738,"props":872,"children":873},{"style":769},[874],{"type":29,"value":781},{"type":24,"tag":738,"props":876,"children":878},{"class":740,"line":877},3,[879,883,888,893],{"type":24,"tag":738,"props":880,"children":881},{"style":745},[882],{"type":29,"value":303},{"type":24,"tag":738,"props":884,"children":885},{"style":755},[886],{"type":29,"value":887}," isclosed ",{"type":24,"tag":738,"props":889,"children":890},{"style":745},[891],{"type":29,"value":892},"=",{"type":24,"tag":738,"props":894,"children":895},{"style":755},[896],{"type":29,"value":897}," false\n",{"type":24,"tag":738,"props":899,"children":901},{"class":740,"line":900},4,[902,907,912,917],{"type":24,"tag":738,"props":903,"children":904},{"style":745},[905],{"type":29,"value":906},"  AND",{"type":24,"tag":738,"props":908,"children":909},{"style":755},[910],{"type":29,"value":911}," amount ",{"type":24,"tag":738,"props":913,"children":914},{"style":745},[915],{"type":29,"value":916},">",{"type":24,"tag":738,"props":918,"children":919},{"style":769},[920],{"type":29,"value":921}," 50000\n",{"type":24,"tag":25,"props":923,"children":924},{},[925,927,932,934,940,941,947,949,955,957,963],{"type":29,"value":926},"Think of ",{"type":24,"tag":283,"props":928,"children":930},{"className":929},[],[931],{"type":29,"value":303},{"type":29,"value":933}," as your report filter. Combine conditions with ",{"type":24,"tag":283,"props":935,"children":937},{"className":936},[],[938],{"type":29,"value":939},"AND",{"type":29,"value":333},{"type":24,"tag":283,"props":942,"children":944},{"className":943},[],[945],{"type":29,"value":946},"OR",{"type":29,"value":948},". For \"any of these,\" use ",{"type":24,"tag":283,"props":950,"children":952},{"className":951},[],[953],{"type":29,"value":954},"IN",{"type":29,"value":956},": ",{"type":24,"tag":283,"props":958,"children":960},{"className":959},[],[961],{"type":29,"value":962},"WHERE type IN ('New Business', 'Upsell')",{"type":29,"value":654},{"type":24,"tag":25,"props":965,"children":966},{},[967,969,975],{"type":29,"value":968},"For pipeline I filter on ",{"type":24,"tag":283,"props":970,"children":972},{"className":971},[],[973],{"type":29,"value":974},"isclosed = false",{"type":29,"value":976}," instead of listing out every open stage. That way, when someone adds a new stage next quarter, nothing breaks.",{"type":24,"tag":532,"props":978,"children":980},{"id":979},"sorting-and-limiting",[981],{"type":29,"value":117},{"type":24,"tag":729,"props":983,"children":985},{"className":731,"code":984,"language":15,"meta":7,"style":7},"SELECT name, amount\nFROM salesforce.opportunity\nWHERE isclosed = false\nORDER BY amount DESC NULLS LAST\nLIMIT 10\n",[986],{"type":24,"tag":283,"props":987,"children":988},{"__ignoreMap":7},[989,1005,1024,1043,1068],{"type":24,"tag":738,"props":990,"children":991},{"class":740,"line":741},[992,996,1000],{"type":24,"tag":738,"props":993,"children":994},{"style":745},[995],{"type":29,"value":288},{"type":24,"tag":738,"props":997,"children":998},{"style":745},[999],{"type":29,"value":752},{"type":24,"tag":738,"props":1001,"children":1002},{"style":755},[1003],{"type":29,"value":1004},", amount\n",{"type":24,"tag":738,"props":1006,"children":1007},{"class":740,"line":761},[1008,1012,1016,1020],{"type":24,"tag":738,"props":1009,"children":1010},{"style":745},[1011],{"type":29,"value":296},{"type":24,"tag":738,"props":1013,"children":1014},{"style":769},[1015],{"type":29,"value":772},{"type":24,"tag":738,"props":1017,"children":1018},{"style":755},[1019],{"type":29,"value":654},{"type":24,"tag":738,"props":1021,"children":1022},{"style":769},[1023],{"type":29,"value":781},{"type":24,"tag":738,"props":1025,"children":1026},{"class":740,"line":877},[1027,1031,1035,1039],{"type":24,"tag":738,"props":1028,"children":1029},{"style":745},[1030],{"type":29,"value":303},{"type":24,"tag":738,"props":1032,"children":1033},{"style":755},[1034],{"type":29,"value":887},{"type":24,"tag":738,"props":1036,"children":1037},{"style":745},[1038],{"type":29,"value":892},{"type":24,"tag":738,"props":1040,"children":1041},{"style":755},[1042],{"type":29,"value":897},{"type":24,"tag":738,"props":1044,"children":1045},{"class":740,"line":900},[1046,1050,1054,1058,1063],{"type":24,"tag":738,"props":1047,"children":1048},{"style":745},[1049],{"type":29,"value":310},{"type":24,"tag":738,"props":1051,"children":1052},{"style":755},[1053],{"type":29,"value":911},{"type":24,"tag":738,"props":1055,"children":1056},{"style":745},[1057],{"type":29,"value":662},{"type":24,"tag":738,"props":1059,"children":1060},{"style":745},[1061],{"type":29,"value":1062}," NULLS",{"type":24,"tag":738,"props":1064,"children":1065},{"style":745},[1066],{"type":29,"value":1067}," LAST\n",{"type":24,"tag":738,"props":1069,"children":1071},{"class":740,"line":1070},5,[1072,1076],{"type":24,"tag":738,"props":1073,"children":1074},{"style":745},[1075],{"type":29,"value":317},{"type":24,"tag":738,"props":1077,"children":1078},{"style":769},[1079],{"type":29,"value":1080}," 10\n",{"type":24,"tag":25,"props":1082,"children":1083},{},[1084,1090,1092,1098,1100,1106],{"type":24,"tag":283,"props":1085,"children":1087},{"className":1086},[],[1088],{"type":29,"value":1089},"ORDER BY amount DESC",{"type":29,"value":1091}," puts the biggest deals at the top. (",{"type":24,"tag":283,"props":1093,"children":1095},{"className":1094},[],[1096],{"type":29,"value":1097},"ASC",{"type":29,"value":1099}," is the default and goes smallest first.) Add ",{"type":24,"tag":283,"props":1101,"children":1103},{"className":1102},[],[1104],{"type":29,"value":1105},"LIMIT 10",{"type":29,"value":1107}," and you've got your top ten open deals.",{"type":24,"tag":532,"props":1109,"children":1111},{"id":1110},"aggregate-functions",[1112],{"type":29,"value":126},{"type":24,"tag":729,"props":1114,"children":1116},{"className":731,"code":1115,"language":15,"meta":7,"style":7},"SELECT COUNT(*)    AS open_deals,\n       SUM(amount) AS pipeline,\n       AVG(amount) AS average_deal,\n       MAX(amount) AS biggest_deal\nFROM salesforce.opportunity\nWHERE isclosed = false\n",[1117],{"type":24,"tag":283,"props":1118,"children":1119},{"__ignoreMap":7},[1120,1156,1178,1199,1220,1239],{"type":24,"tag":738,"props":1121,"children":1122},{"class":740,"line":741},[1123,1127,1132,1137,1142,1147,1151],{"type":24,"tag":738,"props":1124,"children":1125},{"style":745},[1126],{"type":29,"value":288},{"type":24,"tag":738,"props":1128,"children":1129},{"style":769},[1130],{"type":29,"value":1131}," COUNT",{"type":24,"tag":738,"props":1133,"children":1134},{"style":755},[1135],{"type":29,"value":1136},"(",{"type":24,"tag":738,"props":1138,"children":1139},{"style":745},[1140],{"type":29,"value":1141},"*",{"type":24,"tag":738,"props":1143,"children":1144},{"style":755},[1145],{"type":29,"value":1146},")    ",{"type":24,"tag":738,"props":1148,"children":1149},{"style":745},[1150],{"type":29,"value":817},{"type":24,"tag":738,"props":1152,"children":1153},{"style":755},[1154],{"type":29,"value":1155}," open_deals,\n",{"type":24,"tag":738,"props":1157,"children":1158},{"class":740,"line":761},[1159,1164,1169,1173],{"type":24,"tag":738,"props":1160,"children":1161},{"style":769},[1162],{"type":29,"value":1163},"       SUM",{"type":24,"tag":738,"props":1165,"children":1166},{"style":755},[1167],{"type":29,"value":1168},"(amount) ",{"type":24,"tag":738,"props":1170,"children":1171},{"style":745},[1172],{"type":29,"value":817},{"type":24,"tag":738,"props":1174,"children":1175},{"style":755},[1176],{"type":29,"value":1177}," pipeline,\n",{"type":24,"tag":738,"props":1179,"children":1180},{"class":740,"line":877},[1181,1186,1190,1194],{"type":24,"tag":738,"props":1182,"children":1183},{"style":769},[1184],{"type":29,"value":1185},"       AVG",{"type":24,"tag":738,"props":1187,"children":1188},{"style":755},[1189],{"type":29,"value":1168},{"type":24,"tag":738,"props":1191,"children":1192},{"style":745},[1193],{"type":29,"value":817},{"type":24,"tag":738,"props":1195,"children":1196},{"style":755},[1197],{"type":29,"value":1198}," average_deal,\n",{"type":24,"tag":738,"props":1200,"children":1201},{"class":740,"line":900},[1202,1207,1211,1215],{"type":24,"tag":738,"props":1203,"children":1204},{"style":769},[1205],{"type":29,"value":1206},"       MAX",{"type":24,"tag":738,"props":1208,"children":1209},{"style":755},[1210],{"type":29,"value":1168},{"type":24,"tag":738,"props":1212,"children":1213},{"style":745},[1214],{"type":29,"value":817},{"type":24,"tag":738,"props":1216,"children":1217},{"style":755},[1218],{"type":29,"value":1219}," biggest_deal\n",{"type":24,"tag":738,"props":1221,"children":1222},{"class":740,"line":1070},[1223,1227,1231,1235],{"type":24,"tag":738,"props":1224,"children":1225},{"style":745},[1226],{"type":29,"value":296},{"type":24,"tag":738,"props":1228,"children":1229},{"style":769},[1230],{"type":29,"value":772},{"type":24,"tag":738,"props":1232,"children":1233},{"style":755},[1234],{"type":29,"value":654},{"type":24,"tag":738,"props":1236,"children":1237},{"style":769},[1238],{"type":29,"value":781},{"type":24,"tag":738,"props":1240,"children":1242},{"class":740,"line":1241},6,[1243,1247,1251,1255],{"type":24,"tag":738,"props":1244,"children":1245},{"style":745},[1246],{"type":29,"value":303},{"type":24,"tag":738,"props":1248,"children":1249},{"style":755},[1250],{"type":29,"value":887},{"type":24,"tag":738,"props":1252,"children":1253},{"style":745},[1254],{"type":29,"value":892},{"type":24,"tag":738,"props":1256,"children":1257},{"style":755},[1258],{"type":29,"value":897},{"type":24,"tag":25,"props":1260,"children":1261},{},[1262,1264,1270,1272,1277,1279,1285,1287,1293,1294,1300],{"type":29,"value":1263},"These are the summary numbers at the bottom of a report. ",{"type":24,"tag":283,"props":1265,"children":1267},{"className":1266},[],[1268],{"type":29,"value":1269},"COUNT(*)",{"type":29,"value":1271}," counts rows, ",{"type":24,"tag":283,"props":1273,"children":1275},{"className":1274},[],[1276],{"type":29,"value":339},{"type":29,"value":1278}," totals them, ",{"type":24,"tag":283,"props":1280,"children":1282},{"className":1281},[],[1283],{"type":29,"value":1284},"AVG",{"type":29,"value":1286}," averages them, and ",{"type":24,"tag":283,"props":1288,"children":1290},{"className":1289},[],[1291],{"type":29,"value":1292},"MIN",{"type":29,"value":333},{"type":24,"tag":283,"props":1295,"children":1297},{"className":1296},[],[1298],{"type":29,"value":1299},"MAX",{"type":29,"value":1301}," give you the extremes. One gotcha: blank amounts get skipped. Decide whether a blank should mean zero before you trust that total.",{"type":24,"tag":25,"props":1303,"children":1304},{},[1305,1311,1313,1318,1320,1326],{"type":24,"tag":283,"props":1306,"children":1308},{"className":1307},[],[1309],{"type":29,"value":1310},"COUNT(CASE WHEN ... THEN 1 END)",{"type":29,"value":1312}," counts only the rows that match a condition, because ",{"type":24,"tag":283,"props":1314,"children":1316},{"className":1315},[],[1317],{"type":29,"value":331},{"type":29,"value":1319}," skips the blanks the ",{"type":24,"tag":283,"props":1321,"children":1323},{"className":1322},[],[1324],{"type":29,"value":1325},"CASE",{"type":29,"value":1327}," leaves for every other row. That's how you put total deals and won deals next to each other.",{"type":24,"tag":25,"props":1329,"children":1330},{},[1331,1337],{"type":24,"tag":283,"props":1332,"children":1334},{"className":1333},[],[1335],{"type":29,"value":1336},"COUNT(DISTINCT ...)",{"type":29,"value":1338}," counts unique values rather than rows. So, how many accounts have open pipeline, and how many reps own it:",{"type":24,"tag":729,"props":1340,"children":1342},{"className":731,"code":1341,"language":15,"meta":7,"style":7},"SELECT COUNT(*)                  AS open_deals,\n       COUNT(DISTINCT accountid) AS accounts,\n       COUNT(DISTINCT ownerid)   AS reps\nFROM salesforce.opportunity\nWHERE isclosed = false\n",[1343],{"type":24,"tag":283,"props":1344,"children":1345},{"__ignoreMap":7},[1346,1378,1409,1438,1457],{"type":24,"tag":738,"props":1347,"children":1348},{"class":740,"line":741},[1349,1353,1357,1361,1365,1370,1374],{"type":24,"tag":738,"props":1350,"children":1351},{"style":745},[1352],{"type":29,"value":288},{"type":24,"tag":738,"props":1354,"children":1355},{"style":769},[1356],{"type":29,"value":1131},{"type":24,"tag":738,"props":1358,"children":1359},{"style":755},[1360],{"type":29,"value":1136},{"type":24,"tag":738,"props":1362,"children":1363},{"style":745},[1364],{"type":29,"value":1141},{"type":24,"tag":738,"props":1366,"children":1367},{"style":755},[1368],{"type":29,"value":1369},")                  ",{"type":24,"tag":738,"props":1371,"children":1372},{"style":745},[1373],{"type":29,"value":817},{"type":24,"tag":738,"props":1375,"children":1376},{"style":755},[1377],{"type":29,"value":1155},{"type":24,"tag":738,"props":1379,"children":1380},{"class":740,"line":761},[1381,1386,1390,1395,1400,1404],{"type":24,"tag":738,"props":1382,"children":1383},{"style":769},[1384],{"type":29,"value":1385},"       COUNT",{"type":24,"tag":738,"props":1387,"children":1388},{"style":755},[1389],{"type":29,"value":1136},{"type":24,"tag":738,"props":1391,"children":1392},{"style":745},[1393],{"type":29,"value":1394},"DISTINCT",{"type":24,"tag":738,"props":1396,"children":1397},{"style":755},[1398],{"type":29,"value":1399}," accountid) ",{"type":24,"tag":738,"props":1401,"children":1402},{"style":745},[1403],{"type":29,"value":817},{"type":24,"tag":738,"props":1405,"children":1406},{"style":755},[1407],{"type":29,"value":1408}," accounts,\n",{"type":24,"tag":738,"props":1410,"children":1411},{"class":740,"line":877},[1412,1416,1420,1424,1429,1433],{"type":24,"tag":738,"props":1413,"children":1414},{"style":769},[1415],{"type":29,"value":1385},{"type":24,"tag":738,"props":1417,"children":1418},{"style":755},[1419],{"type":29,"value":1136},{"type":24,"tag":738,"props":1421,"children":1422},{"style":745},[1423],{"type":29,"value":1394},{"type":24,"tag":738,"props":1425,"children":1426},{"style":755},[1427],{"type":29,"value":1428}," ownerid)   ",{"type":24,"tag":738,"props":1430,"children":1431},{"style":745},[1432],{"type":29,"value":817},{"type":24,"tag":738,"props":1434,"children":1435},{"style":755},[1436],{"type":29,"value":1437}," reps\n",{"type":24,"tag":738,"props":1439,"children":1440},{"class":740,"line":900},[1441,1445,1449,1453],{"type":24,"tag":738,"props":1442,"children":1443},{"style":745},[1444],{"type":29,"value":296},{"type":24,"tag":738,"props":1446,"children":1447},{"style":769},[1448],{"type":29,"value":772},{"type":24,"tag":738,"props":1450,"children":1451},{"style":755},[1452],{"type":29,"value":654},{"type":24,"tag":738,"props":1454,"children":1455},{"style":769},[1456],{"type":29,"value":781},{"type":24,"tag":738,"props":1458,"children":1459},{"class":740,"line":1070},[1460,1464,1468,1472],{"type":24,"tag":738,"props":1461,"children":1462},{"style":745},[1463],{"type":29,"value":303},{"type":24,"tag":738,"props":1465,"children":1466},{"style":755},[1467],{"type":29,"value":887},{"type":24,"tag":738,"props":1469,"children":1470},{"style":745},[1471],{"type":29,"value":892},{"type":24,"tag":738,"props":1473,"children":1474},{"style":755},[1475],{"type":29,"value":897},{"type":24,"tag":25,"props":1477,"children":1478},{},[1479,1481,1487,1489,1495],{"type":29,"value":1480},"An account with three open deals adds three to ",{"type":24,"tag":283,"props":1482,"children":1484},{"className":1483},[],[1485],{"type":29,"value":1486},"open_deals",{"type":29,"value":1488}," but only one to ",{"type":24,"tag":283,"props":1490,"children":1492},{"className":1491},[],[1493],{"type":29,"value":1494},"accounts",{"type":29,"value":654},{"type":24,"tag":532,"props":1497,"children":1499},{"id":1498},"grouping-with-group-by",[1500],{"type":29,"value":135},{"type":24,"tag":729,"props":1502,"children":1504},{"className":731,"code":1503,"language":15,"meta":7,"style":7},"SELECT stagename   AS stage,\n       COUNT(*)    AS deals,\n       SUM(amount) AS pipeline\nFROM salesforce.opportunity\nWHERE isclosed = false\nGROUP BY stagename\n",[1505],{"type":24,"tag":283,"props":1506,"children":1507},{"__ignoreMap":7},[1508,1529,1557,1577,1596,1615],{"type":24,"tag":738,"props":1509,"children":1510},{"class":740,"line":741},[1511,1515,1520,1524],{"type":24,"tag":738,"props":1512,"children":1513},{"style":745},[1514],{"type":29,"value":288},{"type":24,"tag":738,"props":1516,"children":1517},{"style":755},[1518],{"type":29,"value":1519}," stagename   ",{"type":24,"tag":738,"props":1521,"children":1522},{"style":745},[1523],{"type":29,"value":817},{"type":24,"tag":738,"props":1525,"children":1526},{"style":755},[1527],{"type":29,"value":1528}," stage,\n",{"type":24,"tag":738,"props":1530,"children":1531},{"class":740,"line":761},[1532,1536,1540,1544,1548,1552],{"type":24,"tag":738,"props":1533,"children":1534},{"style":769},[1535],{"type":29,"value":1385},{"type":24,"tag":738,"props":1537,"children":1538},{"style":755},[1539],{"type":29,"value":1136},{"type":24,"tag":738,"props":1541,"children":1542},{"style":745},[1543],{"type":29,"value":1141},{"type":24,"tag":738,"props":1545,"children":1546},{"style":755},[1547],{"type":29,"value":1146},{"type":24,"tag":738,"props":1549,"children":1550},{"style":745},[1551],{"type":29,"value":817},{"type":24,"tag":738,"props":1553,"children":1554},{"style":755},[1555],{"type":29,"value":1556}," deals,\n",{"type":24,"tag":738,"props":1558,"children":1559},{"class":740,"line":877},[1560,1564,1568,1572],{"type":24,"tag":738,"props":1561,"children":1562},{"style":769},[1563],{"type":29,"value":1163},{"type":24,"tag":738,"props":1565,"children":1566},{"style":755},[1567],{"type":29,"value":1168},{"type":24,"tag":738,"props":1569,"children":1570},{"style":745},[1571],{"type":29,"value":817},{"type":24,"tag":738,"props":1573,"children":1574},{"style":755},[1575],{"type":29,"value":1576}," pipeline\n",{"type":24,"tag":738,"props":1578,"children":1579},{"class":740,"line":900},[1580,1584,1588,1592],{"type":24,"tag":738,"props":1581,"children":1582},{"style":745},[1583],{"type":29,"value":296},{"type":24,"tag":738,"props":1585,"children":1586},{"style":769},[1587],{"type":29,"value":772},{"type":24,"tag":738,"props":1589,"children":1590},{"style":755},[1591],{"type":29,"value":654},{"type":24,"tag":738,"props":1593,"children":1594},{"style":769},[1595],{"type":29,"value":781},{"type":24,"tag":738,"props":1597,"children":1598},{"class":740,"line":1070},[1599,1603,1607,1611],{"type":24,"tag":738,"props":1600,"children":1601},{"style":745},[1602],{"type":29,"value":303},{"type":24,"tag":738,"props":1604,"children":1605},{"style":755},[1606],{"type":29,"value":887},{"type":24,"tag":738,"props":1608,"children":1609},{"style":745},[1610],{"type":29,"value":892},{"type":24,"tag":738,"props":1612,"children":1613},{"style":755},[1614],{"type":29,"value":897},{"type":24,"tag":738,"props":1616,"children":1617},{"class":740,"line":1241},[1618,1622],{"type":24,"tag":738,"props":1619,"children":1620},{"style":745},[1621],{"type":29,"value":324},{"type":24,"tag":738,"props":1623,"children":1624},{"style":755},[1625],{"type":29,"value":1626}," stagename\n",{"type":24,"tag":25,"props":1628,"children":1629},{},[1630,1635,1637,1642,1644,1649,1651,1656,1658,1664],{"type":24,"tag":283,"props":1631,"children":1633},{"className":1632},[],[1634],{"type":29,"value":324},{"type":29,"value":1636}," gives you one row per value, the same as grouping a report by Stage. Rule of thumb: if something's in your ",{"type":24,"tag":283,"props":1638,"children":1640},{"className":1639},[],[1641],{"type":29,"value":288},{"type":29,"value":1643}," and it isn't wrapped in a function like ",{"type":24,"tag":283,"props":1645,"children":1647},{"className":1646},[],[1648],{"type":29,"value":339},{"type":29,"value":1650},", it goes in the ",{"type":24,"tag":283,"props":1652,"children":1654},{"className":1653},[],[1655],{"type":29,"value":324},{"type":29,"value":1657},". ",{"type":24,"tag":283,"props":1659,"children":1661},{"className":1660},[],[1662],{"type":29,"value":1663},"GROUP BY 1",{"type":29,"value":1665}," is shorthand for \"group by the first column.\"",{"type":24,"tag":532,"props":1667,"children":1669},{"id":1668},"labeling-rows-with-case-when",[1670],{"type":29,"value":144},{"type":24,"tag":25,"props":1672,"children":1673},{},[1674,1679],{"type":24,"tag":283,"props":1675,"children":1677},{"className":1676},[],[1678],{"type":29,"value":410},{"type":29,"value":1680}," is SQL's version of if\u002Fthen. It does the same job as a bucket field in a report:",{"type":24,"tag":729,"props":1682,"children":1684},{"className":731,"code":1683,"language":15,"meta":7,"style":7},"SELECT CASE WHEN amount >= 100000 THEN 'Large'\n            WHEN amount >= 25000  THEN 'Medium'\n            WHEN amount > 0       THEN 'Small'\n            ELSE 'No amount'\n       END         AS deal_size,\n       COUNT(*)    AS open_deals,\n       SUM(amount) AS pipeline\nFROM salesforce.opportunity\nWHERE isclosed = false\nGROUP BY 1\nORDER BY pipeline DESC NULLS LAST\n",[1685],{"type":24,"tag":283,"props":1686,"children":1687},{"__ignoreMap":7},[1688,1730,1761,1791,1804,1822,1849,1869,1889,1909,1922],{"type":24,"tag":738,"props":1689,"children":1690},{"class":740,"line":741},[1691,1695,1700,1705,1709,1714,1719,1724],{"type":24,"tag":738,"props":1692,"children":1693},{"style":745},[1694],{"type":29,"value":288},{"type":24,"tag":738,"props":1696,"children":1697},{"style":745},[1698],{"type":29,"value":1699}," CASE",{"type":24,"tag":738,"props":1701,"children":1702},{"style":745},[1703],{"type":29,"value":1704}," WHEN",{"type":24,"tag":738,"props":1706,"children":1707},{"style":755},[1708],{"type":29,"value":911},{"type":24,"tag":738,"props":1710,"children":1711},{"style":745},[1712],{"type":29,"value":1713},">=",{"type":24,"tag":738,"props":1715,"children":1716},{"style":769},[1717],{"type":29,"value":1718}," 100000",{"type":24,"tag":738,"props":1720,"children":1721},{"style":745},[1722],{"type":29,"value":1723}," THEN",{"type":24,"tag":738,"props":1725,"children":1727},{"style":1726},"--shiki-default:#032F62;--shiki-dark:#9ECBFF",[1728],{"type":29,"value":1729}," 'Large'\n",{"type":24,"tag":738,"props":1731,"children":1732},{"class":740,"line":761},[1733,1738,1742,1746,1751,1756],{"type":24,"tag":738,"props":1734,"children":1735},{"style":745},[1736],{"type":29,"value":1737},"            WHEN",{"type":24,"tag":738,"props":1739,"children":1740},{"style":755},[1741],{"type":29,"value":911},{"type":24,"tag":738,"props":1743,"children":1744},{"style":745},[1745],{"type":29,"value":1713},{"type":24,"tag":738,"props":1747,"children":1748},{"style":769},[1749],{"type":29,"value":1750}," 25000",{"type":24,"tag":738,"props":1752,"children":1753},{"style":745},[1754],{"type":29,"value":1755},"  THEN",{"type":24,"tag":738,"props":1757,"children":1758},{"style":1726},[1759],{"type":29,"value":1760}," 'Medium'\n",{"type":24,"tag":738,"props":1762,"children":1763},{"class":740,"line":877},[1764,1768,1772,1776,1781,1786],{"type":24,"tag":738,"props":1765,"children":1766},{"style":745},[1767],{"type":29,"value":1737},{"type":24,"tag":738,"props":1769,"children":1770},{"style":755},[1771],{"type":29,"value":911},{"type":24,"tag":738,"props":1773,"children":1774},{"style":745},[1775],{"type":29,"value":916},{"type":24,"tag":738,"props":1777,"children":1778},{"style":769},[1779],{"type":29,"value":1780}," 0",{"type":24,"tag":738,"props":1782,"children":1783},{"style":745},[1784],{"type":29,"value":1785},"       THEN",{"type":24,"tag":738,"props":1787,"children":1788},{"style":1726},[1789],{"type":29,"value":1790}," 'Small'\n",{"type":24,"tag":738,"props":1792,"children":1793},{"class":740,"line":900},[1794,1799],{"type":24,"tag":738,"props":1795,"children":1796},{"style":745},[1797],{"type":29,"value":1798},"            ELSE",{"type":24,"tag":738,"props":1800,"children":1801},{"style":1726},[1802],{"type":29,"value":1803}," 'No amount'\n",{"type":24,"tag":738,"props":1805,"children":1806},{"class":740,"line":1070},[1807,1812,1817],{"type":24,"tag":738,"props":1808,"children":1809},{"style":745},[1810],{"type":29,"value":1811},"       END",{"type":24,"tag":738,"props":1813,"children":1814},{"style":745},[1815],{"type":29,"value":1816},"         AS",{"type":24,"tag":738,"props":1818,"children":1819},{"style":755},[1820],{"type":29,"value":1821}," deal_size,\n",{"type":24,"tag":738,"props":1823,"children":1824},{"class":740,"line":1241},[1825,1829,1833,1837,1841,1845],{"type":24,"tag":738,"props":1826,"children":1827},{"style":769},[1828],{"type":29,"value":1385},{"type":24,"tag":738,"props":1830,"children":1831},{"style":755},[1832],{"type":29,"value":1136},{"type":24,"tag":738,"props":1834,"children":1835},{"style":745},[1836],{"type":29,"value":1141},{"type":24,"tag":738,"props":1838,"children":1839},{"style":755},[1840],{"type":29,"value":1146},{"type":24,"tag":738,"props":1842,"children":1843},{"style":745},[1844],{"type":29,"value":817},{"type":24,"tag":738,"props":1846,"children":1847},{"style":755},[1848],{"type":29,"value":1155},{"type":24,"tag":738,"props":1850,"children":1852},{"class":740,"line":1851},7,[1853,1857,1861,1865],{"type":24,"tag":738,"props":1854,"children":1855},{"style":769},[1856],{"type":29,"value":1163},{"type":24,"tag":738,"props":1858,"children":1859},{"style":755},[1860],{"type":29,"value":1168},{"type":24,"tag":738,"props":1862,"children":1863},{"style":745},[1864],{"type":29,"value":817},{"type":24,"tag":738,"props":1866,"children":1867},{"style":755},[1868],{"type":29,"value":1576},{"type":24,"tag":738,"props":1870,"children":1872},{"class":740,"line":1871},8,[1873,1877,1881,1885],{"type":24,"tag":738,"props":1874,"children":1875},{"style":745},[1876],{"type":29,"value":296},{"type":24,"tag":738,"props":1878,"children":1879},{"style":769},[1880],{"type":29,"value":772},{"type":24,"tag":738,"props":1882,"children":1883},{"style":755},[1884],{"type":29,"value":654},{"type":24,"tag":738,"props":1886,"children":1887},{"style":769},[1888],{"type":29,"value":781},{"type":24,"tag":738,"props":1890,"children":1892},{"class":740,"line":1891},9,[1893,1897,1901,1905],{"type":24,"tag":738,"props":1894,"children":1895},{"style":745},[1896],{"type":29,"value":303},{"type":24,"tag":738,"props":1898,"children":1899},{"style":755},[1900],{"type":29,"value":887},{"type":24,"tag":738,"props":1902,"children":1903},{"style":745},[1904],{"type":29,"value":892},{"type":24,"tag":738,"props":1906,"children":1907},{"style":755},[1908],{"type":29,"value":897},{"type":24,"tag":738,"props":1910,"children":1912},{"class":740,"line":1911},10,[1913,1917],{"type":24,"tag":738,"props":1914,"children":1915},{"style":745},[1916],{"type":29,"value":324},{"type":24,"tag":738,"props":1918,"children":1919},{"style":769},[1920],{"type":29,"value":1921}," 1\n",{"type":24,"tag":738,"props":1923,"children":1925},{"class":740,"line":1924},11,[1926,1930,1935,1939,1943],{"type":24,"tag":738,"props":1927,"children":1928},{"style":745},[1929],{"type":29,"value":310},{"type":24,"tag":738,"props":1931,"children":1932},{"style":755},[1933],{"type":29,"value":1934}," pipeline ",{"type":24,"tag":738,"props":1936,"children":1937},{"style":745},[1938],{"type":29,"value":662},{"type":24,"tag":738,"props":1940,"children":1941},{"style":745},[1942],{"type":29,"value":1062},{"type":24,"tag":738,"props":1944,"children":1945},{"style":745},[1946],{"type":29,"value":1067},{"type":24,"tag":25,"props":1948,"children":1949},{},[1950,1952,1958],{"type":29,"value":1951},"Conditions are checked top to bottom, and a row gets the label of the first one that's true. A $150,000 deal is \"Large,\" even though it's also over $25,000. Anything left over, blank amounts included, falls through to ",{"type":24,"tag":283,"props":1953,"children":1955},{"className":1954},[],[1956],{"type":29,"value":1957},"ELSE",{"type":29,"value":654},{"type":24,"tag":532,"props":1960,"children":1962},{"id":1961},"joining-tables",[1963],{"type":29,"value":153},{"type":24,"tag":729,"props":1965,"children":1967},{"className":731,"code":1966,"language":15,"meta":7,"style":7},"SELECT o.name AS opportunity,\n       a.name AS account,\n       o.amount\nFROM salesforce.opportunity o\nJOIN salesforce.account a ON a.id = o.accountid\n",[1968],{"type":24,"tag":283,"props":1969,"children":1970},{"__ignoreMap":7},[1971,2002,2027,2044,2069],{"type":24,"tag":738,"props":1972,"children":1973},{"class":740,"line":741},[1974,1978,1983,1987,1992,1997],{"type":24,"tag":738,"props":1975,"children":1976},{"style":745},[1977],{"type":29,"value":288},{"type":24,"tag":738,"props":1979,"children":1980},{"style":769},[1981],{"type":29,"value":1982}," o",{"type":24,"tag":738,"props":1984,"children":1985},{"style":755},[1986],{"type":29,"value":654},{"type":24,"tag":738,"props":1988,"children":1989},{"style":769},[1990],{"type":29,"value":1991},"name",{"type":24,"tag":738,"props":1993,"children":1994},{"style":745},[1995],{"type":29,"value":1996}," AS",{"type":24,"tag":738,"props":1998,"children":1999},{"style":755},[2000],{"type":29,"value":2001}," opportunity,\n",{"type":24,"tag":738,"props":2003,"children":2004},{"class":740,"line":761},[2005,2010,2014,2018,2022],{"type":24,"tag":738,"props":2006,"children":2007},{"style":769},[2008],{"type":29,"value":2009},"       a",{"type":24,"tag":738,"props":2011,"children":2012},{"style":755},[2013],{"type":29,"value":654},{"type":24,"tag":738,"props":2015,"children":2016},{"style":769},[2017],{"type":29,"value":1991},{"type":24,"tag":738,"props":2019,"children":2020},{"style":745},[2021],{"type":29,"value":1996},{"type":24,"tag":738,"props":2023,"children":2024},{"style":755},[2025],{"type":29,"value":2026}," account,\n",{"type":24,"tag":738,"props":2028,"children":2029},{"class":740,"line":877},[2030,2035,2039],{"type":24,"tag":738,"props":2031,"children":2032},{"style":769},[2033],{"type":29,"value":2034},"       o",{"type":24,"tag":738,"props":2036,"children":2037},{"style":755},[2038],{"type":29,"value":654},{"type":24,"tag":738,"props":2040,"children":2041},{"style":769},[2042],{"type":29,"value":2043},"amount\n",{"type":24,"tag":738,"props":2045,"children":2046},{"class":740,"line":900},[2047,2051,2055,2059,2064],{"type":24,"tag":738,"props":2048,"children":2049},{"style":745},[2050],{"type":29,"value":296},{"type":24,"tag":738,"props":2052,"children":2053},{"style":769},[2054],{"type":29,"value":772},{"type":24,"tag":738,"props":2056,"children":2057},{"style":755},[2058],{"type":29,"value":654},{"type":24,"tag":738,"props":2060,"children":2061},{"style":769},[2062],{"type":29,"value":2063},"opportunity",{"type":24,"tag":738,"props":2065,"children":2066},{"style":755},[2067],{"type":29,"value":2068}," o\n",{"type":24,"tag":738,"props":2070,"children":2071},{"class":740,"line":1070},[2072,2077,2081,2085,2090,2095,2100,2105,2109,2114,2119,2123,2127],{"type":24,"tag":738,"props":2073,"children":2074},{"style":745},[2075],{"type":29,"value":2076},"JOIN",{"type":24,"tag":738,"props":2078,"children":2079},{"style":769},[2080],{"type":29,"value":772},{"type":24,"tag":738,"props":2082,"children":2083},{"style":755},[2084],{"type":29,"value":654},{"type":24,"tag":738,"props":2086,"children":2087},{"style":769},[2088],{"type":29,"value":2089},"account",{"type":24,"tag":738,"props":2091,"children":2092},{"style":755},[2093],{"type":29,"value":2094}," a ",{"type":24,"tag":738,"props":2096,"children":2097},{"style":745},[2098],{"type":29,"value":2099},"ON",{"type":24,"tag":738,"props":2101,"children":2102},{"style":769},[2103],{"type":29,"value":2104}," a",{"type":24,"tag":738,"props":2106,"children":2107},{"style":755},[2108],{"type":29,"value":654},{"type":24,"tag":738,"props":2110,"children":2111},{"style":769},[2112],{"type":29,"value":2113},"id",{"type":24,"tag":738,"props":2115,"children":2116},{"style":745},[2117],{"type":29,"value":2118}," =",{"type":24,"tag":738,"props":2120,"children":2121},{"style":769},[2122],{"type":29,"value":1982},{"type":24,"tag":738,"props":2124,"children":2125},{"style":755},[2126],{"type":29,"value":654},{"type":24,"tag":738,"props":2128,"children":2129},{"style":769},[2130],{"type":29,"value":2131},"accountid\n",{"type":24,"tag":25,"props":2133,"children":2134},{},[2135,2137,2141,2143,2149,2151,2156],{"type":29,"value":2136},"An opportunity only stores its account's id; the name lives on the account. A ",{"type":24,"tag":350,"props":2138,"children":2139},{},[2140],{"type":29,"value":2076},{"type":29,"value":2142}," connects the two using the value they share, ",{"type":24,"tag":283,"props":2144,"children":2146},{"className":2145},[],[2147],{"type":29,"value":2148},"accountid",{"type":29,"value":2150}," on the opportunity and ",{"type":24,"tag":283,"props":2152,"children":2154},{"className":2153},[],[2155],{"type":29,"value":2113},{"type":29,"value":2157}," on the account. Salesforce calls that a lookup. SQL calls it a join.",{"type":24,"tag":25,"props":2159,"children":2160},{},[2161,2167,2168,2173,2175,2180],{"type":24,"tag":283,"props":2162,"children":2164},{"className":2163},[],[2165],{"type":29,"value":2166},"o",{"type":29,"value":333},{"type":24,"tag":283,"props":2169,"children":2171},{"className":2170},[],[2172],{"type":29,"value":52},{"type":29,"value":2174}," are just short nicknames for the tables, so it's clear whose ",{"type":24,"tag":283,"props":2176,"children":2178},{"className":2177},[],[2179],{"type":29,"value":1991},{"type":29,"value":2181}," is whose.",{"type":24,"tag":25,"props":2183,"children":2184},{},[2185,2187,2192,2194,2200],{"type":29,"value":2186},"A plain ",{"type":24,"tag":283,"props":2188,"children":2190},{"className":2189},[],[2191],{"type":29,"value":2076},{"type":29,"value":2193}," only keeps rows that match on both sides. A ",{"type":24,"tag":283,"props":2195,"children":2197},{"className":2196},[],[2198],{"type":29,"value":2199},"LEFT JOIN",{"type":29,"value":2201}," keeps every row from the first table whether it matches or not, and leaves the other side blank. Reach for it when \"no match\" is part of what you're asking, like finding accounts with no opportunities.",{"type":24,"tag":37,"props":2203,"children":2205},{"id":2204},"advanced-topics",[2206],{"type":29,"value":162},{"type":24,"tag":25,"props":2208,"children":2209},{},[2210],{"type":29,"value":2211},"Now for the stuff reports can't do.",{"type":24,"tag":532,"props":2213,"children":2215},{"id":2214},"combining-leads-and-contacts-with-union",[2216],{"type":29,"value":174},{"type":24,"tag":25,"props":2218,"children":2219},{},[2220,2222,2228],{"type":29,"value":2221},"People in Salesforce live in two places, leads and contacts. ",{"type":24,"tag":283,"props":2223,"children":2225},{"className":2224},[],[2226],{"type":29,"value":2227},"UNION ALL",{"type":29,"value":2229}," stacks two queries on top of each other:",{"type":24,"tag":729,"props":2231,"children":2233},{"className":731,"code":2232,"language":15,"meta":7,"style":7},"SELECT 'Lead' AS type, name, email, company AS account_name\nFROM salesforce.lead\nWHERE isconverted = false\n  AND isdeleted = false\nUNION ALL\nSELECT 'Contact', c.name, c.email, a.name\nFROM salesforce.contact c\nLEFT JOIN salesforce.account a ON a.id = c.accountid\nWHERE c.isdeleted = false\n",[2234],{"type":24,"tag":283,"props":2235,"children":2236},{"__ignoreMap":7},[2237,2280,2300,2320,2340,2348,2411,2436,2492],{"type":24,"tag":738,"props":2238,"children":2239},{"class":740,"line":741},[2240,2244,2249,2253,2258,2262,2266,2271,2275],{"type":24,"tag":738,"props":2241,"children":2242},{"style":745},[2243],{"type":29,"value":288},{"type":24,"tag":738,"props":2245,"children":2246},{"style":1726},[2247],{"type":29,"value":2248}," 'Lead'",{"type":24,"tag":738,"props":2250,"children":2251},{"style":745},[2252],{"type":29,"value":1996},{"type":24,"tag":738,"props":2254,"children":2255},{"style":745},[2256],{"type":29,"value":2257}," type",{"type":24,"tag":738,"props":2259,"children":2260},{"style":755},[2261],{"type":29,"value":290},{"type":24,"tag":738,"props":2263,"children":2264},{"style":745},[2265],{"type":29,"value":1991},{"type":24,"tag":738,"props":2267,"children":2268},{"style":755},[2269],{"type":29,"value":2270},", email, company ",{"type":24,"tag":738,"props":2272,"children":2273},{"style":745},[2274],{"type":29,"value":817},{"type":24,"tag":738,"props":2276,"children":2277},{"style":755},[2278],{"type":29,"value":2279}," account_name\n",{"type":24,"tag":738,"props":2281,"children":2282},{"class":740,"line":761},[2283,2287,2291,2295],{"type":24,"tag":738,"props":2284,"children":2285},{"style":745},[2286],{"type":29,"value":296},{"type":24,"tag":738,"props":2288,"children":2289},{"style":769},[2290],{"type":29,"value":772},{"type":24,"tag":738,"props":2292,"children":2293},{"style":755},[2294],{"type":29,"value":654},{"type":24,"tag":738,"props":2296,"children":2297},{"style":769},[2298],{"type":29,"value":2299},"lead\n",{"type":24,"tag":738,"props":2301,"children":2302},{"class":740,"line":877},[2303,2307,2312,2316],{"type":24,"tag":738,"props":2304,"children":2305},{"style":745},[2306],{"type":29,"value":303},{"type":24,"tag":738,"props":2308,"children":2309},{"style":755},[2310],{"type":29,"value":2311}," isconverted ",{"type":24,"tag":738,"props":2313,"children":2314},{"style":745},[2315],{"type":29,"value":892},{"type":24,"tag":738,"props":2317,"children":2318},{"style":755},[2319],{"type":29,"value":897},{"type":24,"tag":738,"props":2321,"children":2322},{"class":740,"line":900},[2323,2327,2332,2336],{"type":24,"tag":738,"props":2324,"children":2325},{"style":745},[2326],{"type":29,"value":906},{"type":24,"tag":738,"props":2328,"children":2329},{"style":755},[2330],{"type":29,"value":2331}," isdeleted ",{"type":24,"tag":738,"props":2333,"children":2334},{"style":745},[2335],{"type":29,"value":892},{"type":24,"tag":738,"props":2337,"children":2338},{"style":755},[2339],{"type":29,"value":897},{"type":24,"tag":738,"props":2341,"children":2342},{"class":740,"line":1070},[2343],{"type":24,"tag":738,"props":2344,"children":2345},{"style":745},[2346],{"type":29,"value":2347},"UNION ALL\n",{"type":24,"tag":738,"props":2349,"children":2350},{"class":740,"line":1241},[2351,2355,2360,2364,2369,2373,2377,2381,2385,2389,2394,2398,2402,2406],{"type":24,"tag":738,"props":2352,"children":2353},{"style":745},[2354],{"type":29,"value":288},{"type":24,"tag":738,"props":2356,"children":2357},{"style":1726},[2358],{"type":29,"value":2359}," 'Contact'",{"type":24,"tag":738,"props":2361,"children":2362},{"style":755},[2363],{"type":29,"value":290},{"type":24,"tag":738,"props":2365,"children":2366},{"style":769},[2367],{"type":29,"value":2368},"c",{"type":24,"tag":738,"props":2370,"children":2371},{"style":755},[2372],{"type":29,"value":654},{"type":24,"tag":738,"props":2374,"children":2375},{"style":769},[2376],{"type":29,"value":1991},{"type":24,"tag":738,"props":2378,"children":2379},{"style":755},[2380],{"type":29,"value":290},{"type":24,"tag":738,"props":2382,"children":2383},{"style":769},[2384],{"type":29,"value":2368},{"type":24,"tag":738,"props":2386,"children":2387},{"style":755},[2388],{"type":29,"value":654},{"type":24,"tag":738,"props":2390,"children":2391},{"style":769},[2392],{"type":29,"value":2393},"email",{"type":24,"tag":738,"props":2395,"children":2396},{"style":755},[2397],{"type":29,"value":290},{"type":24,"tag":738,"props":2399,"children":2400},{"style":769},[2401],{"type":29,"value":52},{"type":24,"tag":738,"props":2403,"children":2404},{"style":755},[2405],{"type":29,"value":654},{"type":24,"tag":738,"props":2407,"children":2408},{"style":769},[2409],{"type":29,"value":2410},"name\n",{"type":24,"tag":738,"props":2412,"children":2413},{"class":740,"line":1851},[2414,2418,2422,2426,2431],{"type":24,"tag":738,"props":2415,"children":2416},{"style":745},[2417],{"type":29,"value":296},{"type":24,"tag":738,"props":2419,"children":2420},{"style":769},[2421],{"type":29,"value":772},{"type":24,"tag":738,"props":2423,"children":2424},{"style":755},[2425],{"type":29,"value":654},{"type":24,"tag":738,"props":2427,"children":2428},{"style":769},[2429],{"type":29,"value":2430},"contact",{"type":24,"tag":738,"props":2432,"children":2433},{"style":755},[2434],{"type":29,"value":2435}," c\n",{"type":24,"tag":738,"props":2437,"children":2438},{"class":740,"line":1871},[2439,2443,2447,2451,2455,2459,2463,2467,2471,2475,2479,2484,2488],{"type":24,"tag":738,"props":2440,"children":2441},{"style":745},[2442],{"type":29,"value":2199},{"type":24,"tag":738,"props":2444,"children":2445},{"style":769},[2446],{"type":29,"value":772},{"type":24,"tag":738,"props":2448,"children":2449},{"style":755},[2450],{"type":29,"value":654},{"type":24,"tag":738,"props":2452,"children":2453},{"style":769},[2454],{"type":29,"value":2089},{"type":24,"tag":738,"props":2456,"children":2457},{"style":755},[2458],{"type":29,"value":2094},{"type":24,"tag":738,"props":2460,"children":2461},{"style":745},[2462],{"type":29,"value":2099},{"type":24,"tag":738,"props":2464,"children":2465},{"style":769},[2466],{"type":29,"value":2104},{"type":24,"tag":738,"props":2468,"children":2469},{"style":755},[2470],{"type":29,"value":654},{"type":24,"tag":738,"props":2472,"children":2473},{"style":769},[2474],{"type":29,"value":2113},{"type":24,"tag":738,"props":2476,"children":2477},{"style":745},[2478],{"type":29,"value":2118},{"type":24,"tag":738,"props":2480,"children":2481},{"style":769},[2482],{"type":29,"value":2483}," c",{"type":24,"tag":738,"props":2485,"children":2486},{"style":755},[2487],{"type":29,"value":654},{"type":24,"tag":738,"props":2489,"children":2490},{"style":769},[2491],{"type":29,"value":2131},{"type":24,"tag":738,"props":2493,"children":2494},{"class":740,"line":1891},[2495,2499,2503,2507,2512,2516],{"type":24,"tag":738,"props":2496,"children":2497},{"style":745},[2498],{"type":29,"value":303},{"type":24,"tag":738,"props":2500,"children":2501},{"style":769},[2502],{"type":29,"value":2483},{"type":24,"tag":738,"props":2504,"children":2505},{"style":755},[2506],{"type":29,"value":654},{"type":24,"tag":738,"props":2508,"children":2509},{"style":769},[2510],{"type":29,"value":2511},"isdeleted",{"type":24,"tag":738,"props":2513,"children":2514},{"style":745},[2515],{"type":29,"value":2118},{"type":24,"tag":738,"props":2517,"children":2518},{"style":755},[2519],{"type":29,"value":897},{"type":24,"tag":25,"props":2521,"children":2522},{},[2523,2525,2531,2533,2539],{"type":29,"value":2524},"Both halves need the same columns in the same order. Don't skip ",{"type":24,"tag":283,"props":2526,"children":2528},{"className":2527},[],[2529],{"type":29,"value":2530},"isconverted = false",{"type":29,"value":2532},": a converted lead also exists as a contact, and without that filter you'd count those people twice. ",{"type":24,"tag":283,"props":2534,"children":2536},{"className":2535},[],[2537],{"type":29,"value":2538},"isdeleted = false",{"type":29,"value":2540}," keeps out anything sitting in the Recycle Bin.",{"type":24,"tag":25,"props":2542,"children":2543},{},[2544,2546,2552,2554,2560,2562,2567],{"type":29,"value":2545},"Plain ",{"type":24,"tag":283,"props":2547,"children":2549},{"className":2548},[],[2550],{"type":29,"value":2551},"UNION",{"type":29,"value":2553}," (no ",{"type":24,"tag":283,"props":2555,"children":2557},{"className":2556},[],[2558],{"type":29,"value":2559},"ALL",{"type":29,"value":2561},") also strips out exact duplicate rows. It's slower and can hide problems, so I stick with ",{"type":24,"tag":283,"props":2563,"children":2565},{"className":2564},[],[2566],{"type":29,"value":2227},{"type":29,"value":2568}," unless I actually want the de-duplication.",{"type":24,"tag":532,"props":2570,"children":2572},{"id":2571},"breaking-up-a-query-with-ctes",[2573],{"type":29,"value":183},{"type":24,"tag":25,"props":2575,"children":2576},{},[2577,2579,2585],{"type":29,"value":2578},"A CTE (the ",{"type":24,"tag":283,"props":2580,"children":2582},{"className":2581},[],[2583],{"type":29,"value":2584},"WITH",{"type":29,"value":2586}," at the top) lets you give a query a name and then use it like a table further down:",{"type":24,"tag":729,"props":2588,"children":2590},{"className":731,"code":2589,"language":15,"meta":7,"style":7},"WITH people AS (\n  SELECT LOWER(TRIM(email)) AS email FROM salesforce.lead WHERE isconverted = false\n  UNION ALL\n  SELECT LOWER(TRIM(email)) FROM salesforce.contact\n)\nSELECT email, COUNT(*) AS records\nFROM people\nWHERE email IS NOT NULL\nGROUP BY email\nHAVING COUNT(*) > 1\nORDER BY records DESC\n",[2591],{"type":24,"tag":283,"props":2592,"children":2593},{"__ignoreMap":7},[2594,2615,2685,2693,2733,2741,2779,2791,2807,2819,2851],{"type":24,"tag":738,"props":2595,"children":2596},{"class":740,"line":741},[2597,2601,2606,2610],{"type":24,"tag":738,"props":2598,"children":2599},{"style":745},[2600],{"type":29,"value":2584},{"type":24,"tag":738,"props":2602,"children":2603},{"style":755},[2604],{"type":29,"value":2605}," people ",{"type":24,"tag":738,"props":2607,"children":2608},{"style":745},[2609],{"type":29,"value":817},{"type":24,"tag":738,"props":2611,"children":2612},{"style":755},[2613],{"type":29,"value":2614}," (\n",{"type":24,"tag":738,"props":2616,"children":2617},{"class":740,"line":761},[2618,2623,2628,2632,2637,2642,2646,2651,2655,2659,2663,2668,2673,2677,2681],{"type":24,"tag":738,"props":2619,"children":2620},{"style":745},[2621],{"type":29,"value":2622},"  SELECT",{"type":24,"tag":738,"props":2624,"children":2625},{"style":769},[2626],{"type":29,"value":2627}," LOWER",{"type":24,"tag":738,"props":2629,"children":2630},{"style":755},[2631],{"type":29,"value":1136},{"type":24,"tag":738,"props":2633,"children":2634},{"style":769},[2635],{"type":29,"value":2636},"TRIM",{"type":24,"tag":738,"props":2638,"children":2639},{"style":755},[2640],{"type":29,"value":2641},"(email)) ",{"type":24,"tag":738,"props":2643,"children":2644},{"style":745},[2645],{"type":29,"value":817},{"type":24,"tag":738,"props":2647,"children":2648},{"style":755},[2649],{"type":29,"value":2650}," email ",{"type":24,"tag":738,"props":2652,"children":2653},{"style":745},[2654],{"type":29,"value":296},{"type":24,"tag":738,"props":2656,"children":2657},{"style":769},[2658],{"type":29,"value":772},{"type":24,"tag":738,"props":2660,"children":2661},{"style":755},[2662],{"type":29,"value":654},{"type":24,"tag":738,"props":2664,"children":2665},{"style":769},[2666],{"type":29,"value":2667},"lead",{"type":24,"tag":738,"props":2669,"children":2670},{"style":745},[2671],{"type":29,"value":2672}," WHERE",{"type":24,"tag":738,"props":2674,"children":2675},{"style":755},[2676],{"type":29,"value":2311},{"type":24,"tag":738,"props":2678,"children":2679},{"style":745},[2680],{"type":29,"value":892},{"type":24,"tag":738,"props":2682,"children":2683},{"style":755},[2684],{"type":29,"value":897},{"type":24,"tag":738,"props":2686,"children":2687},{"class":740,"line":877},[2688],{"type":24,"tag":738,"props":2689,"children":2690},{"style":745},[2691],{"type":29,"value":2692},"  UNION ALL\n",{"type":24,"tag":738,"props":2694,"children":2695},{"class":740,"line":900},[2696,2700,2704,2708,2712,2716,2720,2724,2728],{"type":24,"tag":738,"props":2697,"children":2698},{"style":745},[2699],{"type":29,"value":2622},{"type":24,"tag":738,"props":2701,"children":2702},{"style":769},[2703],{"type":29,"value":2627},{"type":24,"tag":738,"props":2705,"children":2706},{"style":755},[2707],{"type":29,"value":1136},{"type":24,"tag":738,"props":2709,"children":2710},{"style":769},[2711],{"type":29,"value":2636},{"type":24,"tag":738,"props":2713,"children":2714},{"style":755},[2715],{"type":29,"value":2641},{"type":24,"tag":738,"props":2717,"children":2718},{"style":745},[2719],{"type":29,"value":296},{"type":24,"tag":738,"props":2721,"children":2722},{"style":769},[2723],{"type":29,"value":772},{"type":24,"tag":738,"props":2725,"children":2726},{"style":755},[2727],{"type":29,"value":654},{"type":24,"tag":738,"props":2729,"children":2730},{"style":769},[2731],{"type":29,"value":2732},"contact\n",{"type":24,"tag":738,"props":2734,"children":2735},{"class":740,"line":1070},[2736],{"type":24,"tag":738,"props":2737,"children":2738},{"style":755},[2739],{"type":29,"value":2740},")\n",{"type":24,"tag":738,"props":2742,"children":2743},{"class":740,"line":1241},[2744,2748,2753,2757,2761,2765,2770,2774],{"type":24,"tag":738,"props":2745,"children":2746},{"style":745},[2747],{"type":29,"value":288},{"type":24,"tag":738,"props":2749,"children":2750},{"style":755},[2751],{"type":29,"value":2752}," email, ",{"type":24,"tag":738,"props":2754,"children":2755},{"style":769},[2756],{"type":29,"value":331},{"type":24,"tag":738,"props":2758,"children":2759},{"style":755},[2760],{"type":29,"value":1136},{"type":24,"tag":738,"props":2762,"children":2763},{"style":745},[2764],{"type":29,"value":1141},{"type":24,"tag":738,"props":2766,"children":2767},{"style":755},[2768],{"type":29,"value":2769},") ",{"type":24,"tag":738,"props":2771,"children":2772},{"style":745},[2773],{"type":29,"value":817},{"type":24,"tag":738,"props":2775,"children":2776},{"style":755},[2777],{"type":29,"value":2778}," records\n",{"type":24,"tag":738,"props":2780,"children":2781},{"class":740,"line":1851},[2782,2786],{"type":24,"tag":738,"props":2783,"children":2784},{"style":745},[2785],{"type":29,"value":296},{"type":24,"tag":738,"props":2787,"children":2788},{"style":755},[2789],{"type":29,"value":2790}," people\n",{"type":24,"tag":738,"props":2792,"children":2793},{"class":740,"line":1871},[2794,2798,2802],{"type":24,"tag":738,"props":2795,"children":2796},{"style":745},[2797],{"type":29,"value":303},{"type":24,"tag":738,"props":2799,"children":2800},{"style":755},[2801],{"type":29,"value":2650},{"type":24,"tag":738,"props":2803,"children":2804},{"style":745},[2805],{"type":29,"value":2806},"IS NOT NULL\n",{"type":24,"tag":738,"props":2808,"children":2809},{"class":740,"line":1891},[2810,2814],{"type":24,"tag":738,"props":2811,"children":2812},{"style":745},[2813],{"type":29,"value":324},{"type":24,"tag":738,"props":2815,"children":2816},{"style":755},[2817],{"type":29,"value":2818}," email\n",{"type":24,"tag":738,"props":2820,"children":2821},{"class":740,"line":1911},[2822,2827,2831,2835,2839,2843,2847],{"type":24,"tag":738,"props":2823,"children":2824},{"style":745},[2825],{"type":29,"value":2826},"HAVING",{"type":24,"tag":738,"props":2828,"children":2829},{"style":769},[2830],{"type":29,"value":1131},{"type":24,"tag":738,"props":2832,"children":2833},{"style":755},[2834],{"type":29,"value":1136},{"type":24,"tag":738,"props":2836,"children":2837},{"style":745},[2838],{"type":29,"value":1141},{"type":24,"tag":738,"props":2840,"children":2841},{"style":755},[2842],{"type":29,"value":2769},{"type":24,"tag":738,"props":2844,"children":2845},{"style":745},[2846],{"type":29,"value":916},{"type":24,"tag":738,"props":2848,"children":2849},{"style":769},[2850],{"type":29,"value":1921},{"type":24,"tag":738,"props":2852,"children":2853},{"class":740,"line":1924},[2854,2858,2863],{"type":24,"tag":738,"props":2855,"children":2856},{"style":745},[2857],{"type":29,"value":310},{"type":24,"tag":738,"props":2859,"children":2860},{"style":755},[2861],{"type":29,"value":2862}," records ",{"type":24,"tag":738,"props":2864,"children":2865},{"style":745},[2866],{"type":29,"value":2867},"DESC\n",{"type":24,"tag":25,"props":2869,"children":2870},{},[2871,2873,2878,2880,2885,2887,2893,2895,2901,2902,2908],{"type":29,"value":2872},"This finds email addresses that show up on more than one lead or contact. ",{"type":24,"tag":283,"props":2874,"children":2876},{"className":2875},[],[2877],{"type":29,"value":2826},{"type":29,"value":2879}," is basically a ",{"type":24,"tag":283,"props":2881,"children":2883},{"className":2882},[],[2884],{"type":29,"value":303},{"type":29,"value":2886}," for groups. It filters after the counting. ",{"type":24,"tag":283,"props":2888,"children":2890},{"className":2889},[],[2891],{"type":29,"value":2892},"LOWER(TRIM(...))",{"type":29,"value":2894}," cleans up the emails first, since ",{"type":24,"tag":283,"props":2896,"children":2898},{"className":2897},[],[2899],{"type":29,"value":2900},"Jane@Acme.com ",{"type":29,"value":333},{"type":24,"tag":283,"props":2903,"children":2905},{"className":2904},[],[2906],{"type":29,"value":2907},"jane@acme.com",{"type":29,"value":2909}," are the same person.",{"type":24,"tag":25,"props":2911,"children":2912},{},[2913],{"type":29,"value":2914},"The big win with CTEs is readability. Every step gets a name, and you can run each one by itself to check it.",{"type":24,"tag":532,"props":2916,"children":2918},{"id":2917},"joining-more-than-two-tables",[2919],{"type":29,"value":192},{"type":24,"tag":25,"props":2921,"children":2922},{},[2923],{"type":29,"value":2924},"There's no limit on how many tables you can join. Here's a problem most Salesforce teams have: someone who's already a contact fills out a form, and Salesforce creates a brand-new lead for them. This query finds those leads by matching their email against existing contacts. Then it pulls in the contact's account, the lead owner, the account owner and how many open deals the account has. (You can't do this in a Salesforce report, because a lead isn't linked to a contact until it's converted.)",{"type":24,"tag":729,"props":2926,"children":2928},{"className":731,"code":2927,"language":15,"meta":7,"style":7},"SELECT l.id    AS lead_id,\n       l.name  AS lead_name,\n       lo.name AS lead_owner,\n       a.name  AS account,\n       ao.name AS account_owner,\n       COUNT(DISTINCT CASE WHEN o.isclosed = false THEN o.id END) AS open_deals\nFROM salesforce.lead l\nJOIN salesforce.contact c\n  ON LOWER(TRIM(c.email)) = LOWER(TRIM(l.email))\nJOIN salesforce.account a          ON a.id = c.accountid\nLEFT JOIN salesforce.user lo       ON lo.id = l.ownerid\nJOIN salesforce.user ao            ON ao.id = a.ownerid\nLEFT JOIN salesforce.opportunity o ON o.accountid = a.id\nWHERE l.isconverted = false\nGROUP BY 1, 2, 3, 4, 5\nORDER BY open_deals DESC\n",[2929],{"type":24,"tag":283,"props":2930,"children":2931},{"__ignoreMap":7},[2932,2962,2988,3013,3036,3061,3141,3165,3188,3267,3323,3382,3440,3498,3527,3576],{"type":24,"tag":738,"props":2933,"children":2934},{"class":740,"line":741},[2935,2939,2944,2948,2952,2957],{"type":24,"tag":738,"props":2936,"children":2937},{"style":745},[2938],{"type":29,"value":288},{"type":24,"tag":738,"props":2940,"children":2941},{"style":769},[2942],{"type":29,"value":2943}," l",{"type":24,"tag":738,"props":2945,"children":2946},{"style":755},[2947],{"type":29,"value":654},{"type":24,"tag":738,"props":2949,"children":2950},{"style":769},[2951],{"type":29,"value":2113},{"type":24,"tag":738,"props":2953,"children":2954},{"style":745},[2955],{"type":29,"value":2956},"    AS",{"type":24,"tag":738,"props":2958,"children":2959},{"style":755},[2960],{"type":29,"value":2961}," lead_id,\n",{"type":24,"tag":738,"props":2963,"children":2964},{"class":740,"line":761},[2965,2970,2974,2978,2983],{"type":24,"tag":738,"props":2966,"children":2967},{"style":769},[2968],{"type":29,"value":2969},"       l",{"type":24,"tag":738,"props":2971,"children":2972},{"style":755},[2973],{"type":29,"value":654},{"type":24,"tag":738,"props":2975,"children":2976},{"style":769},[2977],{"type":29,"value":1991},{"type":24,"tag":738,"props":2979,"children":2980},{"style":745},[2981],{"type":29,"value":2982},"  AS",{"type":24,"tag":738,"props":2984,"children":2985},{"style":755},[2986],{"type":29,"value":2987}," lead_name,\n",{"type":24,"tag":738,"props":2989,"children":2990},{"class":740,"line":877},[2991,2996,3000,3004,3008],{"type":24,"tag":738,"props":2992,"children":2993},{"style":769},[2994],{"type":29,"value":2995},"       lo",{"type":24,"tag":738,"props":2997,"children":2998},{"style":755},[2999],{"type":29,"value":654},{"type":24,"tag":738,"props":3001,"children":3002},{"style":769},[3003],{"type":29,"value":1991},{"type":24,"tag":738,"props":3005,"children":3006},{"style":745},[3007],{"type":29,"value":1996},{"type":24,"tag":738,"props":3009,"children":3010},{"style":755},[3011],{"type":29,"value":3012}," lead_owner,\n",{"type":24,"tag":738,"props":3014,"children":3015},{"class":740,"line":900},[3016,3020,3024,3028,3032],{"type":24,"tag":738,"props":3017,"children":3018},{"style":769},[3019],{"type":29,"value":2009},{"type":24,"tag":738,"props":3021,"children":3022},{"style":755},[3023],{"type":29,"value":654},{"type":24,"tag":738,"props":3025,"children":3026},{"style":769},[3027],{"type":29,"value":1991},{"type":24,"tag":738,"props":3029,"children":3030},{"style":745},[3031],{"type":29,"value":2982},{"type":24,"tag":738,"props":3033,"children":3034},{"style":755},[3035],{"type":29,"value":2026},{"type":24,"tag":738,"props":3037,"children":3038},{"class":740,"line":1070},[3039,3044,3048,3052,3056],{"type":24,"tag":738,"props":3040,"children":3041},{"style":769},[3042],{"type":29,"value":3043},"       ao",{"type":24,"tag":738,"props":3045,"children":3046},{"style":755},[3047],{"type":29,"value":654},{"type":24,"tag":738,"props":3049,"children":3050},{"style":769},[3051],{"type":29,"value":1991},{"type":24,"tag":738,"props":3053,"children":3054},{"style":745},[3055],{"type":29,"value":1996},{"type":24,"tag":738,"props":3057,"children":3058},{"style":755},[3059],{"type":29,"value":3060}," account_owner,\n",{"type":24,"tag":738,"props":3062,"children":3063},{"class":740,"line":1241},[3064,3068,3072,3076,3080,3084,3088,3092,3097,3101,3106,3111,3115,3119,3123,3128,3132,3136],{"type":24,"tag":738,"props":3065,"children":3066},{"style":769},[3067],{"type":29,"value":1385},{"type":24,"tag":738,"props":3069,"children":3070},{"style":755},[3071],{"type":29,"value":1136},{"type":24,"tag":738,"props":3073,"children":3074},{"style":745},[3075],{"type":29,"value":1394},{"type":24,"tag":738,"props":3077,"children":3078},{"style":745},[3079],{"type":29,"value":1699},{"type":24,"tag":738,"props":3081,"children":3082},{"style":745},[3083],{"type":29,"value":1704},{"type":24,"tag":738,"props":3085,"children":3086},{"style":769},[3087],{"type":29,"value":1982},{"type":24,"tag":738,"props":3089,"children":3090},{"style":755},[3091],{"type":29,"value":654},{"type":24,"tag":738,"props":3093,"children":3094},{"style":769},[3095],{"type":29,"value":3096},"isclosed",{"type":24,"tag":738,"props":3098,"children":3099},{"style":745},[3100],{"type":29,"value":2118},{"type":24,"tag":738,"props":3102,"children":3103},{"style":755},[3104],{"type":29,"value":3105}," false ",{"type":24,"tag":738,"props":3107,"children":3108},{"style":745},[3109],{"type":29,"value":3110},"THEN",{"type":24,"tag":738,"props":3112,"children":3113},{"style":769},[3114],{"type":29,"value":1982},{"type":24,"tag":738,"props":3116,"children":3117},{"style":755},[3118],{"type":29,"value":654},{"type":24,"tag":738,"props":3120,"children":3121},{"style":769},[3122],{"type":29,"value":2113},{"type":24,"tag":738,"props":3124,"children":3125},{"style":745},[3126],{"type":29,"value":3127}," END",{"type":24,"tag":738,"props":3129,"children":3130},{"style":755},[3131],{"type":29,"value":2769},{"type":24,"tag":738,"props":3133,"children":3134},{"style":745},[3135],{"type":29,"value":817},{"type":24,"tag":738,"props":3137,"children":3138},{"style":755},[3139],{"type":29,"value":3140}," open_deals\n",{"type":24,"tag":738,"props":3142,"children":3143},{"class":740,"line":1851},[3144,3148,3152,3156,3160],{"type":24,"tag":738,"props":3145,"children":3146},{"style":745},[3147],{"type":29,"value":296},{"type":24,"tag":738,"props":3149,"children":3150},{"style":769},[3151],{"type":29,"value":772},{"type":24,"tag":738,"props":3153,"children":3154},{"style":755},[3155],{"type":29,"value":654},{"type":24,"tag":738,"props":3157,"children":3158},{"style":769},[3159],{"type":29,"value":2667},{"type":24,"tag":738,"props":3161,"children":3162},{"style":755},[3163],{"type":29,"value":3164}," l\n",{"type":24,"tag":738,"props":3166,"children":3167},{"class":740,"line":1871},[3168,3172,3176,3180,3184],{"type":24,"tag":738,"props":3169,"children":3170},{"style":745},[3171],{"type":29,"value":2076},{"type":24,"tag":738,"props":3173,"children":3174},{"style":769},[3175],{"type":29,"value":772},{"type":24,"tag":738,"props":3177,"children":3178},{"style":755},[3179],{"type":29,"value":654},{"type":24,"tag":738,"props":3181,"children":3182},{"style":769},[3183],{"type":29,"value":2430},{"type":24,"tag":738,"props":3185,"children":3186},{"style":755},[3187],{"type":29,"value":2435},{"type":24,"tag":738,"props":3189,"children":3190},{"class":740,"line":1891},[3191,3196,3200,3204,3208,3212,3216,3220,3224,3229,3233,3237,3241,3245,3249,3254,3258,3262],{"type":24,"tag":738,"props":3192,"children":3193},{"style":745},[3194],{"type":29,"value":3195},"  ON",{"type":24,"tag":738,"props":3197,"children":3198},{"style":769},[3199],{"type":29,"value":2627},{"type":24,"tag":738,"props":3201,"children":3202},{"style":755},[3203],{"type":29,"value":1136},{"type":24,"tag":738,"props":3205,"children":3206},{"style":769},[3207],{"type":29,"value":2636},{"type":24,"tag":738,"props":3209,"children":3210},{"style":755},[3211],{"type":29,"value":1136},{"type":24,"tag":738,"props":3213,"children":3214},{"style":769},[3215],{"type":29,"value":2368},{"type":24,"tag":738,"props":3217,"children":3218},{"style":755},[3219],{"type":29,"value":654},{"type":24,"tag":738,"props":3221,"children":3222},{"style":769},[3223],{"type":29,"value":2393},{"type":24,"tag":738,"props":3225,"children":3226},{"style":755},[3227],{"type":29,"value":3228},")) ",{"type":24,"tag":738,"props":3230,"children":3231},{"style":745},[3232],{"type":29,"value":892},{"type":24,"tag":738,"props":3234,"children":3235},{"style":769},[3236],{"type":29,"value":2627},{"type":24,"tag":738,"props":3238,"children":3239},{"style":755},[3240],{"type":29,"value":1136},{"type":24,"tag":738,"props":3242,"children":3243},{"style":769},[3244],{"type":29,"value":2636},{"type":24,"tag":738,"props":3246,"children":3247},{"style":755},[3248],{"type":29,"value":1136},{"type":24,"tag":738,"props":3250,"children":3251},{"style":769},[3252],{"type":29,"value":3253},"l",{"type":24,"tag":738,"props":3255,"children":3256},{"style":755},[3257],{"type":29,"value":654},{"type":24,"tag":738,"props":3259,"children":3260},{"style":769},[3261],{"type":29,"value":2393},{"type":24,"tag":738,"props":3263,"children":3264},{"style":755},[3265],{"type":29,"value":3266},"))\n",{"type":24,"tag":738,"props":3268,"children":3269},{"class":740,"line":1911},[3270,3274,3278,3282,3286,3291,3295,3299,3303,3307,3311,3315,3319],{"type":24,"tag":738,"props":3271,"children":3272},{"style":745},[3273],{"type":29,"value":2076},{"type":24,"tag":738,"props":3275,"children":3276},{"style":769},[3277],{"type":29,"value":772},{"type":24,"tag":738,"props":3279,"children":3280},{"style":755},[3281],{"type":29,"value":654},{"type":24,"tag":738,"props":3283,"children":3284},{"style":769},[3285],{"type":29,"value":2089},{"type":24,"tag":738,"props":3287,"children":3288},{"style":755},[3289],{"type":29,"value":3290}," a          ",{"type":24,"tag":738,"props":3292,"children":3293},{"style":745},[3294],{"type":29,"value":2099},{"type":24,"tag":738,"props":3296,"children":3297},{"style":769},[3298],{"type":29,"value":2104},{"type":24,"tag":738,"props":3300,"children":3301},{"style":755},[3302],{"type":29,"value":654},{"type":24,"tag":738,"props":3304,"children":3305},{"style":769},[3306],{"type":29,"value":2113},{"type":24,"tag":738,"props":3308,"children":3309},{"style":745},[3310],{"type":29,"value":2118},{"type":24,"tag":738,"props":3312,"children":3313},{"style":769},[3314],{"type":29,"value":2483},{"type":24,"tag":738,"props":3316,"children":3317},{"style":755},[3318],{"type":29,"value":654},{"type":24,"tag":738,"props":3320,"children":3321},{"style":769},[3322],{"type":29,"value":2131},{"type":24,"tag":738,"props":3324,"children":3325},{"class":740,"line":1924},[3326,3330,3334,3338,3343,3348,3352,3357,3361,3365,3369,3373,3377],{"type":24,"tag":738,"props":3327,"children":3328},{"style":745},[3329],{"type":29,"value":2199},{"type":24,"tag":738,"props":3331,"children":3332},{"style":769},[3333],{"type":29,"value":772},{"type":24,"tag":738,"props":3335,"children":3336},{"style":755},[3337],{"type":29,"value":654},{"type":24,"tag":738,"props":3339,"children":3340},{"style":769},[3341],{"type":29,"value":3342},"user",{"type":24,"tag":738,"props":3344,"children":3345},{"style":755},[3346],{"type":29,"value":3347}," lo       ",{"type":24,"tag":738,"props":3349,"children":3350},{"style":745},[3351],{"type":29,"value":2099},{"type":24,"tag":738,"props":3353,"children":3354},{"style":769},[3355],{"type":29,"value":3356}," lo",{"type":24,"tag":738,"props":3358,"children":3359},{"style":755},[3360],{"type":29,"value":654},{"type":24,"tag":738,"props":3362,"children":3363},{"style":769},[3364],{"type":29,"value":2113},{"type":24,"tag":738,"props":3366,"children":3367},{"style":745},[3368],{"type":29,"value":2118},{"type":24,"tag":738,"props":3370,"children":3371},{"style":769},[3372],{"type":29,"value":2943},{"type":24,"tag":738,"props":3374,"children":3375},{"style":755},[3376],{"type":29,"value":654},{"type":24,"tag":738,"props":3378,"children":3379},{"style":769},[3380],{"type":29,"value":3381},"ownerid\n",{"type":24,"tag":738,"props":3383,"children":3385},{"class":740,"line":3384},12,[3386,3390,3394,3398,3402,3407,3411,3416,3420,3424,3428,3432,3436],{"type":24,"tag":738,"props":3387,"children":3388},{"style":745},[3389],{"type":29,"value":2076},{"type":24,"tag":738,"props":3391,"children":3392},{"style":769},[3393],{"type":29,"value":772},{"type":24,"tag":738,"props":3395,"children":3396},{"style":755},[3397],{"type":29,"value":654},{"type":24,"tag":738,"props":3399,"children":3400},{"style":769},[3401],{"type":29,"value":3342},{"type":24,"tag":738,"props":3403,"children":3404},{"style":755},[3405],{"type":29,"value":3406}," ao            ",{"type":24,"tag":738,"props":3408,"children":3409},{"style":745},[3410],{"type":29,"value":2099},{"type":24,"tag":738,"props":3412,"children":3413},{"style":769},[3414],{"type":29,"value":3415}," ao",{"type":24,"tag":738,"props":3417,"children":3418},{"style":755},[3419],{"type":29,"value":654},{"type":24,"tag":738,"props":3421,"children":3422},{"style":769},[3423],{"type":29,"value":2113},{"type":24,"tag":738,"props":3425,"children":3426},{"style":745},[3427],{"type":29,"value":2118},{"type":24,"tag":738,"props":3429,"children":3430},{"style":769},[3431],{"type":29,"value":2104},{"type":24,"tag":738,"props":3433,"children":3434},{"style":755},[3435],{"type":29,"value":654},{"type":24,"tag":738,"props":3437,"children":3438},{"style":769},[3439],{"type":29,"value":3381},{"type":24,"tag":738,"props":3441,"children":3443},{"class":740,"line":3442},13,[3444,3448,3452,3456,3460,3465,3469,3473,3477,3481,3485,3489,3493],{"type":24,"tag":738,"props":3445,"children":3446},{"style":745},[3447],{"type":29,"value":2199},{"type":24,"tag":738,"props":3449,"children":3450},{"style":769},[3451],{"type":29,"value":772},{"type":24,"tag":738,"props":3453,"children":3454},{"style":755},[3455],{"type":29,"value":654},{"type":24,"tag":738,"props":3457,"children":3458},{"style":769},[3459],{"type":29,"value":2063},{"type":24,"tag":738,"props":3461,"children":3462},{"style":755},[3463],{"type":29,"value":3464}," o ",{"type":24,"tag":738,"props":3466,"children":3467},{"style":745},[3468],{"type":29,"value":2099},{"type":24,"tag":738,"props":3470,"children":3471},{"style":769},[3472],{"type":29,"value":1982},{"type":24,"tag":738,"props":3474,"children":3475},{"style":755},[3476],{"type":29,"value":654},{"type":24,"tag":738,"props":3478,"children":3479},{"style":769},[3480],{"type":29,"value":2148},{"type":24,"tag":738,"props":3482,"children":3483},{"style":745},[3484],{"type":29,"value":2118},{"type":24,"tag":738,"props":3486,"children":3487},{"style":769},[3488],{"type":29,"value":2104},{"type":24,"tag":738,"props":3490,"children":3491},{"style":755},[3492],{"type":29,"value":654},{"type":24,"tag":738,"props":3494,"children":3495},{"style":769},[3496],{"type":29,"value":3497},"id\n",{"type":24,"tag":738,"props":3499,"children":3501},{"class":740,"line":3500},14,[3502,3506,3510,3514,3519,3523],{"type":24,"tag":738,"props":3503,"children":3504},{"style":745},[3505],{"type":29,"value":303},{"type":24,"tag":738,"props":3507,"children":3508},{"style":769},[3509],{"type":29,"value":2943},{"type":24,"tag":738,"props":3511,"children":3512},{"style":755},[3513],{"type":29,"value":654},{"type":24,"tag":738,"props":3515,"children":3516},{"style":769},[3517],{"type":29,"value":3518},"isconverted",{"type":24,"tag":738,"props":3520,"children":3521},{"style":745},[3522],{"type":29,"value":2118},{"type":24,"tag":738,"props":3524,"children":3525},{"style":755},[3526],{"type":29,"value":897},{"type":24,"tag":738,"props":3528,"children":3530},{"class":740,"line":3529},15,[3531,3535,3540,3544,3549,3553,3558,3562,3567,3571],{"type":24,"tag":738,"props":3532,"children":3533},{"style":745},[3534],{"type":29,"value":324},{"type":24,"tag":738,"props":3536,"children":3537},{"style":769},[3538],{"type":29,"value":3539}," 1",{"type":24,"tag":738,"props":3541,"children":3542},{"style":755},[3543],{"type":29,"value":290},{"type":24,"tag":738,"props":3545,"children":3546},{"style":769},[3547],{"type":29,"value":3548},"2",{"type":24,"tag":738,"props":3550,"children":3551},{"style":755},[3552],{"type":29,"value":290},{"type":24,"tag":738,"props":3554,"children":3555},{"style":769},[3556],{"type":29,"value":3557},"3",{"type":24,"tag":738,"props":3559,"children":3560},{"style":755},[3561],{"type":29,"value":290},{"type":24,"tag":738,"props":3563,"children":3564},{"style":769},[3565],{"type":29,"value":3566},"4",{"type":24,"tag":738,"props":3568,"children":3569},{"style":755},[3570],{"type":29,"value":290},{"type":24,"tag":738,"props":3572,"children":3573},{"style":769},[3574],{"type":29,"value":3575},"5\n",{"type":24,"tag":738,"props":3577,"children":3579},{"class":740,"line":3578},16,[3580,3584,3589],{"type":24,"tag":738,"props":3581,"children":3582},{"style":745},[3583],{"type":29,"value":310},{"type":24,"tag":738,"props":3585,"children":3586},{"style":755},[3587],{"type":29,"value":3588}," open_deals ",{"type":24,"tag":738,"props":3590,"children":3591},{"style":745},[3592],{"type":29,"value":2867},{"type":24,"tag":25,"props":3594,"children":3595},{},[3596,3598,3604,3606,3611],{"type":29,"value":3597},"A couple of details. ",{"type":24,"tag":283,"props":3599,"children":3601},{"className":3600},[],[3602],{"type":29,"value":3603},"COUNT(DISTINCT o.id)",{"type":29,"value":3605}," counts each deal once, even when the lead's email matches two contacts at the same account. And the lead id is in the ",{"type":24,"tag":283,"props":3607,"children":3609},{"className":3608},[],[3610],{"type":29,"value":324},{"type":29,"value":3612}," because names aren't unique.",{"type":24,"tag":25,"props":3614,"children":3615},{},[3616,3618,3624,3626,3632,3634,3639],{"type":29,"value":3617},"The user table appears twice, as ",{"type":24,"tag":283,"props":3619,"children":3621},{"className":3620},[],[3622],{"type":29,"value":3623},"lo",{"type":29,"value":3625}," (lead owner) and ",{"type":24,"tag":283,"props":3627,"children":3629},{"className":3628},[],[3630],{"type":29,"value":3631},"ao",{"type":29,"value":3633}," (account owner), since it's answering two different questions. The lead owner is a ",{"type":24,"tag":283,"props":3635,"children":3637},{"className":3636},[],[3638],{"type":29,"value":2199},{"type":29,"value":3640}," because leads can be owned by a queue instead of a user.",{"type":24,"tag":25,"props":3642,"children":3643},{},[3644,3646,3651],{"type":29,"value":3645},"Sorting by ",{"type":24,"tag":283,"props":3647,"children":3649},{"className":3648},[],[3650],{"type":29,"value":1486},{"type":29,"value":3652}," floats leads at accounts with active deals to the top. Check those first, especially the ones where the lead owner and the account owner aren't the same person.",{"type":24,"tag":532,"props":3654,"children":3656},{"id":3655},"picking-one-row-per-group-with-row_number",[3657],{"type":29,"value":201},{"type":24,"tag":25,"props":3659,"children":3660},{},[3661,3663,3668],{"type":29,"value":3662},"Say you want your biggest won deal in each industry, maybe to pick a reference customer or a case study. Not a total; the actual deal. For that you need a ",{"type":24,"tag":350,"props":3664,"children":3665},{},[3666],{"type":29,"value":3667},"window function",{"type":29,"value":3669},", which calculates across a group of rows but keeps every row:",{"type":24,"tag":729,"props":3671,"children":3673},{"className":731,"code":3672,"language":15,"meta":7,"style":7},"WITH ranked AS (\n  SELECT COALESCE(a.industry, '(none)') AS industry,\n         a.name                        AS account,\n         o.name                        AS largest_deal,\n         o.amount,\n         o.closedate,\n         ROW_NUMBER() OVER (PARTITION BY a.industry\n                            ORDER BY o.amount DESC NULLS LAST) AS rn\n  FROM salesforce.opportunity o\n  JOIN salesforce.account a ON a.id = o.accountid\n  WHERE o.iswon = true\n)\nSELECT industry, account, largest_deal, amount, closedate\nFROM ranked\nWHERE rn = 1\nORDER BY amount DESC NULLS LAST\n",[3674],{"type":24,"tag":283,"props":3675,"children":3676},{"__ignoreMap":7},[3677,3697,3748,3773,3798,3818,3838,3884,3931,3955,4011,4041,4048,4060,4072,4092],{"type":24,"tag":738,"props":3678,"children":3679},{"class":740,"line":741},[3680,3684,3689,3693],{"type":24,"tag":738,"props":3681,"children":3682},{"style":745},[3683],{"type":29,"value":2584},{"type":24,"tag":738,"props":3685,"children":3686},{"style":755},[3687],{"type":29,"value":3688}," ranked ",{"type":24,"tag":738,"props":3690,"children":3691},{"style":745},[3692],{"type":29,"value":817},{"type":24,"tag":738,"props":3694,"children":3695},{"style":755},[3696],{"type":29,"value":2614},{"type":24,"tag":738,"props":3698,"children":3699},{"class":740,"line":761},[3700,3704,3709,3713,3717,3721,3726,3730,3735,3739,3743],{"type":24,"tag":738,"props":3701,"children":3702},{"style":745},[3703],{"type":29,"value":2622},{"type":24,"tag":738,"props":3705,"children":3706},{"style":769},[3707],{"type":29,"value":3708}," COALESCE",{"type":24,"tag":738,"props":3710,"children":3711},{"style":755},[3712],{"type":29,"value":1136},{"type":24,"tag":738,"props":3714,"children":3715},{"style":769},[3716],{"type":29,"value":52},{"type":24,"tag":738,"props":3718,"children":3719},{"style":755},[3720],{"type":29,"value":654},{"type":24,"tag":738,"props":3722,"children":3723},{"style":769},[3724],{"type":29,"value":3725},"industry",{"type":24,"tag":738,"props":3727,"children":3728},{"style":755},[3729],{"type":29,"value":290},{"type":24,"tag":738,"props":3731,"children":3732},{"style":1726},[3733],{"type":29,"value":3734},"'(none)'",{"type":24,"tag":738,"props":3736,"children":3737},{"style":755},[3738],{"type":29,"value":2769},{"type":24,"tag":738,"props":3740,"children":3741},{"style":745},[3742],{"type":29,"value":817},{"type":24,"tag":738,"props":3744,"children":3745},{"style":755},[3746],{"type":29,"value":3747}," industry,\n",{"type":24,"tag":738,"props":3749,"children":3750},{"class":740,"line":877},[3751,3756,3760,3764,3769],{"type":24,"tag":738,"props":3752,"children":3753},{"style":769},[3754],{"type":29,"value":3755},"         a",{"type":24,"tag":738,"props":3757,"children":3758},{"style":755},[3759],{"type":29,"value":654},{"type":24,"tag":738,"props":3761,"children":3762},{"style":769},[3763],{"type":29,"value":1991},{"type":24,"tag":738,"props":3765,"children":3766},{"style":745},[3767],{"type":29,"value":3768},"                        AS",{"type":24,"tag":738,"props":3770,"children":3771},{"style":755},[3772],{"type":29,"value":2026},{"type":24,"tag":738,"props":3774,"children":3775},{"class":740,"line":900},[3776,3781,3785,3789,3793],{"type":24,"tag":738,"props":3777,"children":3778},{"style":769},[3779],{"type":29,"value":3780},"         o",{"type":24,"tag":738,"props":3782,"children":3783},{"style":755},[3784],{"type":29,"value":654},{"type":24,"tag":738,"props":3786,"children":3787},{"style":769},[3788],{"type":29,"value":1991},{"type":24,"tag":738,"props":3790,"children":3791},{"style":745},[3792],{"type":29,"value":3768},{"type":24,"tag":738,"props":3794,"children":3795},{"style":755},[3796],{"type":29,"value":3797}," largest_deal,\n",{"type":24,"tag":738,"props":3799,"children":3800},{"class":740,"line":1070},[3801,3805,3809,3813],{"type":24,"tag":738,"props":3802,"children":3803},{"style":769},[3804],{"type":29,"value":3780},{"type":24,"tag":738,"props":3806,"children":3807},{"style":755},[3808],{"type":29,"value":654},{"type":24,"tag":738,"props":3810,"children":3811},{"style":769},[3812],{"type":29,"value":477},{"type":24,"tag":738,"props":3814,"children":3815},{"style":755},[3816],{"type":29,"value":3817},",\n",{"type":24,"tag":738,"props":3819,"children":3820},{"class":740,"line":1241},[3821,3825,3829,3834],{"type":24,"tag":738,"props":3822,"children":3823},{"style":769},[3824],{"type":29,"value":3780},{"type":24,"tag":738,"props":3826,"children":3827},{"style":755},[3828],{"type":29,"value":654},{"type":24,"tag":738,"props":3830,"children":3831},{"style":769},[3832],{"type":29,"value":3833},"closedate",{"type":24,"tag":738,"props":3835,"children":3836},{"style":755},[3837],{"type":29,"value":3817},{"type":24,"tag":738,"props":3839,"children":3840},{"class":740,"line":1851},[3841,3846,3851,3856,3861,3866,3871,3875,3879],{"type":24,"tag":738,"props":3842,"children":3843},{"style":769},[3844],{"type":29,"value":3845},"         ROW_NUMBER",{"type":24,"tag":738,"props":3847,"children":3848},{"style":755},[3849],{"type":29,"value":3850},"() ",{"type":24,"tag":738,"props":3852,"children":3853},{"style":745},[3854],{"type":29,"value":3855},"OVER",{"type":24,"tag":738,"props":3857,"children":3858},{"style":755},[3859],{"type":29,"value":3860}," (",{"type":24,"tag":738,"props":3862,"children":3863},{"style":745},[3864],{"type":29,"value":3865},"PARTITION",{"type":24,"tag":738,"props":3867,"children":3868},{"style":745},[3869],{"type":29,"value":3870}," BY",{"type":24,"tag":738,"props":3872,"children":3873},{"style":769},[3874],{"type":29,"value":2104},{"type":24,"tag":738,"props":3876,"children":3877},{"style":755},[3878],{"type":29,"value":654},{"type":24,"tag":738,"props":3880,"children":3881},{"style":769},[3882],{"type":29,"value":3883},"industry\n",{"type":24,"tag":738,"props":3885,"children":3886},{"class":740,"line":1871},[3887,3892,3896,3900,3904,3909,3913,3918,3922,3926],{"type":24,"tag":738,"props":3888,"children":3889},{"style":745},[3890],{"type":29,"value":3891},"                            ORDER BY",{"type":24,"tag":738,"props":3893,"children":3894},{"style":769},[3895],{"type":29,"value":1982},{"type":24,"tag":738,"props":3897,"children":3898},{"style":755},[3899],{"type":29,"value":654},{"type":24,"tag":738,"props":3901,"children":3902},{"style":769},[3903],{"type":29,"value":477},{"type":24,"tag":738,"props":3905,"children":3906},{"style":745},[3907],{"type":29,"value":3908}," DESC",{"type":24,"tag":738,"props":3910,"children":3911},{"style":745},[3912],{"type":29,"value":1062},{"type":24,"tag":738,"props":3914,"children":3915},{"style":745},[3916],{"type":29,"value":3917}," LAST",{"type":24,"tag":738,"props":3919,"children":3920},{"style":755},[3921],{"type":29,"value":2769},{"type":24,"tag":738,"props":3923,"children":3924},{"style":745},[3925],{"type":29,"value":817},{"type":24,"tag":738,"props":3927,"children":3928},{"style":755},[3929],{"type":29,"value":3930}," rn\n",{"type":24,"tag":738,"props":3932,"children":3933},{"class":740,"line":1891},[3934,3939,3943,3947,3951],{"type":24,"tag":738,"props":3935,"children":3936},{"style":745},[3937],{"type":29,"value":3938},"  FROM",{"type":24,"tag":738,"props":3940,"children":3941},{"style":769},[3942],{"type":29,"value":772},{"type":24,"tag":738,"props":3944,"children":3945},{"style":755},[3946],{"type":29,"value":654},{"type":24,"tag":738,"props":3948,"children":3949},{"style":769},[3950],{"type":29,"value":2063},{"type":24,"tag":738,"props":3952,"children":3953},{"style":755},[3954],{"type":29,"value":2068},{"type":24,"tag":738,"props":3956,"children":3957},{"class":740,"line":1911},[3958,3963,3967,3971,3975,3979,3983,3987,3991,3995,3999,4003,4007],{"type":24,"tag":738,"props":3959,"children":3960},{"style":745},[3961],{"type":29,"value":3962},"  JOIN",{"type":24,"tag":738,"props":3964,"children":3965},{"style":769},[3966],{"type":29,"value":772},{"type":24,"tag":738,"props":3968,"children":3969},{"style":755},[3970],{"type":29,"value":654},{"type":24,"tag":738,"props":3972,"children":3973},{"style":769},[3974],{"type":29,"value":2089},{"type":24,"tag":738,"props":3976,"children":3977},{"style":755},[3978],{"type":29,"value":2094},{"type":24,"tag":738,"props":3980,"children":3981},{"style":745},[3982],{"type":29,"value":2099},{"type":24,"tag":738,"props":3984,"children":3985},{"style":769},[3986],{"type":29,"value":2104},{"type":24,"tag":738,"props":3988,"children":3989},{"style":755},[3990],{"type":29,"value":654},{"type":24,"tag":738,"props":3992,"children":3993},{"style":769},[3994],{"type":29,"value":2113},{"type":24,"tag":738,"props":3996,"children":3997},{"style":745},[3998],{"type":29,"value":2118},{"type":24,"tag":738,"props":4000,"children":4001},{"style":769},[4002],{"type":29,"value":1982},{"type":24,"tag":738,"props":4004,"children":4005},{"style":755},[4006],{"type":29,"value":654},{"type":24,"tag":738,"props":4008,"children":4009},{"style":769},[4010],{"type":29,"value":2131},{"type":24,"tag":738,"props":4012,"children":4013},{"class":740,"line":1924},[4014,4019,4023,4027,4032,4036],{"type":24,"tag":738,"props":4015,"children":4016},{"style":745},[4017],{"type":29,"value":4018},"  WHERE",{"type":24,"tag":738,"props":4020,"children":4021},{"style":769},[4022],{"type":29,"value":1982},{"type":24,"tag":738,"props":4024,"children":4025},{"style":755},[4026],{"type":29,"value":654},{"type":24,"tag":738,"props":4028,"children":4029},{"style":769},[4030],{"type":29,"value":4031},"iswon",{"type":24,"tag":738,"props":4033,"children":4034},{"style":745},[4035],{"type":29,"value":2118},{"type":24,"tag":738,"props":4037,"children":4038},{"style":755},[4039],{"type":29,"value":4040}," true\n",{"type":24,"tag":738,"props":4042,"children":4043},{"class":740,"line":3384},[4044],{"type":24,"tag":738,"props":4045,"children":4046},{"style":755},[4047],{"type":29,"value":2740},{"type":24,"tag":738,"props":4049,"children":4050},{"class":740,"line":3442},[4051,4055],{"type":24,"tag":738,"props":4052,"children":4053},{"style":745},[4054],{"type":29,"value":288},{"type":24,"tag":738,"props":4056,"children":4057},{"style":755},[4058],{"type":29,"value":4059}," industry, account, largest_deal, amount, closedate\n",{"type":24,"tag":738,"props":4061,"children":4062},{"class":740,"line":3500},[4063,4067],{"type":24,"tag":738,"props":4064,"children":4065},{"style":745},[4066],{"type":29,"value":296},{"type":24,"tag":738,"props":4068,"children":4069},{"style":755},[4070],{"type":29,"value":4071}," ranked\n",{"type":24,"tag":738,"props":4073,"children":4074},{"class":740,"line":3529},[4075,4079,4084,4088],{"type":24,"tag":738,"props":4076,"children":4077},{"style":745},[4078],{"type":29,"value":303},{"type":24,"tag":738,"props":4080,"children":4081},{"style":755},[4082],{"type":29,"value":4083}," rn ",{"type":24,"tag":738,"props":4085,"children":4086},{"style":745},[4087],{"type":29,"value":892},{"type":24,"tag":738,"props":4089,"children":4090},{"style":769},[4091],{"type":29,"value":1921},{"type":24,"tag":738,"props":4093,"children":4094},{"class":740,"line":3578},[4095,4099,4103,4107,4111],{"type":24,"tag":738,"props":4096,"children":4097},{"style":745},[4098],{"type":29,"value":310},{"type":24,"tag":738,"props":4100,"children":4101},{"style":755},[4102],{"type":29,"value":911},{"type":24,"tag":738,"props":4104,"children":4105},{"style":745},[4106],{"type":29,"value":662},{"type":24,"tag":738,"props":4108,"children":4109},{"style":745},[4110],{"type":29,"value":1062},{"type":24,"tag":738,"props":4112,"children":4113},{"style":745},[4114],{"type":29,"value":1067},{"type":24,"tag":25,"props":4116,"children":4117},{},[4118,4124,4126,4132,4134,4139],{"type":24,"tag":283,"props":4119,"children":4121},{"className":4120},[],[4122],{"type":29,"value":4123},"ROW_NUMBER() OVER (PARTITION BY a.industry ORDER BY o.amount DESC NULLS LAST)",{"type":29,"value":4125}," numbers the won deals inside each industry, biggest first. The outer query keeps ",{"type":24,"tag":283,"props":4127,"children":4129},{"className":4128},[],[4130],{"type":29,"value":4131},"rn = 1",{"type":29,"value":4133},", the top deal per industry. (Some databases, like Snowflake, have a shortcut for this called ",{"type":24,"tag":283,"props":4135,"children":4137},{"className":4136},[],[4138],{"type":29,"value":618},{"type":29,"value":4140},", which filters on the row number without the CTE.)",{"type":24,"tag":25,"props":4142,"children":4143},{},[4144,4146,4152,4154,4160],{"type":29,"value":4145},"Swap the pieces around and you get other useful versions. ",{"type":24,"tag":283,"props":4147,"children":4149},{"className":4148},[],[4150],{"type":29,"value":4151},"PARTITION BY o.ownerid",{"type":29,"value":4153}," gives each rep's biggest win; ",{"type":24,"tag":283,"props":4155,"children":4157},{"className":4156},[],[4158],{"type":29,"value":4159},"ORDER BY o.closedate DESC",{"type":29,"value":4161}," gives the most recent win in each industry.",{"type":24,"tag":37,"props":4163,"children":4165},{"id":4164},"examples",[4166],{"type":29,"value":210},{"type":24,"tag":532,"props":4168,"children":4170},{"id":4169},"open-pipeline-by-stage",[4171],{"type":29,"value":222},{"type":24,"tag":729,"props":4173,"children":4175},{"className":731,"code":4174,"language":15,"meta":7,"style":7},"SELECT stagename                AS stage,\n       COUNT(*)                 AS deals,\n       ROUND(SUM(amount), 2)    AS pipeline_value,\n       ROUND(AVG(amount), 2)    AS average_deal\nFROM salesforce.opportunity\nWHERE isclosed = false\nGROUP BY 1\nORDER BY pipeline_value DESC NULLS LAST\n",[4176],{"type":24,"tag":283,"props":4177,"children":4178},{"__ignoreMap":7},[4179,4199,4227,4265,4301,4320,4339,4350],{"type":24,"tag":738,"props":4180,"children":4181},{"class":740,"line":741},[4182,4186,4191,4195],{"type":24,"tag":738,"props":4183,"children":4184},{"style":745},[4185],{"type":29,"value":288},{"type":24,"tag":738,"props":4187,"children":4188},{"style":755},[4189],{"type":29,"value":4190}," stagename                ",{"type":24,"tag":738,"props":4192,"children":4193},{"style":745},[4194],{"type":29,"value":817},{"type":24,"tag":738,"props":4196,"children":4197},{"style":755},[4198],{"type":29,"value":1528},{"type":24,"tag":738,"props":4200,"children":4201},{"class":740,"line":761},[4202,4206,4210,4214,4219,4223],{"type":24,"tag":738,"props":4203,"children":4204},{"style":769},[4205],{"type":29,"value":1385},{"type":24,"tag":738,"props":4207,"children":4208},{"style":755},[4209],{"type":29,"value":1136},{"type":24,"tag":738,"props":4211,"children":4212},{"style":745},[4213],{"type":29,"value":1141},{"type":24,"tag":738,"props":4215,"children":4216},{"style":755},[4217],{"type":29,"value":4218},")                 ",{"type":24,"tag":738,"props":4220,"children":4221},{"style":745},[4222],{"type":29,"value":817},{"type":24,"tag":738,"props":4224,"children":4225},{"style":755},[4226],{"type":29,"value":1556},{"type":24,"tag":738,"props":4228,"children":4229},{"class":740,"line":877},[4230,4235,4239,4243,4248,4252,4256,4260],{"type":24,"tag":738,"props":4231,"children":4232},{"style":769},[4233],{"type":29,"value":4234},"       ROUND",{"type":24,"tag":738,"props":4236,"children":4237},{"style":755},[4238],{"type":29,"value":1136},{"type":24,"tag":738,"props":4240,"children":4241},{"style":769},[4242],{"type":29,"value":339},{"type":24,"tag":738,"props":4244,"children":4245},{"style":755},[4246],{"type":29,"value":4247},"(amount), ",{"type":24,"tag":738,"props":4249,"children":4250},{"style":769},[4251],{"type":29,"value":3548},{"type":24,"tag":738,"props":4253,"children":4254},{"style":755},[4255],{"type":29,"value":1146},{"type":24,"tag":738,"props":4257,"children":4258},{"style":745},[4259],{"type":29,"value":817},{"type":24,"tag":738,"props":4261,"children":4262},{"style":755},[4263],{"type":29,"value":4264}," pipeline_value,\n",{"type":24,"tag":738,"props":4266,"children":4267},{"class":740,"line":900},[4268,4272,4276,4280,4284,4288,4292,4296],{"type":24,"tag":738,"props":4269,"children":4270},{"style":769},[4271],{"type":29,"value":4234},{"type":24,"tag":738,"props":4273,"children":4274},{"style":755},[4275],{"type":29,"value":1136},{"type":24,"tag":738,"props":4277,"children":4278},{"style":769},[4279],{"type":29,"value":1284},{"type":24,"tag":738,"props":4281,"children":4282},{"style":755},[4283],{"type":29,"value":4247},{"type":24,"tag":738,"props":4285,"children":4286},{"style":769},[4287],{"type":29,"value":3548},{"type":24,"tag":738,"props":4289,"children":4290},{"style":755},[4291],{"type":29,"value":1146},{"type":24,"tag":738,"props":4293,"children":4294},{"style":745},[4295],{"type":29,"value":817},{"type":24,"tag":738,"props":4297,"children":4298},{"style":755},[4299],{"type":29,"value":4300}," average_deal\n",{"type":24,"tag":738,"props":4302,"children":4303},{"class":740,"line":1070},[4304,4308,4312,4316],{"type":24,"tag":738,"props":4305,"children":4306},{"style":745},[4307],{"type":29,"value":296},{"type":24,"tag":738,"props":4309,"children":4310},{"style":769},[4311],{"type":29,"value":772},{"type":24,"tag":738,"props":4313,"children":4314},{"style":755},[4315],{"type":29,"value":654},{"type":24,"tag":738,"props":4317,"children":4318},{"style":769},[4319],{"type":29,"value":781},{"type":24,"tag":738,"props":4321,"children":4322},{"class":740,"line":1241},[4323,4327,4331,4335],{"type":24,"tag":738,"props":4324,"children":4325},{"style":745},[4326],{"type":29,"value":303},{"type":24,"tag":738,"props":4328,"children":4329},{"style":755},[4330],{"type":29,"value":887},{"type":24,"tag":738,"props":4332,"children":4333},{"style":745},[4334],{"type":29,"value":892},{"type":24,"tag":738,"props":4336,"children":4337},{"style":755},[4338],{"type":29,"value":897},{"type":24,"tag":738,"props":4340,"children":4341},{"class":740,"line":1851},[4342,4346],{"type":24,"tag":738,"props":4343,"children":4344},{"style":745},[4345],{"type":29,"value":324},{"type":24,"tag":738,"props":4347,"children":4348},{"style":769},[4349],{"type":29,"value":1921},{"type":24,"tag":738,"props":4351,"children":4352},{"class":740,"line":1871},[4353,4357,4362,4366,4370],{"type":24,"tag":738,"props":4354,"children":4355},{"style":745},[4356],{"type":29,"value":310},{"type":24,"tag":738,"props":4358,"children":4359},{"style":755},[4360],{"type":29,"value":4361}," pipeline_value ",{"type":24,"tag":738,"props":4363,"children":4364},{"style":745},[4365],{"type":29,"value":662},{"type":24,"tag":738,"props":4367,"children":4368},{"style":745},[4369],{"type":29,"value":1062},{"type":24,"tag":738,"props":4371,"children":4372},{"style":745},[4373],{"type":29,"value":1067},{"type":24,"tag":25,"props":4375,"children":4376},{},[4377,4379,4384],{"type":29,"value":4378},"Everything in flight right now and what it's worth. ",{"type":24,"tag":283,"props":4380,"children":4382},{"className":4381},[],[4383],{"type":29,"value":652},{"type":29,"value":4385}," pushes stages with no amounts to the bottom.",{"type":24,"tag":532,"props":4387,"children":4389},{"id":4388},"bookings-by-month",[4390],{"type":29,"value":231},{"type":24,"tag":729,"props":4392,"children":4394},{"className":731,"code":4393,"language":15,"meta":7,"style":7},"SELECT date_trunc('month', CAST(closedate AS DATE)) AS month,\n       COUNT(*)                                     AS deals_won,\n       ROUND(SUM(amount), 2)                        AS booked\nFROM salesforce.opportunity\nWHERE iswon = true\nGROUP BY 1\nORDER BY 1\n",[4395],{"type":24,"tag":283,"props":4396,"children":4397},{"__ignoreMap":7},[4398,4455,4484,4521,4540,4560,4571],{"type":24,"tag":738,"props":4399,"children":4400},{"class":740,"line":741},[4401,4405,4410,4415,4419,4424,4429,4433,4438,4442,4446,4451],{"type":24,"tag":738,"props":4402,"children":4403},{"style":745},[4404],{"type":29,"value":288},{"type":24,"tag":738,"props":4406,"children":4407},{"style":755},[4408],{"type":29,"value":4409}," date_trunc(",{"type":24,"tag":738,"props":4411,"children":4412},{"style":1726},[4413],{"type":29,"value":4414},"'month'",{"type":24,"tag":738,"props":4416,"children":4417},{"style":755},[4418],{"type":29,"value":290},{"type":24,"tag":738,"props":4420,"children":4421},{"style":769},[4422],{"type":29,"value":4423},"CAST",{"type":24,"tag":738,"props":4425,"children":4426},{"style":755},[4427],{"type":29,"value":4428},"(closedate ",{"type":24,"tag":738,"props":4430,"children":4431},{"style":745},[4432],{"type":29,"value":817},{"type":24,"tag":738,"props":4434,"children":4435},{"style":745},[4436],{"type":29,"value":4437}," DATE",{"type":24,"tag":738,"props":4439,"children":4440},{"style":755},[4441],{"type":29,"value":3228},{"type":24,"tag":738,"props":4443,"children":4444},{"style":745},[4445],{"type":29,"value":817},{"type":24,"tag":738,"props":4447,"children":4448},{"style":745},[4449],{"type":29,"value":4450}," month",{"type":24,"tag":738,"props":4452,"children":4453},{"style":755},[4454],{"type":29,"value":3817},{"type":24,"tag":738,"props":4456,"children":4457},{"class":740,"line":761},[4458,4462,4466,4470,4475,4479],{"type":24,"tag":738,"props":4459,"children":4460},{"style":769},[4461],{"type":29,"value":1385},{"type":24,"tag":738,"props":4463,"children":4464},{"style":755},[4465],{"type":29,"value":1136},{"type":24,"tag":738,"props":4467,"children":4468},{"style":745},[4469],{"type":29,"value":1141},{"type":24,"tag":738,"props":4471,"children":4472},{"style":755},[4473],{"type":29,"value":4474},")                                     ",{"type":24,"tag":738,"props":4476,"children":4477},{"style":745},[4478],{"type":29,"value":817},{"type":24,"tag":738,"props":4480,"children":4481},{"style":755},[4482],{"type":29,"value":4483}," deals_won,\n",{"type":24,"tag":738,"props":4485,"children":4486},{"class":740,"line":877},[4487,4491,4495,4499,4503,4507,4512,4516],{"type":24,"tag":738,"props":4488,"children":4489},{"style":769},[4490],{"type":29,"value":4234},{"type":24,"tag":738,"props":4492,"children":4493},{"style":755},[4494],{"type":29,"value":1136},{"type":24,"tag":738,"props":4496,"children":4497},{"style":769},[4498],{"type":29,"value":339},{"type":24,"tag":738,"props":4500,"children":4501},{"style":755},[4502],{"type":29,"value":4247},{"type":24,"tag":738,"props":4504,"children":4505},{"style":769},[4506],{"type":29,"value":3548},{"type":24,"tag":738,"props":4508,"children":4509},{"style":755},[4510],{"type":29,"value":4511},")                        ",{"type":24,"tag":738,"props":4513,"children":4514},{"style":745},[4515],{"type":29,"value":817},{"type":24,"tag":738,"props":4517,"children":4518},{"style":755},[4519],{"type":29,"value":4520}," booked\n",{"type":24,"tag":738,"props":4522,"children":4523},{"class":740,"line":900},[4524,4528,4532,4536],{"type":24,"tag":738,"props":4525,"children":4526},{"style":745},[4527],{"type":29,"value":296},{"type":24,"tag":738,"props":4529,"children":4530},{"style":769},[4531],{"type":29,"value":772},{"type":24,"tag":738,"props":4533,"children":4534},{"style":755},[4535],{"type":29,"value":654},{"type":24,"tag":738,"props":4537,"children":4538},{"style":769},[4539],{"type":29,"value":781},{"type":24,"tag":738,"props":4541,"children":4542},{"class":740,"line":1070},[4543,4547,4552,4556],{"type":24,"tag":738,"props":4544,"children":4545},{"style":745},[4546],{"type":29,"value":303},{"type":24,"tag":738,"props":4548,"children":4549},{"style":755},[4550],{"type":29,"value":4551}," iswon ",{"type":24,"tag":738,"props":4553,"children":4554},{"style":745},[4555],{"type":29,"value":892},{"type":24,"tag":738,"props":4557,"children":4558},{"style":755},[4559],{"type":29,"value":4040},{"type":24,"tag":738,"props":4561,"children":4562},{"class":740,"line":1241},[4563,4567],{"type":24,"tag":738,"props":4564,"children":4565},{"style":745},[4566],{"type":29,"value":324},{"type":24,"tag":738,"props":4568,"children":4569},{"style":769},[4570],{"type":29,"value":1921},{"type":24,"tag":738,"props":4572,"children":4573},{"class":740,"line":1851},[4574,4578],{"type":24,"tag":738,"props":4575,"children":4576},{"style":745},[4577],{"type":29,"value":310},{"type":24,"tag":738,"props":4579,"children":4580},{"style":769},[4581],{"type":29,"value":1921},{"type":24,"tag":25,"props":4583,"children":4584},{},[4585,4591],{"type":24,"tag":283,"props":4586,"children":4588},{"className":4587},[],[4589],{"type":29,"value":4590},"date_trunc('month', ...)",{"type":29,"value":4592}," rounds each close date back to the first of its month, which drops every deal into a month bucket. It looks good as a bar or line chart.",{"type":24,"tag":532,"props":4594,"children":4596},{"id":4595},"accounts-with-the-most-open-pipeline",[4597],{"type":29,"value":240},{"type":24,"tag":729,"props":4599,"children":4601},{"className":731,"code":4600,"language":15,"meta":7,"style":7},"SELECT a.id                    AS account_id,\n       a.name                  AS account,\n       a.industry,\n       COUNT(o.id)             AS open_deals,\n       ROUND(SUM(o.amount), 2) AS open_pipeline\nFROM salesforce.opportunity o\nJOIN salesforce.account a ON a.id = o.accountid\nWHERE o.isclosed = false\nGROUP BY 1, 2, 3\nORDER BY open_pipeline DESC NULLS LAST\nLIMIT 25\n",[4602],{"type":24,"tag":283,"props":4603,"children":4604},{"__ignoreMap":7},[4605,4634,4658,4677,4713,4766,4789,4844,4871,4899,4923],{"type":24,"tag":738,"props":4606,"children":4607},{"class":740,"line":741},[4608,4612,4616,4620,4624,4629],{"type":24,"tag":738,"props":4609,"children":4610},{"style":745},[4611],{"type":29,"value":288},{"type":24,"tag":738,"props":4613,"children":4614},{"style":769},[4615],{"type":29,"value":2104},{"type":24,"tag":738,"props":4617,"children":4618},{"style":755},[4619],{"type":29,"value":654},{"type":24,"tag":738,"props":4621,"children":4622},{"style":769},[4623],{"type":29,"value":2113},{"type":24,"tag":738,"props":4625,"children":4626},{"style":745},[4627],{"type":29,"value":4628},"                    AS",{"type":24,"tag":738,"props":4630,"children":4631},{"style":755},[4632],{"type":29,"value":4633}," account_id,\n",{"type":24,"tag":738,"props":4635,"children":4636},{"class":740,"line":761},[4637,4641,4645,4649,4654],{"type":24,"tag":738,"props":4638,"children":4639},{"style":769},[4640],{"type":29,"value":2009},{"type":24,"tag":738,"props":4642,"children":4643},{"style":755},[4644],{"type":29,"value":654},{"type":24,"tag":738,"props":4646,"children":4647},{"style":769},[4648],{"type":29,"value":1991},{"type":24,"tag":738,"props":4650,"children":4651},{"style":745},[4652],{"type":29,"value":4653},"                  AS",{"type":24,"tag":738,"props":4655,"children":4656},{"style":755},[4657],{"type":29,"value":2026},{"type":24,"tag":738,"props":4659,"children":4660},{"class":740,"line":877},[4661,4665,4669,4673],{"type":24,"tag":738,"props":4662,"children":4663},{"style":769},[4664],{"type":29,"value":2009},{"type":24,"tag":738,"props":4666,"children":4667},{"style":755},[4668],{"type":29,"value":654},{"type":24,"tag":738,"props":4670,"children":4671},{"style":769},[4672],{"type":29,"value":3725},{"type":24,"tag":738,"props":4674,"children":4675},{"style":755},[4676],{"type":29,"value":3817},{"type":24,"tag":738,"props":4678,"children":4679},{"class":740,"line":900},[4680,4684,4688,4692,4696,4700,4705,4709],{"type":24,"tag":738,"props":4681,"children":4682},{"style":769},[4683],{"type":29,"value":1385},{"type":24,"tag":738,"props":4685,"children":4686},{"style":755},[4687],{"type":29,"value":1136},{"type":24,"tag":738,"props":4689,"children":4690},{"style":769},[4691],{"type":29,"value":2166},{"type":24,"tag":738,"props":4693,"children":4694},{"style":755},[4695],{"type":29,"value":654},{"type":24,"tag":738,"props":4697,"children":4698},{"style":769},[4699],{"type":29,"value":2113},{"type":24,"tag":738,"props":4701,"children":4702},{"style":755},[4703],{"type":29,"value":4704},")             ",{"type":24,"tag":738,"props":4706,"children":4707},{"style":745},[4708],{"type":29,"value":817},{"type":24,"tag":738,"props":4710,"children":4711},{"style":755},[4712],{"type":29,"value":1155},{"type":24,"tag":738,"props":4714,"children":4715},{"class":740,"line":1070},[4716,4720,4724,4728,4732,4736,4740,4744,4749,4753,4757,4761],{"type":24,"tag":738,"props":4717,"children":4718},{"style":769},[4719],{"type":29,"value":4234},{"type":24,"tag":738,"props":4721,"children":4722},{"style":755},[4723],{"type":29,"value":1136},{"type":24,"tag":738,"props":4725,"children":4726},{"style":769},[4727],{"type":29,"value":339},{"type":24,"tag":738,"props":4729,"children":4730},{"style":755},[4731],{"type":29,"value":1136},{"type":24,"tag":738,"props":4733,"children":4734},{"style":769},[4735],{"type":29,"value":2166},{"type":24,"tag":738,"props":4737,"children":4738},{"style":755},[4739],{"type":29,"value":654},{"type":24,"tag":738,"props":4741,"children":4742},{"style":769},[4743],{"type":29,"value":477},{"type":24,"tag":738,"props":4745,"children":4746},{"style":755},[4747],{"type":29,"value":4748},"), ",{"type":24,"tag":738,"props":4750,"children":4751},{"style":769},[4752],{"type":29,"value":3548},{"type":24,"tag":738,"props":4754,"children":4755},{"style":755},[4756],{"type":29,"value":2769},{"type":24,"tag":738,"props":4758,"children":4759},{"style":745},[4760],{"type":29,"value":817},{"type":24,"tag":738,"props":4762,"children":4763},{"style":755},[4764],{"type":29,"value":4765}," open_pipeline\n",{"type":24,"tag":738,"props":4767,"children":4768},{"class":740,"line":1241},[4769,4773,4777,4781,4785],{"type":24,"tag":738,"props":4770,"children":4771},{"style":745},[4772],{"type":29,"value":296},{"type":24,"tag":738,"props":4774,"children":4775},{"style":769},[4776],{"type":29,"value":772},{"type":24,"tag":738,"props":4778,"children":4779},{"style":755},[4780],{"type":29,"value":654},{"type":24,"tag":738,"props":4782,"children":4783},{"style":769},[4784],{"type":29,"value":2063},{"type":24,"tag":738,"props":4786,"children":4787},{"style":755},[4788],{"type":29,"value":2068},{"type":24,"tag":738,"props":4790,"children":4791},{"class":740,"line":1851},[4792,4796,4800,4804,4808,4812,4816,4820,4824,4828,4832,4836,4840],{"type":24,"tag":738,"props":4793,"children":4794},{"style":745},[4795],{"type":29,"value":2076},{"type":24,"tag":738,"props":4797,"children":4798},{"style":769},[4799],{"type":29,"value":772},{"type":24,"tag":738,"props":4801,"children":4802},{"style":755},[4803],{"type":29,"value":654},{"type":24,"tag":738,"props":4805,"children":4806},{"style":769},[4807],{"type":29,"value":2089},{"type":24,"tag":738,"props":4809,"children":4810},{"style":755},[4811],{"type":29,"value":2094},{"type":24,"tag":738,"props":4813,"children":4814},{"style":745},[4815],{"type":29,"value":2099},{"type":24,"tag":738,"props":4817,"children":4818},{"style":769},[4819],{"type":29,"value":2104},{"type":24,"tag":738,"props":4821,"children":4822},{"style":755},[4823],{"type":29,"value":654},{"type":24,"tag":738,"props":4825,"children":4826},{"style":769},[4827],{"type":29,"value":2113},{"type":24,"tag":738,"props":4829,"children":4830},{"style":745},[4831],{"type":29,"value":2118},{"type":24,"tag":738,"props":4833,"children":4834},{"style":769},[4835],{"type":29,"value":1982},{"type":24,"tag":738,"props":4837,"children":4838},{"style":755},[4839],{"type":29,"value":654},{"type":24,"tag":738,"props":4841,"children":4842},{"style":769},[4843],{"type":29,"value":2131},{"type":24,"tag":738,"props":4845,"children":4846},{"class":740,"line":1871},[4847,4851,4855,4859,4863,4867],{"type":24,"tag":738,"props":4848,"children":4849},{"style":745},[4850],{"type":29,"value":303},{"type":24,"tag":738,"props":4852,"children":4853},{"style":769},[4854],{"type":29,"value":1982},{"type":24,"tag":738,"props":4856,"children":4857},{"style":755},[4858],{"type":29,"value":654},{"type":24,"tag":738,"props":4860,"children":4861},{"style":769},[4862],{"type":29,"value":3096},{"type":24,"tag":738,"props":4864,"children":4865},{"style":745},[4866],{"type":29,"value":2118},{"type":24,"tag":738,"props":4868,"children":4869},{"style":755},[4870],{"type":29,"value":897},{"type":24,"tag":738,"props":4872,"children":4873},{"class":740,"line":1891},[4874,4878,4882,4886,4890,4894],{"type":24,"tag":738,"props":4875,"children":4876},{"style":745},[4877],{"type":29,"value":324},{"type":24,"tag":738,"props":4879,"children":4880},{"style":769},[4881],{"type":29,"value":3539},{"type":24,"tag":738,"props":4883,"children":4884},{"style":755},[4885],{"type":29,"value":290},{"type":24,"tag":738,"props":4887,"children":4888},{"style":769},[4889],{"type":29,"value":3548},{"type":24,"tag":738,"props":4891,"children":4892},{"style":755},[4893],{"type":29,"value":290},{"type":24,"tag":738,"props":4895,"children":4896},{"style":769},[4897],{"type":29,"value":4898},"3\n",{"type":24,"tag":738,"props":4900,"children":4901},{"class":740,"line":1911},[4902,4906,4911,4915,4919],{"type":24,"tag":738,"props":4903,"children":4904},{"style":745},[4905],{"type":29,"value":310},{"type":24,"tag":738,"props":4907,"children":4908},{"style":755},[4909],{"type":29,"value":4910}," open_pipeline ",{"type":24,"tag":738,"props":4912,"children":4913},{"style":745},[4914],{"type":29,"value":662},{"type":24,"tag":738,"props":4916,"children":4917},{"style":745},[4918],{"type":29,"value":1062},{"type":24,"tag":738,"props":4920,"children":4921},{"style":745},[4922],{"type":29,"value":1067},{"type":24,"tag":738,"props":4924,"children":4925},{"class":740,"line":1924},[4926,4930],{"type":24,"tag":738,"props":4927,"children":4928},{"style":745},[4929],{"type":29,"value":317},{"type":24,"tag":738,"props":4931,"children":4932},{"style":769},[4933],{"type":29,"value":4934}," 25\n",{"type":24,"tag":25,"props":4936,"children":4937},{},[4938],{"type":29,"value":4939},"A join, a filter and a group: most of the basics in one query. Grouping by account id, and not just the name, keeps two accounts that happen to share a name apart.",{"type":24,"tag":532,"props":4941,"children":4943},{"id":4942},"win-rate-by-owner",[4944],{"type":29,"value":249},{"type":24,"tag":729,"props":4946,"children":4948},{"className":731,"code":4947,"language":15,"meta":7,"style":7},"SELECT u.id                                         AS owner_id,\n       u.name                                       AS owner,\n       COUNT(*)                                     AS closed_deals,\n       COUNT(CASE WHEN o.iswon = true THEN 1 END)       AS won,\n       ROUND(COUNT(CASE WHEN o.iswon = true THEN 1 END) * 100.0\n             \u002F NULLIF(COUNT(*), 0), 1)              AS win_rate_pct,\n       ROUND(SUM(CASE WHEN o.iswon = true THEN o.amount END), 2) AS booked\nFROM salesforce.opportunity o\nJOIN salesforce.user u ON u.id = o.ownerid\nWHERE o.isclosed = true\nGROUP BY 1, 2\nORDER BY booked DESC NULLS LAST\n",[4949],{"type":24,"tag":283,"props":4950,"children":4951},{"__ignoreMap":7},[4952,4982,5012,5040,5106,5187,5248,5335,5358,5414,5441,5461],{"type":24,"tag":738,"props":4953,"children":4954},{"class":740,"line":741},[4955,4959,4964,4968,4972,4977],{"type":24,"tag":738,"props":4956,"children":4957},{"style":745},[4958],{"type":29,"value":288},{"type":24,"tag":738,"props":4960,"children":4961},{"style":769},[4962],{"type":29,"value":4963}," u",{"type":24,"tag":738,"props":4965,"children":4966},{"style":755},[4967],{"type":29,"value":654},{"type":24,"tag":738,"props":4969,"children":4970},{"style":769},[4971],{"type":29,"value":2113},{"type":24,"tag":738,"props":4973,"children":4974},{"style":745},[4975],{"type":29,"value":4976},"                                         AS",{"type":24,"tag":738,"props":4978,"children":4979},{"style":755},[4980],{"type":29,"value":4981}," owner_id,\n",{"type":24,"tag":738,"props":4983,"children":4984},{"class":740,"line":761},[4985,4990,4994,4998,5003,5008],{"type":24,"tag":738,"props":4986,"children":4987},{"style":769},[4988],{"type":29,"value":4989},"       u",{"type":24,"tag":738,"props":4991,"children":4992},{"style":755},[4993],{"type":29,"value":654},{"type":24,"tag":738,"props":4995,"children":4996},{"style":769},[4997],{"type":29,"value":1991},{"type":24,"tag":738,"props":4999,"children":5000},{"style":745},[5001],{"type":29,"value":5002},"                                       AS",{"type":24,"tag":738,"props":5004,"children":5005},{"style":745},[5006],{"type":29,"value":5007}," owner",{"type":24,"tag":738,"props":5009,"children":5010},{"style":755},[5011],{"type":29,"value":3817},{"type":24,"tag":738,"props":5013,"children":5014},{"class":740,"line":877},[5015,5019,5023,5027,5031,5035],{"type":24,"tag":738,"props":5016,"children":5017},{"style":769},[5018],{"type":29,"value":1385},{"type":24,"tag":738,"props":5020,"children":5021},{"style":755},[5022],{"type":29,"value":1136},{"type":24,"tag":738,"props":5024,"children":5025},{"style":745},[5026],{"type":29,"value":1141},{"type":24,"tag":738,"props":5028,"children":5029},{"style":755},[5030],{"type":29,"value":4474},{"type":24,"tag":738,"props":5032,"children":5033},{"style":745},[5034],{"type":29,"value":817},{"type":24,"tag":738,"props":5036,"children":5037},{"style":755},[5038],{"type":29,"value":5039}," closed_deals,\n",{"type":24,"tag":738,"props":5041,"children":5042},{"class":740,"line":900},[5043,5047,5051,5055,5059,5063,5067,5071,5075,5080,5084,5088,5092,5097,5101],{"type":24,"tag":738,"props":5044,"children":5045},{"style":769},[5046],{"type":29,"value":1385},{"type":24,"tag":738,"props":5048,"children":5049},{"style":755},[5050],{"type":29,"value":1136},{"type":24,"tag":738,"props":5052,"children":5053},{"style":745},[5054],{"type":29,"value":1325},{"type":24,"tag":738,"props":5056,"children":5057},{"style":745},[5058],{"type":29,"value":1704},{"type":24,"tag":738,"props":5060,"children":5061},{"style":769},[5062],{"type":29,"value":1982},{"type":24,"tag":738,"props":5064,"children":5065},{"style":755},[5066],{"type":29,"value":654},{"type":24,"tag":738,"props":5068,"children":5069},{"style":769},[5070],{"type":29,"value":4031},{"type":24,"tag":738,"props":5072,"children":5073},{"style":745},[5074],{"type":29,"value":2118},{"type":24,"tag":738,"props":5076,"children":5077},{"style":755},[5078],{"type":29,"value":5079}," true ",{"type":24,"tag":738,"props":5081,"children":5082},{"style":745},[5083],{"type":29,"value":3110},{"type":24,"tag":738,"props":5085,"children":5086},{"style":769},[5087],{"type":29,"value":3539},{"type":24,"tag":738,"props":5089,"children":5090},{"style":745},[5091],{"type":29,"value":3127},{"type":24,"tag":738,"props":5093,"children":5094},{"style":755},[5095],{"type":29,"value":5096},")       ",{"type":24,"tag":738,"props":5098,"children":5099},{"style":745},[5100],{"type":29,"value":817},{"type":24,"tag":738,"props":5102,"children":5103},{"style":755},[5104],{"type":29,"value":5105}," won,\n",{"type":24,"tag":738,"props":5107,"children":5108},{"class":740,"line":1070},[5109,5113,5117,5121,5125,5129,5133,5137,5141,5145,5149,5153,5157,5161,5165,5169,5173,5178,5182],{"type":24,"tag":738,"props":5110,"children":5111},{"style":769},[5112],{"type":29,"value":4234},{"type":24,"tag":738,"props":5114,"children":5115},{"style":755},[5116],{"type":29,"value":1136},{"type":24,"tag":738,"props":5118,"children":5119},{"style":769},[5120],{"type":29,"value":331},{"type":24,"tag":738,"props":5122,"children":5123},{"style":755},[5124],{"type":29,"value":1136},{"type":24,"tag":738,"props":5126,"children":5127},{"style":745},[5128],{"type":29,"value":1325},{"type":24,"tag":738,"props":5130,"children":5131},{"style":745},[5132],{"type":29,"value":1704},{"type":24,"tag":738,"props":5134,"children":5135},{"style":769},[5136],{"type":29,"value":1982},{"type":24,"tag":738,"props":5138,"children":5139},{"style":755},[5140],{"type":29,"value":654},{"type":24,"tag":738,"props":5142,"children":5143},{"style":769},[5144],{"type":29,"value":4031},{"type":24,"tag":738,"props":5146,"children":5147},{"style":745},[5148],{"type":29,"value":2118},{"type":24,"tag":738,"props":5150,"children":5151},{"style":755},[5152],{"type":29,"value":5079},{"type":24,"tag":738,"props":5154,"children":5155},{"style":745},[5156],{"type":29,"value":3110},{"type":24,"tag":738,"props":5158,"children":5159},{"style":769},[5160],{"type":29,"value":3539},{"type":24,"tag":738,"props":5162,"children":5163},{"style":745},[5164],{"type":29,"value":3127},{"type":24,"tag":738,"props":5166,"children":5167},{"style":755},[5168],{"type":29,"value":2769},{"type":24,"tag":738,"props":5170,"children":5171},{"style":745},[5172],{"type":29,"value":1141},{"type":24,"tag":738,"props":5174,"children":5175},{"style":769},[5176],{"type":29,"value":5177}," 100",{"type":24,"tag":738,"props":5179,"children":5180},{"style":755},[5181],{"type":29,"value":654},{"type":24,"tag":738,"props":5183,"children":5184},{"style":769},[5185],{"type":29,"value":5186},"0\n",{"type":24,"tag":738,"props":5188,"children":5189},{"class":740,"line":1241},[5190,5195,5200,5204,5208,5212,5216,5220,5225,5229,5234,5239,5243],{"type":24,"tag":738,"props":5191,"children":5192},{"style":745},[5193],{"type":29,"value":5194},"             \u002F",{"type":24,"tag":738,"props":5196,"children":5197},{"style":769},[5198],{"type":29,"value":5199}," NULLIF",{"type":24,"tag":738,"props":5201,"children":5202},{"style":755},[5203],{"type":29,"value":1136},{"type":24,"tag":738,"props":5205,"children":5206},{"style":769},[5207],{"type":29,"value":331},{"type":24,"tag":738,"props":5209,"children":5210},{"style":755},[5211],{"type":29,"value":1136},{"type":24,"tag":738,"props":5213,"children":5214},{"style":745},[5215],{"type":29,"value":1141},{"type":24,"tag":738,"props":5217,"children":5218},{"style":755},[5219],{"type":29,"value":4748},{"type":24,"tag":738,"props":5221,"children":5222},{"style":769},[5223],{"type":29,"value":5224},"0",{"type":24,"tag":738,"props":5226,"children":5227},{"style":755},[5228],{"type":29,"value":4748},{"type":24,"tag":738,"props":5230,"children":5231},{"style":769},[5232],{"type":29,"value":5233},"1",{"type":24,"tag":738,"props":5235,"children":5236},{"style":755},[5237],{"type":29,"value":5238},")              ",{"type":24,"tag":738,"props":5240,"children":5241},{"style":745},[5242],{"type":29,"value":817},{"type":24,"tag":738,"props":5244,"children":5245},{"style":755},[5246],{"type":29,"value":5247}," win_rate_pct,\n",{"type":24,"tag":738,"props":5249,"children":5250},{"class":740,"line":1851},[5251,5255,5259,5263,5267,5271,5275,5279,5283,5287,5291,5295,5299,5303,5307,5311,5315,5319,5323,5327,5331],{"type":24,"tag":738,"props":5252,"children":5253},{"style":769},[5254],{"type":29,"value":4234},{"type":24,"tag":738,"props":5256,"children":5257},{"style":755},[5258],{"type":29,"value":1136},{"type":24,"tag":738,"props":5260,"children":5261},{"style":769},[5262],{"type":29,"value":339},{"type":24,"tag":738,"props":5264,"children":5265},{"style":755},[5266],{"type":29,"value":1136},{"type":24,"tag":738,"props":5268,"children":5269},{"style":745},[5270],{"type":29,"value":1325},{"type":24,"tag":738,"props":5272,"children":5273},{"style":745},[5274],{"type":29,"value":1704},{"type":24,"tag":738,"props":5276,"children":5277},{"style":769},[5278],{"type":29,"value":1982},{"type":24,"tag":738,"props":5280,"children":5281},{"style":755},[5282],{"type":29,"value":654},{"type":24,"tag":738,"props":5284,"children":5285},{"style":769},[5286],{"type":29,"value":4031},{"type":24,"tag":738,"props":5288,"children":5289},{"style":745},[5290],{"type":29,"value":2118},{"type":24,"tag":738,"props":5292,"children":5293},{"style":755},[5294],{"type":29,"value":5079},{"type":24,"tag":738,"props":5296,"children":5297},{"style":745},[5298],{"type":29,"value":3110},{"type":24,"tag":738,"props":5300,"children":5301},{"style":769},[5302],{"type":29,"value":1982},{"type":24,"tag":738,"props":5304,"children":5305},{"style":755},[5306],{"type":29,"value":654},{"type":24,"tag":738,"props":5308,"children":5309},{"style":769},[5310],{"type":29,"value":477},{"type":24,"tag":738,"props":5312,"children":5313},{"style":745},[5314],{"type":29,"value":3127},{"type":24,"tag":738,"props":5316,"children":5317},{"style":755},[5318],{"type":29,"value":4748},{"type":24,"tag":738,"props":5320,"children":5321},{"style":769},[5322],{"type":29,"value":3548},{"type":24,"tag":738,"props":5324,"children":5325},{"style":755},[5326],{"type":29,"value":2769},{"type":24,"tag":738,"props":5328,"children":5329},{"style":745},[5330],{"type":29,"value":817},{"type":24,"tag":738,"props":5332,"children":5333},{"style":755},[5334],{"type":29,"value":4520},{"type":24,"tag":738,"props":5336,"children":5337},{"class":740,"line":1871},[5338,5342,5346,5350,5354],{"type":24,"tag":738,"props":5339,"children":5340},{"style":745},[5341],{"type":29,"value":296},{"type":24,"tag":738,"props":5343,"children":5344},{"style":769},[5345],{"type":29,"value":772},{"type":24,"tag":738,"props":5347,"children":5348},{"style":755},[5349],{"type":29,"value":654},{"type":24,"tag":738,"props":5351,"children":5352},{"style":769},[5353],{"type":29,"value":2063},{"type":24,"tag":738,"props":5355,"children":5356},{"style":755},[5357],{"type":29,"value":2068},{"type":24,"tag":738,"props":5359,"children":5360},{"class":740,"line":1891},[5361,5365,5369,5373,5377,5382,5386,5390,5394,5398,5402,5406,5410],{"type":24,"tag":738,"props":5362,"children":5363},{"style":745},[5364],{"type":29,"value":2076},{"type":24,"tag":738,"props":5366,"children":5367},{"style":769},[5368],{"type":29,"value":772},{"type":24,"tag":738,"props":5370,"children":5371},{"style":755},[5372],{"type":29,"value":654},{"type":24,"tag":738,"props":5374,"children":5375},{"style":769},[5376],{"type":29,"value":3342},{"type":24,"tag":738,"props":5378,"children":5379},{"style":755},[5380],{"type":29,"value":5381}," u ",{"type":24,"tag":738,"props":5383,"children":5384},{"style":745},[5385],{"type":29,"value":2099},{"type":24,"tag":738,"props":5387,"children":5388},{"style":769},[5389],{"type":29,"value":4963},{"type":24,"tag":738,"props":5391,"children":5392},{"style":755},[5393],{"type":29,"value":654},{"type":24,"tag":738,"props":5395,"children":5396},{"style":769},[5397],{"type":29,"value":2113},{"type":24,"tag":738,"props":5399,"children":5400},{"style":745},[5401],{"type":29,"value":2118},{"type":24,"tag":738,"props":5403,"children":5404},{"style":769},[5405],{"type":29,"value":1982},{"type":24,"tag":738,"props":5407,"children":5408},{"style":755},[5409],{"type":29,"value":654},{"type":24,"tag":738,"props":5411,"children":5412},{"style":769},[5413],{"type":29,"value":3381},{"type":24,"tag":738,"props":5415,"children":5416},{"class":740,"line":1911},[5417,5421,5425,5429,5433,5437],{"type":24,"tag":738,"props":5418,"children":5419},{"style":745},[5420],{"type":29,"value":303},{"type":24,"tag":738,"props":5422,"children":5423},{"style":769},[5424],{"type":29,"value":1982},{"type":24,"tag":738,"props":5426,"children":5427},{"style":755},[5428],{"type":29,"value":654},{"type":24,"tag":738,"props":5430,"children":5431},{"style":769},[5432],{"type":29,"value":3096},{"type":24,"tag":738,"props":5434,"children":5435},{"style":745},[5436],{"type":29,"value":2118},{"type":24,"tag":738,"props":5438,"children":5439},{"style":755},[5440],{"type":29,"value":4040},{"type":24,"tag":738,"props":5442,"children":5443},{"class":740,"line":1924},[5444,5448,5452,5456],{"type":24,"tag":738,"props":5445,"children":5446},{"style":745},[5447],{"type":29,"value":324},{"type":24,"tag":738,"props":5449,"children":5450},{"style":769},[5451],{"type":29,"value":3539},{"type":24,"tag":738,"props":5453,"children":5454},{"style":755},[5455],{"type":29,"value":290},{"type":24,"tag":738,"props":5457,"children":5458},{"style":769},[5459],{"type":29,"value":5460},"2\n",{"type":24,"tag":738,"props":5462,"children":5463},{"class":740,"line":3384},[5464,5468,5473,5477,5481],{"type":24,"tag":738,"props":5465,"children":5466},{"style":745},[5467],{"type":29,"value":310},{"type":24,"tag":738,"props":5469,"children":5470},{"style":755},[5471],{"type":29,"value":5472}," booked ",{"type":24,"tag":738,"props":5474,"children":5475},{"style":745},[5476],{"type":29,"value":662},{"type":24,"tag":738,"props":5478,"children":5479},{"style":745},[5480],{"type":29,"value":1062},{"type":24,"tag":738,"props":5482,"children":5483},{"style":745},[5484],{"type":29,"value":1067},{"type":24,"tag":25,"props":5486,"children":5487},{},[5488,5490,5496,5498,5504],{"type":29,"value":5489},"Only closed deals count here, so open pipeline can't make the rate look better than it is. ",{"type":24,"tag":283,"props":5491,"children":5493},{"className":5492},[],[5494],{"type":29,"value":5495},"NULLIF(COUNT(*), 0)",{"type":29,"value":5497}," avoids dividing by zero, and ",{"type":24,"tag":283,"props":5499,"children":5501},{"className":5500},[],[5502],{"type":29,"value":5503},"* 100.0",{"type":29,"value":5505}," turns the result into a percentage with decimals.",{"type":24,"tag":532,"props":5507,"children":5509},{"id":5508},"lead-engagement-and-conversion-by-source",[5510],{"type":29,"value":258},{"type":24,"tag":25,"props":5512,"children":5513},{},[5514,5516,5521],{"type":29,"value":5515},"For this one, a lead counts as ",{"type":24,"tag":350,"props":5517,"children":5518},{},[5519],{"type":29,"value":5520},"engaged",{"type":29,"value":5522}," if it has at least one task (a call, an email, a to-do) logged after it was created:",{"type":24,"tag":729,"props":5524,"children":5526},{"className":731,"code":5525,"language":15,"meta":7,"style":7},"WITH engaged AS (\n  SELECT DISTINCT l.id AS lead_id\n  FROM salesforce.lead l\n  JOIN salesforce.task t\n    ON t.whoid IN (l.id, l.convertedcontactid)\n   AND CAST(t.createddate AS TIMESTAMP) >= CAST(l.createddate AS TIMESTAMP)\n  WHERE t.isdeleted = false\n)\nSELECT COALESCE(l.leadsource, '(none)')                AS source,\n       COUNT(*)                                        AS leads,\n       COUNT(e.lead_id)                                AS engaged,\n       ROUND(COUNT(e.lead_id) * 100.0\n             \u002F NULLIF(COUNT(*), 0), 1)                 AS engaged_pct,\n       COUNT(CASE WHEN l.isconverted = true THEN 1 END)    AS converted,\n       ROUND(COUNT(CASE WHEN l.isconverted = true THEN 1 END) * 100.0\n             \u002F NULLIF(COUNT(*), 0), 1)                 AS conversion_pct\nFROM salesforce.lead l\nLEFT JOIN engaged e ON e.lead_id = l.id\nWHERE l.isdeleted = false\nGROUP BY 1\nORDER BY leads DESC\n",[5527],{"type":24,"tag":283,"props":5528,"children":5529},{"__ignoreMap":7},[5530,5550,5579,5602,5627,5691,5771,5798,5805,5855,5884,5923,5974,6030,6094,6173,6229,6253,6299,6327,6339],{"type":24,"tag":738,"props":5531,"children":5532},{"class":740,"line":741},[5533,5537,5542,5546],{"type":24,"tag":738,"props":5534,"children":5535},{"style":745},[5536],{"type":29,"value":2584},{"type":24,"tag":738,"props":5538,"children":5539},{"style":755},[5540],{"type":29,"value":5541}," engaged ",{"type":24,"tag":738,"props":5543,"children":5544},{"style":745},[5545],{"type":29,"value":817},{"type":24,"tag":738,"props":5547,"children":5548},{"style":755},[5549],{"type":29,"value":2614},{"type":24,"tag":738,"props":5551,"children":5552},{"class":740,"line":761},[5553,5558,5562,5566,5570,5574],{"type":24,"tag":738,"props":5554,"children":5555},{"style":745},[5556],{"type":29,"value":5557},"  SELECT DISTINCT",{"type":24,"tag":738,"props":5559,"children":5560},{"style":769},[5561],{"type":29,"value":2943},{"type":24,"tag":738,"props":5563,"children":5564},{"style":755},[5565],{"type":29,"value":654},{"type":24,"tag":738,"props":5567,"children":5568},{"style":769},[5569],{"type":29,"value":2113},{"type":24,"tag":738,"props":5571,"children":5572},{"style":745},[5573],{"type":29,"value":1996},{"type":24,"tag":738,"props":5575,"children":5576},{"style":755},[5577],{"type":29,"value":5578}," lead_id\n",{"type":24,"tag":738,"props":5580,"children":5581},{"class":740,"line":877},[5582,5586,5590,5594,5598],{"type":24,"tag":738,"props":5583,"children":5584},{"style":745},[5585],{"type":29,"value":3938},{"type":24,"tag":738,"props":5587,"children":5588},{"style":769},[5589],{"type":29,"value":772},{"type":24,"tag":738,"props":5591,"children":5592},{"style":755},[5593],{"type":29,"value":654},{"type":24,"tag":738,"props":5595,"children":5596},{"style":769},[5597],{"type":29,"value":2667},{"type":24,"tag":738,"props":5599,"children":5600},{"style":755},[5601],{"type":29,"value":3164},{"type":24,"tag":738,"props":5603,"children":5604},{"class":740,"line":900},[5605,5609,5613,5617,5622],{"type":24,"tag":738,"props":5606,"children":5607},{"style":745},[5608],{"type":29,"value":3962},{"type":24,"tag":738,"props":5610,"children":5611},{"style":769},[5612],{"type":29,"value":772},{"type":24,"tag":738,"props":5614,"children":5615},{"style":755},[5616],{"type":29,"value":654},{"type":24,"tag":738,"props":5618,"children":5619},{"style":769},[5620],{"type":29,"value":5621},"task",{"type":24,"tag":738,"props":5623,"children":5624},{"style":755},[5625],{"type":29,"value":5626}," t\n",{"type":24,"tag":738,"props":5628,"children":5629},{"class":740,"line":1070},[5630,5635,5640,5644,5649,5654,5658,5662,5666,5670,5674,5678,5682,5687],{"type":24,"tag":738,"props":5631,"children":5632},{"style":745},[5633],{"type":29,"value":5634},"    ON",{"type":24,"tag":738,"props":5636,"children":5637},{"style":769},[5638],{"type":29,"value":5639}," t",{"type":24,"tag":738,"props":5641,"children":5642},{"style":755},[5643],{"type":29,"value":654},{"type":24,"tag":738,"props":5645,"children":5646},{"style":769},[5647],{"type":29,"value":5648},"whoid",{"type":24,"tag":738,"props":5650,"children":5651},{"style":745},[5652],{"type":29,"value":5653}," IN",{"type":24,"tag":738,"props":5655,"children":5656},{"style":755},[5657],{"type":29,"value":3860},{"type":24,"tag":738,"props":5659,"children":5660},{"style":769},[5661],{"type":29,"value":3253},{"type":24,"tag":738,"props":5663,"children":5664},{"style":755},[5665],{"type":29,"value":654},{"type":24,"tag":738,"props":5667,"children":5668},{"style":769},[5669],{"type":29,"value":2113},{"type":24,"tag":738,"props":5671,"children":5672},{"style":755},[5673],{"type":29,"value":290},{"type":24,"tag":738,"props":5675,"children":5676},{"style":769},[5677],{"type":29,"value":3253},{"type":24,"tag":738,"props":5679,"children":5680},{"style":755},[5681],{"type":29,"value":654},{"type":24,"tag":738,"props":5683,"children":5684},{"style":769},[5685],{"type":29,"value":5686},"convertedcontactid",{"type":24,"tag":738,"props":5688,"children":5689},{"style":755},[5690],{"type":29,"value":2740},{"type":24,"tag":738,"props":5692,"children":5693},{"class":740,"line":1241},[5694,5699,5704,5708,5713,5717,5722,5726,5731,5735,5739,5743,5747,5751,5755,5759,5763,5767],{"type":24,"tag":738,"props":5695,"children":5696},{"style":745},[5697],{"type":29,"value":5698},"   AND",{"type":24,"tag":738,"props":5700,"children":5701},{"style":769},[5702],{"type":29,"value":5703}," CAST",{"type":24,"tag":738,"props":5705,"children":5706},{"style":755},[5707],{"type":29,"value":1136},{"type":24,"tag":738,"props":5709,"children":5710},{"style":769},[5711],{"type":29,"value":5712},"t",{"type":24,"tag":738,"props":5714,"children":5715},{"style":755},[5716],{"type":29,"value":654},{"type":24,"tag":738,"props":5718,"children":5719},{"style":769},[5720],{"type":29,"value":5721},"createddate",{"type":24,"tag":738,"props":5723,"children":5724},{"style":745},[5725],{"type":29,"value":1996},{"type":24,"tag":738,"props":5727,"children":5728},{"style":745},[5729],{"type":29,"value":5730}," TIMESTAMP",{"type":24,"tag":738,"props":5732,"children":5733},{"style":755},[5734],{"type":29,"value":2769},{"type":24,"tag":738,"props":5736,"children":5737},{"style":745},[5738],{"type":29,"value":1713},{"type":24,"tag":738,"props":5740,"children":5741},{"style":769},[5742],{"type":29,"value":5703},{"type":24,"tag":738,"props":5744,"children":5745},{"style":755},[5746],{"type":29,"value":1136},{"type":24,"tag":738,"props":5748,"children":5749},{"style":769},[5750],{"type":29,"value":3253},{"type":24,"tag":738,"props":5752,"children":5753},{"style":755},[5754],{"type":29,"value":654},{"type":24,"tag":738,"props":5756,"children":5757},{"style":769},[5758],{"type":29,"value":5721},{"type":24,"tag":738,"props":5760,"children":5761},{"style":745},[5762],{"type":29,"value":1996},{"type":24,"tag":738,"props":5764,"children":5765},{"style":745},[5766],{"type":29,"value":5730},{"type":24,"tag":738,"props":5768,"children":5769},{"style":755},[5770],{"type":29,"value":2740},{"type":24,"tag":738,"props":5772,"children":5773},{"class":740,"line":1851},[5774,5778,5782,5786,5790,5794],{"type":24,"tag":738,"props":5775,"children":5776},{"style":745},[5777],{"type":29,"value":4018},{"type":24,"tag":738,"props":5779,"children":5780},{"style":769},[5781],{"type":29,"value":5639},{"type":24,"tag":738,"props":5783,"children":5784},{"style":755},[5785],{"type":29,"value":654},{"type":24,"tag":738,"props":5787,"children":5788},{"style":769},[5789],{"type":29,"value":2511},{"type":24,"tag":738,"props":5791,"children":5792},{"style":745},[5793],{"type":29,"value":2118},{"type":24,"tag":738,"props":5795,"children":5796},{"style":755},[5797],{"type":29,"value":897},{"type":24,"tag":738,"props":5799,"children":5800},{"class":740,"line":1871},[5801],{"type":24,"tag":738,"props":5802,"children":5803},{"style":755},[5804],{"type":29,"value":2740},{"type":24,"tag":738,"props":5806,"children":5807},{"class":740,"line":1891},[5808,5812,5816,5820,5824,5828,5833,5837,5841,5846,5850],{"type":24,"tag":738,"props":5809,"children":5810},{"style":745},[5811],{"type":29,"value":288},{"type":24,"tag":738,"props":5813,"children":5814},{"style":769},[5815],{"type":29,"value":3708},{"type":24,"tag":738,"props":5817,"children":5818},{"style":755},[5819],{"type":29,"value":1136},{"type":24,"tag":738,"props":5821,"children":5822},{"style":769},[5823],{"type":29,"value":3253},{"type":24,"tag":738,"props":5825,"children":5826},{"style":755},[5827],{"type":29,"value":654},{"type":24,"tag":738,"props":5829,"children":5830},{"style":769},[5831],{"type":29,"value":5832},"leadsource",{"type":24,"tag":738,"props":5834,"children":5835},{"style":755},[5836],{"type":29,"value":290},{"type":24,"tag":738,"props":5838,"children":5839},{"style":1726},[5840],{"type":29,"value":3734},{"type":24,"tag":738,"props":5842,"children":5843},{"style":755},[5844],{"type":29,"value":5845},")                ",{"type":24,"tag":738,"props":5847,"children":5848},{"style":745},[5849],{"type":29,"value":817},{"type":24,"tag":738,"props":5851,"children":5852},{"style":755},[5853],{"type":29,"value":5854}," source,\n",{"type":24,"tag":738,"props":5856,"children":5857},{"class":740,"line":1911},[5858,5862,5866,5870,5875,5879],{"type":24,"tag":738,"props":5859,"children":5860},{"style":769},[5861],{"type":29,"value":1385},{"type":24,"tag":738,"props":5863,"children":5864},{"style":755},[5865],{"type":29,"value":1136},{"type":24,"tag":738,"props":5867,"children":5868},{"style":745},[5869],{"type":29,"value":1141},{"type":24,"tag":738,"props":5871,"children":5872},{"style":755},[5873],{"type":29,"value":5874},")                                        ",{"type":24,"tag":738,"props":5876,"children":5877},{"style":745},[5878],{"type":29,"value":817},{"type":24,"tag":738,"props":5880,"children":5881},{"style":755},[5882],{"type":29,"value":5883}," leads,\n",{"type":24,"tag":738,"props":5885,"children":5886},{"class":740,"line":1924},[5887,5891,5895,5900,5904,5909,5914,5918],{"type":24,"tag":738,"props":5888,"children":5889},{"style":769},[5890],{"type":29,"value":1385},{"type":24,"tag":738,"props":5892,"children":5893},{"style":755},[5894],{"type":29,"value":1136},{"type":24,"tag":738,"props":5896,"children":5897},{"style":769},[5898],{"type":29,"value":5899},"e",{"type":24,"tag":738,"props":5901,"children":5902},{"style":755},[5903],{"type":29,"value":654},{"type":24,"tag":738,"props":5905,"children":5906},{"style":769},[5907],{"type":29,"value":5908},"lead_id",{"type":24,"tag":738,"props":5910,"children":5911},{"style":755},[5912],{"type":29,"value":5913},")                                ",{"type":24,"tag":738,"props":5915,"children":5916},{"style":745},[5917],{"type":29,"value":817},{"type":24,"tag":738,"props":5919,"children":5920},{"style":755},[5921],{"type":29,"value":5922}," engaged,\n",{"type":24,"tag":738,"props":5924,"children":5925},{"class":740,"line":3384},[5926,5930,5934,5938,5942,5946,5950,5954,5958,5962,5966,5970],{"type":24,"tag":738,"props":5927,"children":5928},{"style":769},[5929],{"type":29,"value":4234},{"type":24,"tag":738,"props":5931,"children":5932},{"style":755},[5933],{"type":29,"value":1136},{"type":24,"tag":738,"props":5935,"children":5936},{"style":769},[5937],{"type":29,"value":331},{"type":24,"tag":738,"props":5939,"children":5940},{"style":755},[5941],{"type":29,"value":1136},{"type":24,"tag":738,"props":5943,"children":5944},{"style":769},[5945],{"type":29,"value":5899},{"type":24,"tag":738,"props":5947,"children":5948},{"style":755},[5949],{"type":29,"value":654},{"type":24,"tag":738,"props":5951,"children":5952},{"style":769},[5953],{"type":29,"value":5908},{"type":24,"tag":738,"props":5955,"children":5956},{"style":755},[5957],{"type":29,"value":2769},{"type":24,"tag":738,"props":5959,"children":5960},{"style":745},[5961],{"type":29,"value":1141},{"type":24,"tag":738,"props":5963,"children":5964},{"style":769},[5965],{"type":29,"value":5177},{"type":24,"tag":738,"props":5967,"children":5968},{"style":755},[5969],{"type":29,"value":654},{"type":24,"tag":738,"props":5971,"children":5972},{"style":769},[5973],{"type":29,"value":5186},{"type":24,"tag":738,"props":5975,"children":5976},{"class":740,"line":3442},[5977,5981,5985,5989,5993,5997,6001,6005,6009,6013,6017,6021,6025],{"type":24,"tag":738,"props":5978,"children":5979},{"style":745},[5980],{"type":29,"value":5194},{"type":24,"tag":738,"props":5982,"children":5983},{"style":769},[5984],{"type":29,"value":5199},{"type":24,"tag":738,"props":5986,"children":5987},{"style":755},[5988],{"type":29,"value":1136},{"type":24,"tag":738,"props":5990,"children":5991},{"style":769},[5992],{"type":29,"value":331},{"type":24,"tag":738,"props":5994,"children":5995},{"style":755},[5996],{"type":29,"value":1136},{"type":24,"tag":738,"props":5998,"children":5999},{"style":745},[6000],{"type":29,"value":1141},{"type":24,"tag":738,"props":6002,"children":6003},{"style":755},[6004],{"type":29,"value":4748},{"type":24,"tag":738,"props":6006,"children":6007},{"style":769},[6008],{"type":29,"value":5224},{"type":24,"tag":738,"props":6010,"children":6011},{"style":755},[6012],{"type":29,"value":4748},{"type":24,"tag":738,"props":6014,"children":6015},{"style":769},[6016],{"type":29,"value":5233},{"type":24,"tag":738,"props":6018,"children":6019},{"style":755},[6020],{"type":29,"value":4218},{"type":24,"tag":738,"props":6022,"children":6023},{"style":745},[6024],{"type":29,"value":817},{"type":24,"tag":738,"props":6026,"children":6027},{"style":755},[6028],{"type":29,"value":6029}," engaged_pct,\n",{"type":24,"tag":738,"props":6031,"children":6032},{"class":740,"line":3500},[6033,6037,6041,6045,6049,6053,6057,6061,6065,6069,6073,6077,6081,6085,6089],{"type":24,"tag":738,"props":6034,"children":6035},{"style":769},[6036],{"type":29,"value":1385},{"type":24,"tag":738,"props":6038,"children":6039},{"style":755},[6040],{"type":29,"value":1136},{"type":24,"tag":738,"props":6042,"children":6043},{"style":745},[6044],{"type":29,"value":1325},{"type":24,"tag":738,"props":6046,"children":6047},{"style":745},[6048],{"type":29,"value":1704},{"type":24,"tag":738,"props":6050,"children":6051},{"style":769},[6052],{"type":29,"value":2943},{"type":24,"tag":738,"props":6054,"children":6055},{"style":755},[6056],{"type":29,"value":654},{"type":24,"tag":738,"props":6058,"children":6059},{"style":769},[6060],{"type":29,"value":3518},{"type":24,"tag":738,"props":6062,"children":6063},{"style":745},[6064],{"type":29,"value":2118},{"type":24,"tag":738,"props":6066,"children":6067},{"style":755},[6068],{"type":29,"value":5079},{"type":24,"tag":738,"props":6070,"children":6071},{"style":745},[6072],{"type":29,"value":3110},{"type":24,"tag":738,"props":6074,"children":6075},{"style":769},[6076],{"type":29,"value":3539},{"type":24,"tag":738,"props":6078,"children":6079},{"style":745},[6080],{"type":29,"value":3127},{"type":24,"tag":738,"props":6082,"children":6083},{"style":755},[6084],{"type":29,"value":1146},{"type":24,"tag":738,"props":6086,"children":6087},{"style":745},[6088],{"type":29,"value":817},{"type":24,"tag":738,"props":6090,"children":6091},{"style":755},[6092],{"type":29,"value":6093}," converted,\n",{"type":24,"tag":738,"props":6095,"children":6096},{"class":740,"line":3529},[6097,6101,6105,6109,6113,6117,6121,6125,6129,6133,6137,6141,6145,6149,6153,6157,6161,6165,6169],{"type":24,"tag":738,"props":6098,"children":6099},{"style":769},[6100],{"type":29,"value":4234},{"type":24,"tag":738,"props":6102,"children":6103},{"style":755},[6104],{"type":29,"value":1136},{"type":24,"tag":738,"props":6106,"children":6107},{"style":769},[6108],{"type":29,"value":331},{"type":24,"tag":738,"props":6110,"children":6111},{"style":755},[6112],{"type":29,"value":1136},{"type":24,"tag":738,"props":6114,"children":6115},{"style":745},[6116],{"type":29,"value":1325},{"type":24,"tag":738,"props":6118,"children":6119},{"style":745},[6120],{"type":29,"value":1704},{"type":24,"tag":738,"props":6122,"children":6123},{"style":769},[6124],{"type":29,"value":2943},{"type":24,"tag":738,"props":6126,"children":6127},{"style":755},[6128],{"type":29,"value":654},{"type":24,"tag":738,"props":6130,"children":6131},{"style":769},[6132],{"type":29,"value":3518},{"type":24,"tag":738,"props":6134,"children":6135},{"style":745},[6136],{"type":29,"value":2118},{"type":24,"tag":738,"props":6138,"children":6139},{"style":755},[6140],{"type":29,"value":5079},{"type":24,"tag":738,"props":6142,"children":6143},{"style":745},[6144],{"type":29,"value":3110},{"type":24,"tag":738,"props":6146,"children":6147},{"style":769},[6148],{"type":29,"value":3539},{"type":24,"tag":738,"props":6150,"children":6151},{"style":745},[6152],{"type":29,"value":3127},{"type":24,"tag":738,"props":6154,"children":6155},{"style":755},[6156],{"type":29,"value":2769},{"type":24,"tag":738,"props":6158,"children":6159},{"style":745},[6160],{"type":29,"value":1141},{"type":24,"tag":738,"props":6162,"children":6163},{"style":769},[6164],{"type":29,"value":5177},{"type":24,"tag":738,"props":6166,"children":6167},{"style":755},[6168],{"type":29,"value":654},{"type":24,"tag":738,"props":6170,"children":6171},{"style":769},[6172],{"type":29,"value":5186},{"type":24,"tag":738,"props":6174,"children":6175},{"class":740,"line":3578},[6176,6180,6184,6188,6192,6196,6200,6204,6208,6212,6216,6220,6224],{"type":24,"tag":738,"props":6177,"children":6178},{"style":745},[6179],{"type":29,"value":5194},{"type":24,"tag":738,"props":6181,"children":6182},{"style":769},[6183],{"type":29,"value":5199},{"type":24,"tag":738,"props":6185,"children":6186},{"style":755},[6187],{"type":29,"value":1136},{"type":24,"tag":738,"props":6189,"children":6190},{"style":769},[6191],{"type":29,"value":331},{"type":24,"tag":738,"props":6193,"children":6194},{"style":755},[6195],{"type":29,"value":1136},{"type":24,"tag":738,"props":6197,"children":6198},{"style":745},[6199],{"type":29,"value":1141},{"type":24,"tag":738,"props":6201,"children":6202},{"style":755},[6203],{"type":29,"value":4748},{"type":24,"tag":738,"props":6205,"children":6206},{"style":769},[6207],{"type":29,"value":5224},{"type":24,"tag":738,"props":6209,"children":6210},{"style":755},[6211],{"type":29,"value":4748},{"type":24,"tag":738,"props":6213,"children":6214},{"style":769},[6215],{"type":29,"value":5233},{"type":24,"tag":738,"props":6217,"children":6218},{"style":755},[6219],{"type":29,"value":4218},{"type":24,"tag":738,"props":6221,"children":6222},{"style":745},[6223],{"type":29,"value":817},{"type":24,"tag":738,"props":6225,"children":6226},{"style":755},[6227],{"type":29,"value":6228}," conversion_pct\n",{"type":24,"tag":738,"props":6230,"children":6232},{"class":740,"line":6231},17,[6233,6237,6241,6245,6249],{"type":24,"tag":738,"props":6234,"children":6235},{"style":745},[6236],{"type":29,"value":296},{"type":24,"tag":738,"props":6238,"children":6239},{"style":769},[6240],{"type":29,"value":772},{"type":24,"tag":738,"props":6242,"children":6243},{"style":755},[6244],{"type":29,"value":654},{"type":24,"tag":738,"props":6246,"children":6247},{"style":769},[6248],{"type":29,"value":2667},{"type":24,"tag":738,"props":6250,"children":6251},{"style":755},[6252],{"type":29,"value":3164},{"type":24,"tag":738,"props":6254,"children":6256},{"class":740,"line":6255},18,[6257,6261,6266,6270,6275,6279,6283,6287,6291,6295],{"type":24,"tag":738,"props":6258,"children":6259},{"style":745},[6260],{"type":29,"value":2199},{"type":24,"tag":738,"props":6262,"children":6263},{"style":755},[6264],{"type":29,"value":6265}," engaged e ",{"type":24,"tag":738,"props":6267,"children":6268},{"style":745},[6269],{"type":29,"value":2099},{"type":24,"tag":738,"props":6271,"children":6272},{"style":769},[6273],{"type":29,"value":6274}," e",{"type":24,"tag":738,"props":6276,"children":6277},{"style":755},[6278],{"type":29,"value":654},{"type":24,"tag":738,"props":6280,"children":6281},{"style":769},[6282],{"type":29,"value":5908},{"type":24,"tag":738,"props":6284,"children":6285},{"style":745},[6286],{"type":29,"value":2118},{"type":24,"tag":738,"props":6288,"children":6289},{"style":769},[6290],{"type":29,"value":2943},{"type":24,"tag":738,"props":6292,"children":6293},{"style":755},[6294],{"type":29,"value":654},{"type":24,"tag":738,"props":6296,"children":6297},{"style":769},[6298],{"type":29,"value":3497},{"type":24,"tag":738,"props":6300,"children":6302},{"class":740,"line":6301},19,[6303,6307,6311,6315,6319,6323],{"type":24,"tag":738,"props":6304,"children":6305},{"style":745},[6306],{"type":29,"value":303},{"type":24,"tag":738,"props":6308,"children":6309},{"style":769},[6310],{"type":29,"value":2943},{"type":24,"tag":738,"props":6312,"children":6313},{"style":755},[6314],{"type":29,"value":654},{"type":24,"tag":738,"props":6316,"children":6317},{"style":769},[6318],{"type":29,"value":2511},{"type":24,"tag":738,"props":6320,"children":6321},{"style":745},[6322],{"type":29,"value":2118},{"type":24,"tag":738,"props":6324,"children":6325},{"style":755},[6326],{"type":29,"value":897},{"type":24,"tag":738,"props":6328,"children":6330},{"class":740,"line":6329},20,[6331,6335],{"type":24,"tag":738,"props":6332,"children":6333},{"style":745},[6334],{"type":29,"value":324},{"type":24,"tag":738,"props":6336,"children":6337},{"style":769},[6338],{"type":29,"value":1921},{"type":24,"tag":738,"props":6340,"children":6342},{"class":740,"line":6341},21,[6343,6347,6352],{"type":24,"tag":738,"props":6344,"children":6345},{"style":745},[6346],{"type":29,"value":310},{"type":24,"tag":738,"props":6348,"children":6349},{"style":755},[6350],{"type":29,"value":6351}," leads ",{"type":24,"tag":738,"props":6353,"children":6354},{"style":745},[6355],{"type":29,"value":2867},{"type":24,"tag":25,"props":6357,"children":6358},{},[6359,6361,6366,6368,6373,6375,6380,6382,6387],{"type":29,"value":6360},"The ",{"type":24,"tag":283,"props":6362,"children":6364},{"className":6363},[],[6365],{"type":29,"value":5520},{"type":29,"value":6367}," CTE finds every lead with a task dated after the lead was created. Tasks point to a lead or contact through ",{"type":24,"tag":283,"props":6369,"children":6371},{"className":6370},[],[6372],{"type":29,"value":5648},{"type":29,"value":6374},", and when a lead converts, Salesforce moves its tasks over to the new contact. So the join checks both the lead id and ",{"type":24,"tag":283,"props":6376,"children":6378},{"className":6377},[],[6379],{"type":29,"value":5686},{"type":29,"value":6381},", which means converted leads still count as engaged. ",{"type":24,"tag":283,"props":6383,"children":6385},{"className":6384},[],[6386],{"type":29,"value":1394},{"type":29,"value":6388}," keeps it to one row per lead, no matter how many tasks it has.",{"type":24,"tag":25,"props":6390,"children":6391},{},[6392,6394,6399,6401,6407,6409,6415,6417,6422],{"type":29,"value":6393},"The main query ",{"type":24,"tag":283,"props":6395,"children":6397},{"className":6396},[],[6398],{"type":29,"value":2199},{"type":29,"value":6400},"s that list onto all leads, so ",{"type":24,"tag":283,"props":6402,"children":6404},{"className":6403},[],[6405],{"type":29,"value":6406},"COUNT(e.lead_id)",{"type":29,"value":6408}," only counts the engaged ones (it skips blanks). ",{"type":24,"tag":283,"props":6410,"children":6412},{"className":6411},[],[6413],{"type":29,"value":6414},"COALESCE",{"type":29,"value":6416}," gives leads with no source a row of their own, ",{"type":24,"tag":283,"props":6418,"children":6420},{"className":6419},[],[6421],{"type":29,"value":3734},{"type":29,"value":654},{"type":24,"tag":25,"props":6424,"children":6425},{},[6426],{"type":29,"value":6427},"With engaged and converted side by side, you can see where leads get stuck. Low engagement means nobody's working that source. High engagement but low conversion means it's being worked; it's just not turning into deals.",{"type":24,"tag":25,"props":6429,"children":6430},{},[6431],{"type":29,"value":6432},"You'll need the Task object synced for this one.",{"type":24,"tag":532,"props":6434,"children":6436},{"id":6435},"lead-follow-up-velocity-and-conversion",[6437],{"type":29,"value":267},{"type":24,"tag":25,"props":6439,"children":6440},{},[6441],{"type":29,"value":6442},"How quickly does someone follow up on a new lead, and do the fast follow-ups convert better? Here follow-up velocity means the time between a lead being created and the first task or event logged against it:",{"type":24,"tag":729,"props":6444,"children":6446},{"className":731,"code":6445,"language":15,"meta":7,"style":7},"WITH activities AS (\n  SELECT whoid, createddate FROM salesforce.task  WHERE isdeleted = false\n  UNION ALL\n  SELECT whoid, createddate FROM salesforce.event WHERE isdeleted = false\n),\nfirst_touch AS (\n  SELECT l.id,\n         l.isconverted,\n         EXTRACT(EPOCH FROM\n           date_trunc('minute', MIN(CAST(a.createddate AS TIMESTAMP)))\n           - date_trunc('minute', CAST(l.createddate AS TIMESTAMP))\n         ) \u002F 60 AS minutes_to_first_touch\n  FROM salesforce.lead l\n  LEFT JOIN activities a\n    ON a.whoid IN (l.id, l.convertedcontactid)\n   AND CAST(a.createddate AS TIMESTAMP) >= CAST(l.createddate AS TIMESTAMP)\n  WHERE l.isdeleted = false\n  GROUP BY l.id, l.isconverted, l.createddate\n)\nSELECT CASE WHEN minutes_to_first_touch IS NULL  THEN '5. Never'\n            WHEN minutes_to_first_touch \u003C 60     THEN '1. Within an hour'\n            WHEN minutes_to_first_touch \u003C 1440   THEN '2. Within a day'\n            WHEN minutes_to_first_touch \u003C 10080  THEN '3. Within a week'\n            ELSE '4. Over a week'\n       END                                             AS follow_up,\n       COUNT(*)                                        AS leads,\n       COUNT(CASE WHEN isconverted = true THEN 1 END)      AS converted,\n       ROUND(COUNT(CASE WHEN isconverted = true THEN 1 END) * 100.0\n             \u002F NULLIF(COUNT(*), 0), 1)                 AS conversion_pct\nFROM first_touch\nGROUP BY 1\nORDER BY 1\n",[6447],{"type":24,"tag":283,"props":6448,"children":6449},{"__ignoreMap":7},[6450,6470,6514,6521,6565,6573,6589,6612,6632,6645,6703,6755,6782,6805,6818,6877,6952,6979,7032,7039,7078,7108,7139,7169,7182,7200,7228,7285,7357,7413,7426,7438],{"type":24,"tag":738,"props":6451,"children":6452},{"class":740,"line":741},[6453,6457,6462,6466],{"type":24,"tag":738,"props":6454,"children":6455},{"style":745},[6456],{"type":29,"value":2584},{"type":24,"tag":738,"props":6458,"children":6459},{"style":755},[6460],{"type":29,"value":6461}," activities ",{"type":24,"tag":738,"props":6463,"children":6464},{"style":745},[6465],{"type":29,"value":817},{"type":24,"tag":738,"props":6467,"children":6468},{"style":755},[6469],{"type":29,"value":2614},{"type":24,"tag":738,"props":6471,"children":6472},{"class":740,"line":761},[6473,6477,6482,6486,6490,6494,6498,6502,6506,6510],{"type":24,"tag":738,"props":6474,"children":6475},{"style":745},[6476],{"type":29,"value":2622},{"type":24,"tag":738,"props":6478,"children":6479},{"style":755},[6480],{"type":29,"value":6481}," whoid, createddate ",{"type":24,"tag":738,"props":6483,"children":6484},{"style":745},[6485],{"type":29,"value":296},{"type":24,"tag":738,"props":6487,"children":6488},{"style":769},[6489],{"type":29,"value":772},{"type":24,"tag":738,"props":6491,"children":6492},{"style":755},[6493],{"type":29,"value":654},{"type":24,"tag":738,"props":6495,"children":6496},{"style":769},[6497],{"type":29,"value":5621},{"type":24,"tag":738,"props":6499,"children":6500},{"style":745},[6501],{"type":29,"value":4018},{"type":24,"tag":738,"props":6503,"children":6504},{"style":755},[6505],{"type":29,"value":2331},{"type":24,"tag":738,"props":6507,"children":6508},{"style":745},[6509],{"type":29,"value":892},{"type":24,"tag":738,"props":6511,"children":6512},{"style":755},[6513],{"type":29,"value":897},{"type":24,"tag":738,"props":6515,"children":6516},{"class":740,"line":877},[6517],{"type":24,"tag":738,"props":6518,"children":6519},{"style":745},[6520],{"type":29,"value":2692},{"type":24,"tag":738,"props":6522,"children":6523},{"class":740,"line":900},[6524,6528,6532,6536,6540,6544,6549,6553,6557,6561],{"type":24,"tag":738,"props":6525,"children":6526},{"style":745},[6527],{"type":29,"value":2622},{"type":24,"tag":738,"props":6529,"children":6530},{"style":755},[6531],{"type":29,"value":6481},{"type":24,"tag":738,"props":6533,"children":6534},{"style":745},[6535],{"type":29,"value":296},{"type":24,"tag":738,"props":6537,"children":6538},{"style":769},[6539],{"type":29,"value":772},{"type":24,"tag":738,"props":6541,"children":6542},{"style":755},[6543],{"type":29,"value":654},{"type":24,"tag":738,"props":6545,"children":6546},{"style":769},[6547],{"type":29,"value":6548},"event",{"type":24,"tag":738,"props":6550,"children":6551},{"style":745},[6552],{"type":29,"value":2672},{"type":24,"tag":738,"props":6554,"children":6555},{"style":755},[6556],{"type":29,"value":2331},{"type":24,"tag":738,"props":6558,"children":6559},{"style":745},[6560],{"type":29,"value":892},{"type":24,"tag":738,"props":6562,"children":6563},{"style":755},[6564],{"type":29,"value":897},{"type":24,"tag":738,"props":6566,"children":6567},{"class":740,"line":1070},[6568],{"type":24,"tag":738,"props":6569,"children":6570},{"style":755},[6571],{"type":29,"value":6572},"),\n",{"type":24,"tag":738,"props":6574,"children":6575},{"class":740,"line":1241},[6576,6581,6585],{"type":24,"tag":738,"props":6577,"children":6578},{"style":755},[6579],{"type":29,"value":6580},"first_touch ",{"type":24,"tag":738,"props":6582,"children":6583},{"style":745},[6584],{"type":29,"value":817},{"type":24,"tag":738,"props":6586,"children":6587},{"style":755},[6588],{"type":29,"value":2614},{"type":24,"tag":738,"props":6590,"children":6591},{"class":740,"line":1851},[6592,6596,6600,6604,6608],{"type":24,"tag":738,"props":6593,"children":6594},{"style":745},[6595],{"type":29,"value":2622},{"type":24,"tag":738,"props":6597,"children":6598},{"style":769},[6599],{"type":29,"value":2943},{"type":24,"tag":738,"props":6601,"children":6602},{"style":755},[6603],{"type":29,"value":654},{"type":24,"tag":738,"props":6605,"children":6606},{"style":769},[6607],{"type":29,"value":2113},{"type":24,"tag":738,"props":6609,"children":6610},{"style":755},[6611],{"type":29,"value":3817},{"type":24,"tag":738,"props":6613,"children":6614},{"class":740,"line":1871},[6615,6620,6624,6628],{"type":24,"tag":738,"props":6616,"children":6617},{"style":769},[6618],{"type":29,"value":6619},"         l",{"type":24,"tag":738,"props":6621,"children":6622},{"style":755},[6623],{"type":29,"value":654},{"type":24,"tag":738,"props":6625,"children":6626},{"style":769},[6627],{"type":29,"value":3518},{"type":24,"tag":738,"props":6629,"children":6630},{"style":755},[6631],{"type":29,"value":3817},{"type":24,"tag":738,"props":6633,"children":6634},{"class":740,"line":1891},[6635,6640],{"type":24,"tag":738,"props":6636,"children":6637},{"style":755},[6638],{"type":29,"value":6639},"         EXTRACT(EPOCH ",{"type":24,"tag":738,"props":6641,"children":6642},{"style":745},[6643],{"type":29,"value":6644},"FROM\n",{"type":24,"tag":738,"props":6646,"children":6647},{"class":740,"line":1911},[6648,6653,6658,6662,6666,6670,6674,6678,6682,6686,6690,6694,6698],{"type":24,"tag":738,"props":6649,"children":6650},{"style":755},[6651],{"type":29,"value":6652},"           date_trunc(",{"type":24,"tag":738,"props":6654,"children":6655},{"style":1726},[6656],{"type":29,"value":6657},"'minute'",{"type":24,"tag":738,"props":6659,"children":6660},{"style":755},[6661],{"type":29,"value":290},{"type":24,"tag":738,"props":6663,"children":6664},{"style":769},[6665],{"type":29,"value":1292},{"type":24,"tag":738,"props":6667,"children":6668},{"style":755},[6669],{"type":29,"value":1136},{"type":24,"tag":738,"props":6671,"children":6672},{"style":769},[6673],{"type":29,"value":4423},{"type":24,"tag":738,"props":6675,"children":6676},{"style":755},[6677],{"type":29,"value":1136},{"type":24,"tag":738,"props":6679,"children":6680},{"style":769},[6681],{"type":29,"value":52},{"type":24,"tag":738,"props":6683,"children":6684},{"style":755},[6685],{"type":29,"value":654},{"type":24,"tag":738,"props":6687,"children":6688},{"style":769},[6689],{"type":29,"value":5721},{"type":24,"tag":738,"props":6691,"children":6692},{"style":745},[6693],{"type":29,"value":1996},{"type":24,"tag":738,"props":6695,"children":6696},{"style":745},[6697],{"type":29,"value":5730},{"type":24,"tag":738,"props":6699,"children":6700},{"style":755},[6701],{"type":29,"value":6702},")))\n",{"type":24,"tag":738,"props":6704,"children":6705},{"class":740,"line":1924},[6706,6711,6715,6719,6723,6727,6731,6735,6739,6743,6747,6751],{"type":24,"tag":738,"props":6707,"children":6708},{"style":745},[6709],{"type":29,"value":6710},"           -",{"type":24,"tag":738,"props":6712,"children":6713},{"style":755},[6714],{"type":29,"value":4409},{"type":24,"tag":738,"props":6716,"children":6717},{"style":1726},[6718],{"type":29,"value":6657},{"type":24,"tag":738,"props":6720,"children":6721},{"style":755},[6722],{"type":29,"value":290},{"type":24,"tag":738,"props":6724,"children":6725},{"style":769},[6726],{"type":29,"value":4423},{"type":24,"tag":738,"props":6728,"children":6729},{"style":755},[6730],{"type":29,"value":1136},{"type":24,"tag":738,"props":6732,"children":6733},{"style":769},[6734],{"type":29,"value":3253},{"type":24,"tag":738,"props":6736,"children":6737},{"style":755},[6738],{"type":29,"value":654},{"type":24,"tag":738,"props":6740,"children":6741},{"style":769},[6742],{"type":29,"value":5721},{"type":24,"tag":738,"props":6744,"children":6745},{"style":745},[6746],{"type":29,"value":1996},{"type":24,"tag":738,"props":6748,"children":6749},{"style":745},[6750],{"type":29,"value":5730},{"type":24,"tag":738,"props":6752,"children":6753},{"style":755},[6754],{"type":29,"value":3266},{"type":24,"tag":738,"props":6756,"children":6757},{"class":740,"line":3384},[6758,6763,6768,6773,6777],{"type":24,"tag":738,"props":6759,"children":6760},{"style":755},[6761],{"type":29,"value":6762},"         ) ",{"type":24,"tag":738,"props":6764,"children":6765},{"style":745},[6766],{"type":29,"value":6767},"\u002F",{"type":24,"tag":738,"props":6769,"children":6770},{"style":769},[6771],{"type":29,"value":6772}," 60",{"type":24,"tag":738,"props":6774,"children":6775},{"style":745},[6776],{"type":29,"value":1996},{"type":24,"tag":738,"props":6778,"children":6779},{"style":755},[6780],{"type":29,"value":6781}," minutes_to_first_touch\n",{"type":24,"tag":738,"props":6783,"children":6784},{"class":740,"line":3442},[6785,6789,6793,6797,6801],{"type":24,"tag":738,"props":6786,"children":6787},{"style":745},[6788],{"type":29,"value":3938},{"type":24,"tag":738,"props":6790,"children":6791},{"style":769},[6792],{"type":29,"value":772},{"type":24,"tag":738,"props":6794,"children":6795},{"style":755},[6796],{"type":29,"value":654},{"type":24,"tag":738,"props":6798,"children":6799},{"style":769},[6800],{"type":29,"value":2667},{"type":24,"tag":738,"props":6802,"children":6803},{"style":755},[6804],{"type":29,"value":3164},{"type":24,"tag":738,"props":6806,"children":6807},{"class":740,"line":3500},[6808,6813],{"type":24,"tag":738,"props":6809,"children":6810},{"style":745},[6811],{"type":29,"value":6812},"  LEFT JOIN",{"type":24,"tag":738,"props":6814,"children":6815},{"style":755},[6816],{"type":29,"value":6817}," activities a\n",{"type":24,"tag":738,"props":6819,"children":6820},{"class":740,"line":3529},[6821,6825,6829,6833,6837,6841,6845,6849,6853,6857,6861,6865,6869,6873],{"type":24,"tag":738,"props":6822,"children":6823},{"style":745},[6824],{"type":29,"value":5634},{"type":24,"tag":738,"props":6826,"children":6827},{"style":769},[6828],{"type":29,"value":2104},{"type":24,"tag":738,"props":6830,"children":6831},{"style":755},[6832],{"type":29,"value":654},{"type":24,"tag":738,"props":6834,"children":6835},{"style":769},[6836],{"type":29,"value":5648},{"type":24,"tag":738,"props":6838,"children":6839},{"style":745},[6840],{"type":29,"value":5653},{"type":24,"tag":738,"props":6842,"children":6843},{"style":755},[6844],{"type":29,"value":3860},{"type":24,"tag":738,"props":6846,"children":6847},{"style":769},[6848],{"type":29,"value":3253},{"type":24,"tag":738,"props":6850,"children":6851},{"style":755},[6852],{"type":29,"value":654},{"type":24,"tag":738,"props":6854,"children":6855},{"style":769},[6856],{"type":29,"value":2113},{"type":24,"tag":738,"props":6858,"children":6859},{"style":755},[6860],{"type":29,"value":290},{"type":24,"tag":738,"props":6862,"children":6863},{"style":769},[6864],{"type":29,"value":3253},{"type":24,"tag":738,"props":6866,"children":6867},{"style":755},[6868],{"type":29,"value":654},{"type":24,"tag":738,"props":6870,"children":6871},{"style":769},[6872],{"type":29,"value":5686},{"type":24,"tag":738,"props":6874,"children":6875},{"style":755},[6876],{"type":29,"value":2740},{"type":24,"tag":738,"props":6878,"children":6879},{"class":740,"line":3578},[6880,6884,6888,6892,6896,6900,6904,6908,6912,6916,6920,6924,6928,6932,6936,6940,6944,6948],{"type":24,"tag":738,"props":6881,"children":6882},{"style":745},[6883],{"type":29,"value":5698},{"type":24,"tag":738,"props":6885,"children":6886},{"style":769},[6887],{"type":29,"value":5703},{"type":24,"tag":738,"props":6889,"children":6890},{"style":755},[6891],{"type":29,"value":1136},{"type":24,"tag":738,"props":6893,"children":6894},{"style":769},[6895],{"type":29,"value":52},{"type":24,"tag":738,"props":6897,"children":6898},{"style":755},[6899],{"type":29,"value":654},{"type":24,"tag":738,"props":6901,"children":6902},{"style":769},[6903],{"type":29,"value":5721},{"type":24,"tag":738,"props":6905,"children":6906},{"style":745},[6907],{"type":29,"value":1996},{"type":24,"tag":738,"props":6909,"children":6910},{"style":745},[6911],{"type":29,"value":5730},{"type":24,"tag":738,"props":6913,"children":6914},{"style":755},[6915],{"type":29,"value":2769},{"type":24,"tag":738,"props":6917,"children":6918},{"style":745},[6919],{"type":29,"value":1713},{"type":24,"tag":738,"props":6921,"children":6922},{"style":769},[6923],{"type":29,"value":5703},{"type":24,"tag":738,"props":6925,"children":6926},{"style":755},[6927],{"type":29,"value":1136},{"type":24,"tag":738,"props":6929,"children":6930},{"style":769},[6931],{"type":29,"value":3253},{"type":24,"tag":738,"props":6933,"children":6934},{"style":755},[6935],{"type":29,"value":654},{"type":24,"tag":738,"props":6937,"children":6938},{"style":769},[6939],{"type":29,"value":5721},{"type":24,"tag":738,"props":6941,"children":6942},{"style":745},[6943],{"type":29,"value":1996},{"type":24,"tag":738,"props":6945,"children":6946},{"style":745},[6947],{"type":29,"value":5730},{"type":24,"tag":738,"props":6949,"children":6950},{"style":755},[6951],{"type":29,"value":2740},{"type":24,"tag":738,"props":6953,"children":6954},{"class":740,"line":6231},[6955,6959,6963,6967,6971,6975],{"type":24,"tag":738,"props":6956,"children":6957},{"style":745},[6958],{"type":29,"value":4018},{"type":24,"tag":738,"props":6960,"children":6961},{"style":769},[6962],{"type":29,"value":2943},{"type":24,"tag":738,"props":6964,"children":6965},{"style":755},[6966],{"type":29,"value":654},{"type":24,"tag":738,"props":6968,"children":6969},{"style":769},[6970],{"type":29,"value":2511},{"type":24,"tag":738,"props":6972,"children":6973},{"style":745},[6974],{"type":29,"value":2118},{"type":24,"tag":738,"props":6976,"children":6977},{"style":755},[6978],{"type":29,"value":897},{"type":24,"tag":738,"props":6980,"children":6981},{"class":740,"line":6255},[6982,6987,6991,6995,6999,7003,7007,7011,7015,7019,7023,7027],{"type":24,"tag":738,"props":6983,"children":6984},{"style":745},[6985],{"type":29,"value":6986},"  GROUP BY",{"type":24,"tag":738,"props":6988,"children":6989},{"style":769},[6990],{"type":29,"value":2943},{"type":24,"tag":738,"props":6992,"children":6993},{"style":755},[6994],{"type":29,"value":654},{"type":24,"tag":738,"props":6996,"children":6997},{"style":769},[6998],{"type":29,"value":2113},{"type":24,"tag":738,"props":7000,"children":7001},{"style":755},[7002],{"type":29,"value":290},{"type":24,"tag":738,"props":7004,"children":7005},{"style":769},[7006],{"type":29,"value":3253},{"type":24,"tag":738,"props":7008,"children":7009},{"style":755},[7010],{"type":29,"value":654},{"type":24,"tag":738,"props":7012,"children":7013},{"style":769},[7014],{"type":29,"value":3518},{"type":24,"tag":738,"props":7016,"children":7017},{"style":755},[7018],{"type":29,"value":290},{"type":24,"tag":738,"props":7020,"children":7021},{"style":769},[7022],{"type":29,"value":3253},{"type":24,"tag":738,"props":7024,"children":7025},{"style":755},[7026],{"type":29,"value":654},{"type":24,"tag":738,"props":7028,"children":7029},{"style":769},[7030],{"type":29,"value":7031},"createddate\n",{"type":24,"tag":738,"props":7033,"children":7034},{"class":740,"line":6301},[7035],{"type":24,"tag":738,"props":7036,"children":7037},{"style":755},[7038],{"type":29,"value":2740},{"type":24,"tag":738,"props":7040,"children":7041},{"class":740,"line":6329},[7042,7046,7050,7054,7059,7064,7069,7073],{"type":24,"tag":738,"props":7043,"children":7044},{"style":745},[7045],{"type":29,"value":288},{"type":24,"tag":738,"props":7047,"children":7048},{"style":745},[7049],{"type":29,"value":1699},{"type":24,"tag":738,"props":7051,"children":7052},{"style":745},[7053],{"type":29,"value":1704},{"type":24,"tag":738,"props":7055,"children":7056},{"style":755},[7057],{"type":29,"value":7058}," minutes_to_first_touch ",{"type":24,"tag":738,"props":7060,"children":7061},{"style":745},[7062],{"type":29,"value":7063},"IS",{"type":24,"tag":738,"props":7065,"children":7066},{"style":745},[7067],{"type":29,"value":7068}," NULL",{"type":24,"tag":738,"props":7070,"children":7071},{"style":745},[7072],{"type":29,"value":1755},{"type":24,"tag":738,"props":7074,"children":7075},{"style":1726},[7076],{"type":29,"value":7077}," '5. Never'\n",{"type":24,"tag":738,"props":7079,"children":7080},{"class":740,"line":6341},[7081,7085,7089,7094,7098,7103],{"type":24,"tag":738,"props":7082,"children":7083},{"style":745},[7084],{"type":29,"value":1737},{"type":24,"tag":738,"props":7086,"children":7087},{"style":755},[7088],{"type":29,"value":7058},{"type":24,"tag":738,"props":7090,"children":7091},{"style":745},[7092],{"type":29,"value":7093},"\u003C",{"type":24,"tag":738,"props":7095,"children":7096},{"style":769},[7097],{"type":29,"value":6772},{"type":24,"tag":738,"props":7099,"children":7100},{"style":745},[7101],{"type":29,"value":7102},"     THEN",{"type":24,"tag":738,"props":7104,"children":7105},{"style":1726},[7106],{"type":29,"value":7107}," '1. Within an hour'\n",{"type":24,"tag":738,"props":7109,"children":7111},{"class":740,"line":7110},22,[7112,7116,7120,7124,7129,7134],{"type":24,"tag":738,"props":7113,"children":7114},{"style":745},[7115],{"type":29,"value":1737},{"type":24,"tag":738,"props":7117,"children":7118},{"style":755},[7119],{"type":29,"value":7058},{"type":24,"tag":738,"props":7121,"children":7122},{"style":745},[7123],{"type":29,"value":7093},{"type":24,"tag":738,"props":7125,"children":7126},{"style":769},[7127],{"type":29,"value":7128}," 1440",{"type":24,"tag":738,"props":7130,"children":7131},{"style":745},[7132],{"type":29,"value":7133},"   THEN",{"type":24,"tag":738,"props":7135,"children":7136},{"style":1726},[7137],{"type":29,"value":7138}," '2. Within a day'\n",{"type":24,"tag":738,"props":7140,"children":7142},{"class":740,"line":7141},23,[7143,7147,7151,7155,7160,7164],{"type":24,"tag":738,"props":7144,"children":7145},{"style":745},[7146],{"type":29,"value":1737},{"type":24,"tag":738,"props":7148,"children":7149},{"style":755},[7150],{"type":29,"value":7058},{"type":24,"tag":738,"props":7152,"children":7153},{"style":745},[7154],{"type":29,"value":7093},{"type":24,"tag":738,"props":7156,"children":7157},{"style":769},[7158],{"type":29,"value":7159}," 10080",{"type":24,"tag":738,"props":7161,"children":7162},{"style":745},[7163],{"type":29,"value":1755},{"type":24,"tag":738,"props":7165,"children":7166},{"style":1726},[7167],{"type":29,"value":7168}," '3. Within a week'\n",{"type":24,"tag":738,"props":7170,"children":7172},{"class":740,"line":7171},24,[7173,7177],{"type":24,"tag":738,"props":7174,"children":7175},{"style":745},[7176],{"type":29,"value":1798},{"type":24,"tag":738,"props":7178,"children":7179},{"style":1726},[7180],{"type":29,"value":7181}," '4. Over a week'\n",{"type":24,"tag":738,"props":7183,"children":7185},{"class":740,"line":7184},25,[7186,7190,7195],{"type":24,"tag":738,"props":7187,"children":7188},{"style":745},[7189],{"type":29,"value":1811},{"type":24,"tag":738,"props":7191,"children":7192},{"style":745},[7193],{"type":29,"value":7194},"                                             AS",{"type":24,"tag":738,"props":7196,"children":7197},{"style":755},[7198],{"type":29,"value":7199}," follow_up,\n",{"type":24,"tag":738,"props":7201,"children":7203},{"class":740,"line":7202},26,[7204,7208,7212,7216,7220,7224],{"type":24,"tag":738,"props":7205,"children":7206},{"style":769},[7207],{"type":29,"value":1385},{"type":24,"tag":738,"props":7209,"children":7210},{"style":755},[7211],{"type":29,"value":1136},{"type":24,"tag":738,"props":7213,"children":7214},{"style":745},[7215],{"type":29,"value":1141},{"type":24,"tag":738,"props":7217,"children":7218},{"style":755},[7219],{"type":29,"value":5874},{"type":24,"tag":738,"props":7221,"children":7222},{"style":745},[7223],{"type":29,"value":817},{"type":24,"tag":738,"props":7225,"children":7226},{"style":755},[7227],{"type":29,"value":5883},{"type":24,"tag":738,"props":7229,"children":7231},{"class":740,"line":7230},27,[7232,7236,7240,7244,7248,7252,7256,7260,7264,7268,7272,7277,7281],{"type":24,"tag":738,"props":7233,"children":7234},{"style":769},[7235],{"type":29,"value":1385},{"type":24,"tag":738,"props":7237,"children":7238},{"style":755},[7239],{"type":29,"value":1136},{"type":24,"tag":738,"props":7241,"children":7242},{"style":745},[7243],{"type":29,"value":1325},{"type":24,"tag":738,"props":7245,"children":7246},{"style":745},[7247],{"type":29,"value":1704},{"type":24,"tag":738,"props":7249,"children":7250},{"style":755},[7251],{"type":29,"value":2311},{"type":24,"tag":738,"props":7253,"children":7254},{"style":745},[7255],{"type":29,"value":892},{"type":24,"tag":738,"props":7257,"children":7258},{"style":755},[7259],{"type":29,"value":5079},{"type":24,"tag":738,"props":7261,"children":7262},{"style":745},[7263],{"type":29,"value":3110},{"type":24,"tag":738,"props":7265,"children":7266},{"style":769},[7267],{"type":29,"value":3539},{"type":24,"tag":738,"props":7269,"children":7270},{"style":745},[7271],{"type":29,"value":3127},{"type":24,"tag":738,"props":7273,"children":7274},{"style":755},[7275],{"type":29,"value":7276},")      ",{"type":24,"tag":738,"props":7278,"children":7279},{"style":745},[7280],{"type":29,"value":817},{"type":24,"tag":738,"props":7282,"children":7283},{"style":755},[7284],{"type":29,"value":6093},{"type":24,"tag":738,"props":7286,"children":7288},{"class":740,"line":7287},28,[7289,7293,7297,7301,7305,7309,7313,7317,7321,7325,7329,7333,7337,7341,7345,7349,7353],{"type":24,"tag":738,"props":7290,"children":7291},{"style":769},[7292],{"type":29,"value":4234},{"type":24,"tag":738,"props":7294,"children":7295},{"style":755},[7296],{"type":29,"value":1136},{"type":24,"tag":738,"props":7298,"children":7299},{"style":769},[7300],{"type":29,"value":331},{"type":24,"tag":738,"props":7302,"children":7303},{"style":755},[7304],{"type":29,"value":1136},{"type":24,"tag":738,"props":7306,"children":7307},{"style":745},[7308],{"type":29,"value":1325},{"type":24,"tag":738,"props":7310,"children":7311},{"style":745},[7312],{"type":29,"value":1704},{"type":24,"tag":738,"props":7314,"children":7315},{"style":755},[7316],{"type":29,"value":2311},{"type":24,"tag":738,"props":7318,"children":7319},{"style":745},[7320],{"type":29,"value":892},{"type":24,"tag":738,"props":7322,"children":7323},{"style":755},[7324],{"type":29,"value":5079},{"type":24,"tag":738,"props":7326,"children":7327},{"style":745},[7328],{"type":29,"value":3110},{"type":24,"tag":738,"props":7330,"children":7331},{"style":769},[7332],{"type":29,"value":3539},{"type":24,"tag":738,"props":7334,"children":7335},{"style":745},[7336],{"type":29,"value":3127},{"type":24,"tag":738,"props":7338,"children":7339},{"style":755},[7340],{"type":29,"value":2769},{"type":24,"tag":738,"props":7342,"children":7343},{"style":745},[7344],{"type":29,"value":1141},{"type":24,"tag":738,"props":7346,"children":7347},{"style":769},[7348],{"type":29,"value":5177},{"type":24,"tag":738,"props":7350,"children":7351},{"style":755},[7352],{"type":29,"value":654},{"type":24,"tag":738,"props":7354,"children":7355},{"style":769},[7356],{"type":29,"value":5186},{"type":24,"tag":738,"props":7358,"children":7360},{"class":740,"line":7359},29,[7361,7365,7369,7373,7377,7381,7385,7389,7393,7397,7401,7405,7409],{"type":24,"tag":738,"props":7362,"children":7363},{"style":745},[7364],{"type":29,"value":5194},{"type":24,"tag":738,"props":7366,"children":7367},{"style":769},[7368],{"type":29,"value":5199},{"type":24,"tag":738,"props":7370,"children":7371},{"style":755},[7372],{"type":29,"value":1136},{"type":24,"tag":738,"props":7374,"children":7375},{"style":769},[7376],{"type":29,"value":331},{"type":24,"tag":738,"props":7378,"children":7379},{"style":755},[7380],{"type":29,"value":1136},{"type":24,"tag":738,"props":7382,"children":7383},{"style":745},[7384],{"type":29,"value":1141},{"type":24,"tag":738,"props":7386,"children":7387},{"style":755},[7388],{"type":29,"value":4748},{"type":24,"tag":738,"props":7390,"children":7391},{"style":769},[7392],{"type":29,"value":5224},{"type":24,"tag":738,"props":7394,"children":7395},{"style":755},[7396],{"type":29,"value":4748},{"type":24,"tag":738,"props":7398,"children":7399},{"style":769},[7400],{"type":29,"value":5233},{"type":24,"tag":738,"props":7402,"children":7403},{"style":755},[7404],{"type":29,"value":4218},{"type":24,"tag":738,"props":7406,"children":7407},{"style":745},[7408],{"type":29,"value":817},{"type":24,"tag":738,"props":7410,"children":7411},{"style":755},[7412],{"type":29,"value":6228},{"type":24,"tag":738,"props":7414,"children":7416},{"class":740,"line":7415},30,[7417,7421],{"type":24,"tag":738,"props":7418,"children":7419},{"style":745},[7420],{"type":29,"value":296},{"type":24,"tag":738,"props":7422,"children":7423},{"style":755},[7424],{"type":29,"value":7425}," first_touch\n",{"type":24,"tag":738,"props":7427,"children":7429},{"class":740,"line":7428},31,[7430,7434],{"type":24,"tag":738,"props":7431,"children":7432},{"style":745},[7433],{"type":29,"value":324},{"type":24,"tag":738,"props":7435,"children":7436},{"style":769},[7437],{"type":29,"value":1921},{"type":24,"tag":738,"props":7439,"children":7441},{"class":740,"line":7440},32,[7442,7446],{"type":24,"tag":738,"props":7443,"children":7444},{"style":745},[7445],{"type":29,"value":310},{"type":24,"tag":738,"props":7447,"children":7448},{"style":769},[7449],{"type":29,"value":1921},{"type":24,"tag":25,"props":7451,"children":7452},{},[7453],{"type":29,"value":7454},"This one uses a bit of everything from this post:",{"type":24,"tag":343,"props":7456,"children":7457},{},[7458,7467,7506,7515],{"type":24,"tag":48,"props":7459,"children":7460},{},[7461,7465],{"type":24,"tag":350,"props":7462,"children":7463},{},[7464],{"type":29,"value":2227},{"type":29,"value":7466}," stacks tasks and events into one list of activities, because a follow-up could be either.",{"type":24,"tag":48,"props":7468,"children":7469},{},[7470,7482,7484,7489,7491,7497,7499,7504],{"type":24,"tag":350,"props":7471,"children":7472},{},[7473,7474,7480],{"type":29,"value":6360},{"type":24,"tag":283,"props":7475,"children":7477},{"className":7476},[],[7478],{"type":29,"value":7479},"first_touch",{"type":29,"value":7481}," CTE",{"type":29,"value":7483}," gets each lead's earliest activity with ",{"type":24,"tag":283,"props":7485,"children":7487},{"className":7486},[],[7488],{"type":29,"value":1292},{"type":29,"value":7490},", and ",{"type":24,"tag":283,"props":7492,"children":7494},{"className":7493},[],[7495],{"type":29,"value":7496},"date_diff",{"type":29,"value":7498}," turns the gap into minutes. Like the last example, it checks the converted contact too, since Salesforce moves a lead's activities there when it converts. The ",{"type":24,"tag":283,"props":7500,"children":7502},{"className":7501},[],[7503],{"type":29,"value":2199},{"type":29,"value":7505}," keeps leads nobody ever followed up on (they show up blank).",{"type":24,"tag":48,"props":7507,"children":7508},{},[7509,7513],{"type":24,"tag":350,"props":7510,"children":7511},{},[7512],{"type":29,"value":410},{"type":29,"value":7514}," sorts the minutes into buckets. There are 1,440 minutes in a day and 10,080 in a week. The numbers at the start of each label are only there to keep them in order.",{"type":24,"tag":48,"props":7516,"children":7517},{},[7518,7523],{"type":24,"tag":350,"props":7519,"children":7520},{},[7521],{"type":29,"value":7522},"Conversion % per bucket",{"type":29,"value":7524}," is the payoff. If \"Within an hour\" converts noticeably better than \"Over a week,\" speed matters for your team.",{"type":24,"tag":25,"props":7526,"children":7527},{},[7528,7530,7535,7537,7542,7543,7548],{"type":29,"value":7529},"Remember, this is a correlation. Reps might just be jumping on the leads that already look promising. If the pattern holds inside a single lead source (add ",{"type":24,"tag":283,"props":7531,"children":7533},{"className":7532},[],[7534],{"type":29,"value":5832},{"type":29,"value":7536}," to the ",{"type":24,"tag":283,"props":7538,"children":7540},{"className":7539},[],[7541],{"type":29,"value":288},{"type":29,"value":333},{"type":24,"tag":283,"props":7544,"children":7546},{"className":7545},[],[7547],{"type":29,"value":324},{"type":29,"value":7549},"), you've got a much stronger case for a faster follow-up rule. You'll need Task and Event synced for this one.",{"type":24,"tag":25,"props":7551,"children":7552},{},[7553,7555,7560],{"type":29,"value":7554},"Once a query becomes the way your team defines something, like pipeline or win rate, save it as a ",{"type":24,"tag":350,"props":7556,"children":7557},{},[7558],{"type":29,"value":7559},"model",{"type":29,"value":7561},". A model is a saved, versioned query that anyone in the workspace can query like a table, so everyone's working from the same number. And when Salesforce sits next to the rest of your data (Marketo, Google Ads, your product database), these same joins are how you tie it all together.",{"type":24,"tag":37,"props":7563,"children":7565},{"id":7564},"related-reading",[7566],{"type":29,"value":7567},"Related reading",{"type":24,"tag":44,"props":7569,"children":7570},{},[7571,7578],{"type":24,"tag":48,"props":7572,"children":7573},{},[7574],{"type":24,"tag":52,"props":7575,"children":7576},{"href":428},[7577],{"type":29,"value":431},{"type":24,"tag":48,"props":7579,"children":7580},{},[7581],{"type":24,"tag":52,"props":7582,"children":7584},{"href":7583},"\u002Fblog\u002Fwhat-is-a-semantic-model",[7585],{"type":29,"value":7586},"What is a semantic model?",{"type":24,"tag":25,"props":7588,"children":7589},{},[7590],{"type":24,"tag":505,"props":7591,"children":7592},{},[7593,7595,7601,7603],{"type":29,"value":7594},"Want to try these queries on your own Salesforce data? ",{"type":24,"tag":52,"props":7596,"children":7598},{"href":7597},"\u002F#videos",[7599],{"type":29,"value":7600},"Watch a demo",{"type":29,"value":7602},", or ",{"type":24,"tag":52,"props":7604,"children":7606},{"href":7605},"https:\u002F\u002Fapp.datachef.com\u002Flogin?utm_source=blog&utm_content=sql-101-for-salesforce-users",[7607],{"type":29,"value":7608},"try Datachef free.",{"type":24,"tag":7610,"props":7611,"children":7612},"style",{},[7613],{"type":29,"value":7614},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":7,"searchDepth":761,"depth":761,"links":7616},[7617,7618,7619,7622,7631,7637,7645],{"id":39,"depth":761,"text":42},{"id":274,"depth":761,"text":57},{"id":436,"depth":761,"text":66,"children":7620},[7621],{"id":534,"depth":877,"text":78},{"id":715,"depth":761,"text":87,"children":7623},[7624,7625,7626,7627,7628,7629,7630],{"id":725,"depth":877,"text":99},{"id":830,"depth":877,"text":108},{"id":979,"depth":877,"text":117},{"id":1110,"depth":877,"text":126},{"id":1498,"depth":877,"text":135},{"id":1668,"depth":877,"text":144},{"id":1961,"depth":877,"text":153},{"id":2204,"depth":761,"text":162,"children":7632},[7633,7634,7635,7636],{"id":2214,"depth":877,"text":174},{"id":2571,"depth":877,"text":183},{"id":2917,"depth":877,"text":192},{"id":3655,"depth":877,"text":201},{"id":4164,"depth":761,"text":210,"children":7638},[7639,7640,7641,7642,7643,7644],{"id":4169,"depth":877,"text":222},{"id":4388,"depth":877,"text":231},{"id":4595,"depth":877,"text":240},{"id":4942,"depth":877,"text":249},{"id":5508,"depth":877,"text":258},{"id":6435,"depth":877,"text":267},{"id":7564,"depth":761,"text":7567},"markdown","content:blog:sql-101-for-salesforce-users.md","content","blog\u002Fsql-101-for-salesforce-users.md","blog\u002Fsql-101-for-salesforce-users","md",1794442271273]