http://www.perlmonks.org?node_id=1187041


in reply to Re: How to add columns with new row name using perl from mysql query?
in thread How to add columns with new row name using perl from mysql query?

This isn't a ROLLUP, because you don't want totals. Instead, you could use the UNION ALL, but use a CTE so the subquery is only applied once. That is, use a WITH to SELECT all the rows in the TABLE, but add ROW_NUMBER() OVER (ORDER BY queue_name). To avoid the UNION ALL (which in this case would have little to no point, as the CTE already makes a centralized query) you could wrap the ROW_NUMBER() in a CASE to go no higher than 6.Then a single aggregate query with a GROUP BY on the row number.