Converting a TEXT to dateColumn in Grafana - postgresql

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

Related

How to use datetime column as a time series in Grafana using Postgres

Hi guys I have a table in Postgres basically storing a door time entry. for example everytime you scan a card to open a door we store the datetime and your id
table format
CREATE TABLE doorentry(date CHARACTER VARYING(50),id VARCHAR(255))
dataset looks like this
[{ "id" : "aadams1", "date" : "09-10-2022-14:55:30"},{ "id" : "jjames2", "date" : "09-10-2022-14:55:31"}]
I would like to group the dataset by their timestamp per hour.
I have sort of achieved that with
SELECT SUBSTRING(date,0,16) AS byhour, COUNT(id)
FROM doorentry GROUP BY byhour ORDER BY byhour;
that looks something like this
enter image description here
Would appreciate any help showing this data as a time series graph/new query to be able to use Grafana drop down to show data for the last day or hour like this:
enter image description here
Use:
SELECT
$__timeGroupAlias(<TIMESTAMPZ column>, '1h'),
count(id) AS "count"
FROM doorentry
WHERE
$__timeFilter(<TIMESTAMPZ column>)
GROUP BY 1
ORDER BY 1
But you don't have TIMESTAMPZ type in date, but VARCHAR. I would recommend to use TIMESTAMPZ for date column. With VARCHAR you have to convert varchar to timestamp first with SQL, e.g. TO_TIMESTAMP(date, 'MM-DD-YYYY HH:MI:SS').
See doc for available macros: https://grafana.com/docs/grafana/latest/datasources/postgres/#macros Those macros generate more complex SQL under the hood.

Reorganize the data by months in grafana using psql

im trying to do a "AVG" by months but grafana wont let me do it because im using two Selects and the first one is using the other select and i can`t specifie my time column.
I want to get a "time series"(type of grafana dashboard) where it show´s me the average by month in grafana but i dont know how could i do it with psql and the code i have.
This is the code im using:
SELECT AVG(lol), NOW() as time FROM
(SELECT COUNT(DISTINCT(ticket_id)), SUM(time_spent_minutes) AS "lol"
FROM ticket_messages
WHERE admin_id IN ('20439', '20457', '20291', '20371', '20357', '20235','20449','20355','20488')
GROUP BY ticket_id) as media
Where is the temporal column coming from? As of now your query is not doing anything specifically.
I would think that probably you have a ticket_date column available somewhere, the below query could become
SELECT
EXTRACT(MONTH FROM ticket_date) ticket_month,
SUM(time_spent_minutes)/COUNT(DISTINCT ticket_id) avg_time
FROM ticket_messages
WHERE
admin_id IN ('20439', '20457', '20291', '20371', '20357', '20235','20449','20355','20488')
GROUP BY EXTRACT(MONTH FROM ticket_date)

Can't filter date and time record using postgres

I'm trying to filter a record by date-time, but no success.
When I send the request from the client to the server, I can see that the date and time matches with the value stored in the entity. However, there's something I'm not understanding on the record, and I think it is the problem from not succeeding the filter.
I'd like to know how to store only date and time in postgres.
You can see in the image that it is not just storing the date and time, it has more information, and I would like to know how to save the date and time only, without this numerical sequence that comes after the time.
For example, I don't want it to be saved as: 2022-03-28 09:50:26.077
I want it to be saved like this: 2022-03-28 09:50:26
I've done a lot of research and haven't found any resources that can help me with this issue. I really don't know why it's saving this number sequence, I don't know if this is something that involves timezone...
Script SQL
create table mytable (
...
column_name timestamp not null default now()::timestamp
);
How to proceed with this? Is there any technique to remove those numbers and leave only the date and time?
You can use DATE_TRUNC():
CREATE TABLE mytable (
id int,
column_name timestamp not null default date_trunc('second', now()::timestamp)
);
INSERT INTO mytable(id) VALUES(1) RETURNING *;

Date time type in postgresql

I need storage 2 colums with date_time in my postgresql DB table.
date_time date_time_human
676484556463346 09.06.2017 9:38:00
date_time - like oracle timestamp( this date in seconds)
date_time_human - date in normal form
What type of field should be?
INSERT INTO tabl (date_time, date_time_human) VALUES(now(), now())
It seems to me that if you have the same value in these fields, then there is no sense in starting it twice.
Create one field of type timestamp. And in seconds you can output and record using functions.
Data Type Formatting Functions

how to insert a time in oracle 10g database

I want to insert date and time in oracle database, I have created the table with columns
create table myadmin
( employe_id number(5),
supervisor Varchar2(20),
department Varchar2(20),
action Varchar2(20),
sdate date,
stime date)
While inserting the values below it gives an error. Please tell me how to insert the time ?
insert into myadmin
( employe_id,supervisor,department,action,sdate,stime) values
(83,'gaurav','helpdesk','pick','23-jan-2013','09:43:00');
You have to use keyword to_date for date insert in oracle like this.
to_date('23-01-2013','dd-mm-yyyy')
Basically you have to use keyword to_date('your date','your date format').
You can also add date and time together if you want and it would be something like this
to_date('23-01-2013 09:43:00','dd-mm-yyyy hh24:mi:ss')
A date in Oracle always has a date part and a time part. Having date and time in two separate columns only makes sense, if it can occur that date is null and time is not. (And still, you could set date to an improbable value like 1.1.0001 then.)
However, if you want to stick to those two separate fields, then make your string a datetime with the to_date function specifying the format used:
insert into myadmin
( employe_id,supervisor,department,action,sdate,stime) values
(83,'gaurav','helpdesk','pick',to_date('23-01-2013','dd-mm-yyyy'), to_date('09:43:00', 'hh24:mi:ss'));