Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternate method for PIVOT - IN with sub-query support

As per oracle documentation, PIVOT function doesn't support sub-query in the "IN" clause but it is possible in the PIVOT XML function. E.g.

SELECT *
FROM table_name
PIVOT
(
  SUM(column_name_1)
  FOR [column_name_2] IN (['Output_Column1'],['Output_Column2'])
)
AS aliasName

I need to replace the ['Output_Column1'],['Output_Column2'] with sub-query.

Is there some other function equivalent to PIVOT where we can supply sub-query instead hard coding the entire output columns or even in the PIVOT function itself?.

like image 253
Vijay Krish Avatar asked Nov 14 '22 01:11

Vijay Krish


1 Answers

No, the number of columns must be known at parse time. For PIVOT XML there is no problem because such query returns only one column.

like image 183
Marcin Wroblewski Avatar answered Dec 09 '22 13:12

Marcin Wroblewski