• 1000 элегантное решение.
Пример желаемого результата:
str = "SELECT sample_id
, SUM(CASE WHEN gear_id = 1 THEN counts ELSE 0 END) AS g1
, SUM(CASE WHEN gear_id = 2 THEN counts ELSE 0 END) AS g2
...
, SUM(CASE WHEN gear_id = N THEN counts ELSE 0 END) AS gN
FROM table1 GROUP BY sample_id"