gbrian / grafana-simple-sql-datasource

Grafana SQL datasource
MIT License
62 stars 20 forks source link

Query data show not correct #21

Closed ntting closed 6 years ago

ntting commented 6 years ago

Hi @gbrian Thanks very much for your worker, i have a problem, I want to show the y axis is F_Value_1, the x axis is time.

  1. how to write the sql?

The query sql is: SELECT F_Value_1 , F_BeginTime FROM dbo.T_InverterData_Min WHERE F_StationID=1 AND F_GridConnectedPointID=1 AND F_InverterID=1 AND F_BeginTime >= '$from' AND F_BeginTime <= '$to' ORDER BY F_BeginTime ASC But the the panel is not show correct line. the targets in response is error, one target become the many number. the number is the number of F_Value_1 different value. How to write the correct sql, please help me.

the sql server database is: 1

panel: 2

response: 3

buiductri commented 6 years ago

In my case, I write the SQL to return 3 fields: 'Time', 'Metric', 'Value' in correct column order. And then I sort the result by Time and Metric to make it draw correctly.

Here is an example:

SELECT  'Time'      = DATEADD(HOUR, -@server_zone, d.CounterTime)
    ,   'Metric'    = CASE LEFT(c.MachineName, 2) WHEN '\\' THEN SUBSTRING(c.MachineName, 3, LEN(c.MachineName)) ELSE c.MachineName END
    ,   'Value'     = SUM(d.CounterValue)
FROM        #CounterDetails AS c WITH (NOLOCK)
CROSS APPLY (
    SELECT  _d.CounterTime, _d.CounterValue
    FROM    dbo.CounterData AS _d WITH (NOLOCK)
    WHERE   _d.CounterID = c.CounterID
        AND _d.CounterTime BETWEEN @timefrom AND @timeto
) AS d
GROUP BY    d.CounterTime, c.MachineName
ORDER BY    'Time', 'Metric';

Hope this help.

gbrian commented 6 years ago

@ntting , Please follow @bily-gates suggestion and add the alias 'Time', 'Metric' and 'Value' to your columns. The plugin tries to automagically identify who is who ... but obviously is not doing so well :( I'll take a look at that asap.

Thanks @bily-gates

ntting commented 6 years ago

@bily-gates @gbrian Thanks very much, I test it, the result is correct according the reply of bily-gates

buiductri commented 6 years ago

@ntting Cool. Glad that helped. 😄