Rewrite the following SQL to use Common Table Expressions instead: SELECT COUNT(*) AS…
- User
Rewrite the following SQL to use Common Table Expressions instead:
SELECT COUNT() AS num_promokits, AVG(days_since_last_view) AS avg_days_since_last_view, AVG(days_between_last_viewed_and_last_received) AS avg_days_btwn_last_viewed_and_received, SUM(num_unread_leads) AS total_unread_leads, SUM(num_unread_direct_leads) AS total_unread_direct_leads FROM ( SELECT event_inquiries.promokits_ID, datetime_last_lead_viewed, DATEDIFF(NOW(), datetime_last_lead_viewed) AS days_since_last_view, DATEDIFF(datetime_last_lead_received, datetime_last_lead_viewed) AS days_between_last_viewed_and_last_received, COUNT() AS num_unread_leads, SUM(IF(source = 'Direct Contact', 1, 0)) AS num_unread_direct_leads FROM promokits INNER JOIN ( SELECT event_inquiries.promokits_ID AS
promokits_ID, MAX(event_inquiries.datetime_promokit_viewed) ASdatetime_last_lead_viewed, MAX(event_inquiries.datetime_added) ASdatetime_last_lead_receivedFROM event_inquiries GROUP BY event_inquiries.promokits_ID ) test ON promokits.ID = test.promokits_ID INNER JOIN event_inquiries ON promokits.ID = event_inquiries.promokits_ID WHERE promokits.gigsalad_hidden = 0 AND promokits.gigsalad_active = 1 AND promokits.approved = 1 -- All the qualifications necessary to be active on the site and still receiving leads AND event_inquiries.datetime_added < (NOW() - INTERVAL {{buffer_days}} DAY) -- A period of time in which we give members an opportunity to read their leads; helps to ensure folks aren't just on vacation AND event_inquiries.datetime_added > test.datetime_last_lead_viewed -- Only count the leads that have been received more recently than the last time the PromoKit read one of their leads GROUP BY promokits.ID HAVING num_unread_leads >= {{unread_lead_min}} ORDER BY num_unread_leads DESC ) source - Assistant
Here's the rewritten SQL using Common Table Expressions (CTEs):
sql
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.