Single graph with multiple plots with MS SQL Server datasource

Hi, First post and request. I am constrained to using MS SQL server as a data source for my dashboard. I am trying to plot multiple plots on a single graph. The data base linked to this graph will change depending on user input but keeps the same column structure. To quickly describe:
I have Time, status, unit_number, lat, lon, event_code, size.

Within the unit_number column I have various machine unit numbers. I wish to plot a running total of event_codes separately for each unit number.

I am able to do this manually by having separate queries for each unit number (29 units) which is not too much of a task but is clunky.

I made the following query using a while loop in SQL server, which changes the name of the particular unit for the selection, and increments the name of the running total column (RunningTotal_0, RunningTotal_1…) for each iteration. In SQL server, each query is successful and the tables are produced. However in Grafana only the first iteration produces a graph.

Can anyone offer some advice to solve this issue or suggest a more simple approach (I am admittedly quite new to MS SQL)