r/Egypt_Developers • • 3h ago

Data analysis Built a sales analytics project in SQL Server and Similar one with DuckDB

I put together an end-to-end analysis project in SQL and would like some feedback from people who work with this daily.

Setup: a star schema with one fact table (fact_sales) and two dimensions (dim_products, dim_customers). Everything is plain SQL, no BI tool involved.

What I covered:

  • Monthly trends in sales, distinct customers and quantity, using DATETRUNC
  • Running totals and moving averages with window functions
  • Yearly product performance compared against each product's own average and the previous year (AVG() OVER (PARTITION BY ...) and LAG())
  • Part-to-whole: each category's share of total sales
  • Segmentation: products by cost range, and customers as VIP / Regular / New based on lifespan and total spend
  • A report_customers view with age group, recency, lifespan, average order value and average monthly spend

What I took from it:

Window functions did most of the heavy lifting. Anything involving "compared to last year" or "compared to its own average" was far easier with LAG() and partitioned averages than with self-joins. Putting the customer metrics in a CTE first and the segmentation logic on top also kept the final view readable.

I then ran the same analysis in DuckDB locally. No server to set up, and it was quick on this dataset. Most queries worked with small changes, like DATETRUNC to date_trunc and GETDATE() to current_date. [Adjust this part to match what you actually changed.]

Things I know could be better:

  • The cost ranges use overlapping BETWEEN boundaries at 500, which I should clean up
  • Age uses DATEDIFF(year, ...), which is only approximate
  • The "moving average" is really a cumulative average, not a fixed-window one

Repo with the full script and README:Repo Link

DuckDB repo

I'm looking for a job so if anyone know a company that need long term SQL and excel professional user. I will appreciate if you contacted me in DM or my Social Media accounts

I'd appreciate thoughts on the segmentation logic, whether you'd structure the customer view differently, and anything that looks off in the queries.

1 Upvotes

1 comment sorted by

1

u/AutoModerator 3h ago

سعيدين جدا بمشاركتك معانا

لو لسه جديد، ادخل سيرفر الديسكورد هناك بنشارك مصادر ونتكلم مع بعض في البرمجة والشغل.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.