Posts

Showing posts from March, 2022

Create a Simple Calendar or Date Dimension On-the-fly

 I have a report where I want the user to pick from 3 options for the date range on the report: Latest business date; generally this would be yesterday Week-to-date Month-to-date The SQL below was actually used in Snowflake. Change CURRENT_DATE to GETDATE() for SQL Server. The idea is to get the list of dates from the CTE that are relevant and join to any fact table based on the value of the CALENDAR_KEY column. I use a format of yyyymmdd for CALENDAR_KEY. WITH CTE_DATES (DATE_FILTER, CALENDAR_KEY)  AS (     -- BUSINESS_DATE     SELECT         'BUSINESS-DATE' AS DATE_FILTER     ,   CALENDAR_KEY        FROM CALENDAR     WHERE FULL_DATE = CURRENT_DATE - 1       UNION ALL     -- CURRENT WEEK thru BUSINESS_DATE     SELECT         'WEEK-TO-DATE'     ,   CALENDAR_KEY     FROM CALENDAR     WHERE D...

Creating a date dimension or calendar table in SQL Server

https://www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server  

How do I prevent a trigger from firing for one process?

https://am2.co/2022/03/how-do-i-prevent-a-trigger-from-firing-for-one-process/  

Stairway to Advanced T-SQL Level 5: Turning Data On Its Side Using PIVOT Operator

https://www.sqlservercentral.com/steps/stairway-to-advanced-t-sql-level-5-turning-data-on-its-side-using-pivot-operator