how to transform data to required format of bar chart panel

I query the mysql database and get the result as following (table 1):

City network count(*)
C1 N1 24
C2 N1 60
C1 N2 20
C2 N2 58

The value of city and network cannot be hard-coded. There may be C3, C4, and N3, N4….

I need to show the data in Grafana with bar char panel as following,
tt

so I have to transform my table into another format as following (table2)
city N1 N2
C1 24 20
C2 60 58

How to make the transform in grafana? I tried rewriting sql to get table 2 directly, also tried transform table in transform tab of panel edit page, nothing works.