r/SQL • • 1d ago

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

/r/Egypt_Developers/comments/1ycw1rc/built_a_sales_analytics_project_in_sql_server_and/
1 Upvotes

5 comments sorted by

2

u/Sanbi102 1d ago

For the customer segments, try an explicit as-of date instead of GETDATE()/current_date, and exclude sales after that date. That makes historical segments reproducible and lets you compare SQL Server and DuckDB against the same snapshot.

1

u/Bassiette03 1d ago

Okay Awesome tip. I will update the script thank you sir much appreciated

1

u/Discordbothost 1d ago

Fill in missing product-year rows before the yearly LAG comparison. Otherwise it compares with the previous year that had sales, which may not be last year. Keep 'no sales' separate from years before the product existed.

1

u/Bassiette03 1d ago

Thank you for the feedback. I will update it Thank you much appreciated 👍

1

u/No_Addition_992 15h ago

Nice project — clean star schema and good use of window functions. For the moving average, give it a fixed window instead of a running one: AVG(sales) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) gives a true 3-month moving average. That plus the BETWEEN fix at 500 would tidy up the two things you flagged.