Spreading Data across Months on a Single Row

Spreading Data across Months on a Single Row

This is a SQL trick for spreading out data across months on a single row. It also works with Sybase TSQL. The 1-ABS(SIGN()) function will evaluate to zero for all months outside of column.

 SELECT	 A,B,C,FISCAL_YEAR,may = SUM(T1.number_of_cases * (1 - ABS(SIGN(T1.fiscal_month - 1)))),jun = SUM(T1.number_of_cases * (1 - ABS(SIGN(T1.fiscal_month - 2)))),jul = SUM(T1.number_of_cases * (1 - ABS(SIGN(T1.fiscal_month - 3)))),aug = SUM(T1.number_of_cases * (1 - ABS(SIGN(T1.fiscal_month - 4)))),sep = SUM(T1.number_of_cases * (1 - ABS(SIGN(T1.fiscal_month - 5)))),oct = SUM(T1.number_of_cases * (1 - ABS(SIGN(T1.fiscal_month - 6)))),nov = SUM(T1.number_of_cases * (1 - ABS(SIGN(T1.fiscal_month - 7)))),dec = SUM(T1.number_of_cases * (1 - ABS(SIGN(T1.fiscal_month - 8)))),jan = SUM(T1.number_of_cases * (1 - ABS(SIGN(T1.fiscal_month - 9)))),feb = SUM(T1.number_of_cases * (1 - ABS(SIGN(T1.fiscal_month -10)))),mar = SUM(T1.number_of_cases * (1 - ABS(SIGN(T1.fiscal_month -11)))),apr = SUM(T1.number_of_cases * (1 - ABS(SIGN(T1.fiscal_month -12))))FROM	 table_name T1GROUP BY A,B,C,FISCAL_YEAR
Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular