TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst · 76.7K subscribers
Post #2789 910
SELECT transaction_date,
CASE WHEN EXTRACT(DOW FROM transaction_date) IN (0, 6) THEN 'Weekend' ELSE 'Weekday' END AS day_type
FROM transactions;


📊 20. Business Example — Peak Sales Day

Suppose we want to find which date generated the highest revenue.

WITH daily_sales AS (
SELECT order_date, SUM(amount) AS daily_revenue FROM orders GROUP BY order_date
)
SELECT order_date, daily_revenue FROM daily_sales ORDER BY daily_revenue DESC LIMIT 1;


🧠 21. Date Truncation vs Date Extraction

Extraction gets a component: EXTRACT(YEAR FROM order_date) → 2026

Truncation converts to a larger bucket: DATE_TRUNC('month', order_date) → 2026-09-01

• EXTRACT → Give the part

• DATE_TRUNC → Give the period bucket

🔄 22. Adding Intervals to Dates

Sometimes you need to calculate a future or previous date.

For example, PostgreSQL:

SELECT order_date, order_date + INTERVAL '7 days' AS follow_up_date FROM orders;


This can be useful for follow-up dates, SLA deadlines, subscription periods, reminder dates.

📉 23. Finding Inactive Customers

One approach is:

SELECT customer_id, MAX(order_date) AS last_order_date FROM orders GROUP BY customer_id;


Then filter:

WITH customer_activity AS (
SELECT customer_id, MAX(order_date) AS last_order_date FROM orders GROUP BY customer_id
)

SELECT customer_id, last_order_date FROM customer_activity WHERE last_order_date < CURRENT_DATE - INTERVAL '90 days';


📈 24. Year-over-Year Analysis

WITH monthly_sales AS (
SELECT DATE_TRUNC('month', order_date) AS month, SUM(amount) AS revenue
FROM orders GROUP BY DATE_TRUNC('month', order_date)
)

SELECT month, revenue, LAG(revenue, 12) OVER (ORDER BY month) AS previous_year_revenue
FROM monthly_sales;


Here LAG(..., 12) looks 12 rows backward. If months are missing, LAG(..., 12) may not correspond to same calendar month one year earlier. For robust YoY, explicitly align dates or build a complete calendar.

📅 25. Calendar Tables

A calendar table contains one row for each date in a period.

Example:

• 2026-01-01 | 2026 | 1 | January | Thursday

• 2026-01-02 | 2026 | 1 | January | Friday

Calendar tables are extremely useful for time-series analysis, missing-date detection, reporting, fiscal calendars, holidays, business days, YoY comparisons.

🏦 26. Financial Year Analysis

Business calendars don't always follow January–December. For example April → March. You can create fiscal-year logic using date functions and CASE.

Conceptually:

CASE
WHEN EXTRACT(MONTH FROM order_date) >= 4 THEN EXTRACT(YEAR FROM order_date) + 1
ELSE EXTRACT(YEAR FROM order_date)
END AS fiscal_year
  • ❤ 2
More from @sqlanalyst
  1. Oct 7, 2026SQL Interview Series — Part 4 📌 Question 4: Find the Highest Salary in Each Department Su…
  2. Oct 7, 2026𝗠𝗮𝘀𝘁𝗲𝗿 𝗣𝗼𝘄𝗲𝗿 𝗕𝗜 𝗳𝗼𝗿 𝗙𝗥𝗘𝗘! 🔥 Learn Power BI through these FREE learnin…
  3. Sep 29, 2026SQL Interview Series — Part 2 📌 Question 2: Find Duplicate Records Suppose you have an Em…
  4. Sep 29, 2026𝗙𝗥𝗘𝗘 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀 𝗧𝗼 𝗟𝗲𝗮𝗿𝗻 𝗔𝗜 𝗶𝗻 𝟮𝟬𝟮𝟲🚀 ​ Explore 6 free resource…
  5. Sep 29, 2026SQL Interview Series — Part 1 Hi guys, let's start a SQL interview series covering frequen…
  6. Sep 28, 2026🧠 Real-World SQL Scenario-Based Questions & Answers 1. Get the 2nd highest salary from th…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →