reformat CTE query
added references for pivot sample