Convert row to column in influxDB - select

In my InfluxDB I want to convert the output of my query from raw to column.
The query is:
SELECT max(*) FROM table_X WHERE time > now() - 6m GROUP BY time(5m) fill(previous) ORDER BY DESC LIMIT 1
The result is:
But I want to get, for example, this output
How Can I do?
Help me please

Influx db provides the pivot function feature which will enable the converting rows to columns.
pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/pivot/
Note: Pivot function feature available in 2.x version of the influx db flux queries. In 1.xx version of influx database doesn't support pivot.

Related

How to run Grafana with QuestDB

I've imported a demo dataset in QuestDB and I can query it successfully from the console. I'm using Grafana to build a dashboard to test visualization.
My QuestDB installation is running on port 9000 and I can import it without any issues:
curl -F data=#weather.csv http://localhost:9000/imp
I'm running the following query which is failing:
SELECT timestamp as time,
avg(visMiles) AS average_visibility
FROM 'weather.csv'
WHERE $__timeFilter(timestamp)
SAMPLE BY $__interval
LIMIT 1000
The error I get is
pq: unknown function name: between(TIMESTAMP,STRING,STRING)
I'm using a dataset provided in their examples.
QuestDB relies on a designated timestamp specified during table creation. This would not cause an error if one was provided with the curl request as a URL param, given a column named 'timestamp':
curl -F data=#weather.csv http://localhost:9000/imp?timestamp=timestamp
Another option is during a SELECT operation, a timestamp() function can specify one dynamically. If you've imported using curl and not set a designated timestamp, there are two options:
Modify your query to use timestamp() on the column you want to designate:
SELECT timestamp as time,
avg(visMiles) AS average_visibility
FROM (‘weather.csv’ timestamp(timestamp))
WHERE $__timeFilter(timestamp)
SAMPLE BY $__interval
LIMIT 1000
Create a new table which is a copy of your original dataset but designate a timestamp during creation. ORDER BY is used because the demo dataset has unordered timestamp entries:
create table temp_table as (select * from ‘weather.csv’ order by timestamp) timestamp(timestamp);
And instead of querying your original dataset, use the temp_table:
SELECT timestamp as time,
avg(visMiles) AS average_visibility
FROM temp_table
WHERE $__timeFilter(timestamp)
SAMPLE BY $__interval
LIMIT 1000
If you need more info on the use of designated timestamps, the QuestDB concepts / timestamp docs page has further details.
Edit: There are some more resources to with this topic such as a guide for Grafana with QuestDB and GitHub repo with docker-compose.

Grafana PostgreSQL distinct on() with time series

I'm quite new to Grafana and Postgres and could use some help with this. I have a dataset in PostgreSQL with temperature forecasts. Mutiple forecasts are published at various points throughout the day (indicated by dump_date) for the same reference date. Say: at 06:00 today and at 12:00 today a forecast is published for tomorrow (where the time is indicated by start_time). Now I want to visualize the temperature forecast as a time series using Grafana. However, I only want to visualize the latest published forecast (12:00) and not both forecasts. I thought I would use DISTINCT ON() to select only the latest published forecast from this dataset, but somehow with Grafana this is not responding. My code in Grafana is as follows:
SELECT
$__time(distinct on(t_ID.start_time)),
concat('Forecast')::text as metric,
t_ID.value
FROM
forecast_table t_ID
WHERE
$__timeFilter(t_ID.start_time)
and t_ID.start_time >= (current_timestamp - interval '30 minute')
and t_ID.dump_date >= (current_timestamp - interval '30 minute')
ORDER BY
t_ID.start_time asc,
t_ID.dump_date desc
This is not working however since I get the message: 'syntax error at or near AS'. What should I do?
You are using Grafana macro $__time, so your query in the editor:
SELECT
$__time(distinct on(t_ID.start_time)),
generates SQL:
SELECT
distinct on(t_ID.start_time AS "time"),
which is incorrect SQL syntax.
I wouldn't use macro. I would write correct SQL directly, e.g.
SELECT
distinct_on(t_ID.start_time) AS "time",
Also use Generated SQL and Query inspector Grafana features for debugging and query development. Make sure that Grafana generates correct SQL for Postgres.

Converting a TEXT to dateColumn in Grafana

New to Grafana.
I have set a Postgres as a data source and am trying to create a sample time series dashboard like so...
SELECT
$__timeGroupAlias(UNIX_TIMESTAMP(start_time),$__interval),
count(events) AS "events"
FROM source_table
WHERE
$__timeFilter(UNIX_TIMESTAMP(start_time))
GROUP BY 1
ORDER BY 1
The problem is that in my table in postgres the start_time is of a type TEXT and this throws a
macro __timeGroup needs time column and interval and optional fill value
on Grafana side.
Can someone explain how can my start_time be properly converted to DateColumn so that the macros would work?
Thank you

DB2 : Need to get the list of columns and distinct value counts for a given db2 table

For data profiling purpose , I just need to get the idea if a columns in a given table has values populated or not. For that, I need to get the list of columns and distinct value counts for a given db2 table.
If you are using Db2 for Linux, Unix or Windows you could try
SELECT TABSCHEMA, TABNAME, COLNAME, COLCARD FROM SYSCAT.COLUMNS

PostgreSQL does not order timestamp column correctly

I have a table in a PostgreSQL database with a column of TIMESTAMP WITHOUT TIME ZONE type. I need to order the records by this column and apparently PostgreSQL has some trouble doing it as both
...ORDER BY time_column
and
...ORDER BY time_column DESC
give me the same order of elements for my 3-element sample of records having the same time_column value, except the amount of milliseconds in it.
It seems that while sorting, it does not consider milliseconds in the value.
I am sure the milliseconds are in fact stored in the database because when I fetch the records, I can see them in my DateTime field.
When I first load all the records and then order them by the time_column in memory, the result is correct.
Am I missing some option to make the ordering behave correctly?
EDIT: I was apparently missing a lot. The problem was not in PostgreSQL, but in NHibernate stripping the milliseconds off the DateTime property.
It's a foolish notion that PostgreSQL wouldn't be able to sort timestamps correctly.
Run a quick test and rest asured:
CREATE TEMP TABLE t (x timestamp without time zone);
INSERT INTO t VALUES
('2012-03-01 23:34:19.879707')
,('2012-03-01 23:34:19.01386')
,('2012-03-01 23:34:19.738593');
SELECT x FROM t ORDER by x DESC;
SELECT x FROM t ORDER by x;
q.e.d.
Then try to find out, what's really happening in your query. If you can't, post a testcase and you will be helped presto pronto.
try cast your column to ::timestamp like that:
SELECT * FROM TABLE
ORDER BY time_column::timestamp