'Why does PIVOT logic require IN statement, even when using WITH [table_name] AS syntax

I am just getting started with GBQ but ran into a logic issue (as it seems to me).

When I run this query, it works great, but it requires the IN operator in the PIVOT clause -

WITH Midn as (
SELECT m.CLYE_CLASS_APPLIED_FOR as Class_Year, m.ALPHA, m.MIST_CODE
    FROM `aw-mids.MIDS.MIDS_ALL` m
    where m.CLYE_CLASS_APPLIED_FOR between 2022 and 2025
      and m.MIST_CODE not in ('96', '97')
)

select * from 
    (select Class_Year, Alpha, Mist_Code from Midn)
    PIVOT 
    (count(alpha) as Num_Midn for Class_Year in (2022, 2023, 2024, 2025) )

Without the IN in the PIVOT clause, I get "Syntax Error: Unexpected ")" at [11:50].

Notes:

  • I used the WITH [table_name] AS syntax because I thought this would preclude me from having to enter the IN operator in the PIVOT clause, but it is required ...

  • In the PIVOT clause, IN is required, you cannot use BETWEEN, <=, >= etc.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source