How to create a month-wise pivot using Oracle PIVOT.

You can create a month-wise pivot using Oracle PIVOT

Since your period is FY 2025-26 (Apr-2025 to Mar-2026), we first extract the month from CREATED_DATE and then pivot.

Month-wise Pivot Query

SELECT *
FROM
(
SELECT ZONE,
       INSTL_ADDR_STATE_CODE CIRCLE,
       FETCH_BA(INSTL_ADDR_SSA_CODE) BA,
       TO_CHAR(CREATED_DATE,'MON') MON
FROM FMS_CAF_CA_BA
WHERE ORD_SALES_FRANCHISEE_CODE IN
(
   SELECT FRANCHISEE_CODES
   FROM FRANCHISEE_MAPPING
   WHERE USERNAME IN
   (
      SELECT USERNAME
      FROM FMS_USERS
      WHERE SERVICE_TYPE='DSA'
      AND FR_TYPE='DSA'
      AND ZONE IN ('NZ','EZ')
      AND USERNAME IN
      (
         SELECT DISTINCT USERNAME
         FROM FMS_FRANCHISE_CREATE_TEMP
         WHERE FR_TYPE='DSA'
         AND ZONE IN('EZ','NZ')
      )
   )
)
AND CREATED_DATE >= DATE '2025-04-01'
AND ORD_CREATED_FLG IN ('Y','C','P')
)
PIVOT
(
COUNT(*) 
FOR MON IN
(
'APR' AS APR,
'MAY' AS MAY,
'JUN' AS JUN,
'JUL' AS JUL,
'AUG' AS AUG,
'SEP' AS SEP,
'OCT' AS OCT,
'NOV' AS NOV,
'DEC' AS DEC,
'JAN' AS JAN,
'FEB' AS FEB,
'MAR' AS MAR
)
)
ORDER BY ZONE, CIRCLE, BA;

Output Format



Post a Comment

And that's all there is to it!

If anyone has any other questions or requests for future How To posts, you can either ask them in the comments or email me. Please don't feel shy at all!

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

Previous Post Next Post