I am having a little trouble showing the number of times a user is running a SW version/HW version through Grafana. The following query works correctly in MySQL Workbench but when done in Grafana, the following error is given: Invalid type for column time/time_sec, must be of type timestamp or unix timestamp, got: string 1536692387
I have tried converting time_sec into a DATETIME type through CAST and CONVERT but have had no success. Does anyone have suggestions on how to handle this? Thank you.
Here is the current query (through workbench-the variables are defined in Grafana):
SET #$swVersion = 'LATEST';
SET #$hwString = 'TXT';
SELECT
IF(COUNT(user) > 0, epochStartTime, NOW()) AS time_sec,
COUNT(user) AS 'John Doe'
FROM
table
WHERE
STRCMP(user, 'Name') = 0
AND swVersion IN (#$swVersion)
AND hwString IN (#$hwString)
Try this:
UNIX_TIMESTAMP(IF(COUNT(user) > 0, epochStartTime, NOW())) AS time_sec
in your query.
Related
Trying and failing to get a date-sub function to work in a node-red function. MySQL db.
Without any date_sub, this works fine (in a function)-
var options = { hour12: false };
var nowtime = new Date().toLocaleString("en-GB", options);
msg.topic = `SELECT COUNT(*) AS rowcount FROM \`node-red\`.\`tag_reads\` WHERE \`datetime\` < "${nowtime}"`;
return msg;
With the date_sub part the query fails (with a count of zero even though there are valid records, the query works fine in a conventional jsp)
msg.topic = `SELECT COUNT(*) AS rowcount FROM \`node-red\`.\`tag_reads\` WHERE \`datetime\` > date_sub( "${nowtime}", INTERVAL 90 MINUTE) `;
I suspect it is syntax, escape codes etc. The resulting sql string appears to show that datetime (from the table) is not being interpreted as a string (just 'datetime' appears rather than the contents of datetime which are, for example, 10/6/2022, 11:18:43), but, if this was the case then the first select statement would not be working either.
Thoughts appreciated
Ralph
it looks like in this statement
WHERE \`datetime\` < "${nowtime}" mysql is converting the string to datetime for you.
MySQL has the ability to compare two different dates written as a string expression.
from How to do date comparisons in MySQL
However, in this statement, date_sub( "${nowtime}", date_sub wants the argument to be of type date.
Syntax
DATE_SUB(date, INTERVAL value interval)
from MySQL DATE_SUB() Function
When I execute the mysql query in postgresql it is showing the error .I have searched in google about the error but they are not working for me.Please suggest me to solve.
below is the mysql query:
SELECT caller_id_number,
caller_id_name,
dialed_number,
count(*) NumberOfCalls,
round(sum(duration_seconds)/60,2) CallDurationMin
FROM kazoo_cdr_rpt
WHERE cdr_date_time > '20-08-2017'
AND cdr_date_time < 24-08-2017
AND length(caller_id_number) < 5
AND direction = "outbound"
GROUP BY dialed_number;
when I execute above query in postgres it is getting the error as
round(sum(durati...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
If your duration second is varchar, you'll have to convert it to a number:
SELECT caller_id_number,
caller_id_name,
dialed_number,
count(*) NumberOfCalls,
round(sum(CAST(duration_seconds as numeric))/60,2) CallDurationMin
FROM kazoo_cdr_rpt
WHERE cdr_date_time > '20-08-2017'
AND cdr_date_time < '24-08-2017'
AND length(caller_id_number) < 5
AND direction = "outbound"
GROUP BY caller_id_number, caller_id_name, dialed_number;
I ran this query in DBeaver
SELECT DLY.badge_nbr,
DLY.DIM_DT_ID,attribute_type
FROM FACT_MDM_DAILY_INT DLY
WHERE SCENARIO_TYPE = 'VOLTAGE'
AND ATTRIBUTE_TYPE = 'Phase_A_Average_RMS_Voltage'
AND DLY.dim_dt_id >= TO_DATE('2016-01-28','yyyy-mm-dd');
I get the error as QUERY [VIRTUAL] [ERROR]. Interestingly when I run the same query without date comparison in WHERE clause it works fine.
SELECT DLY.badge_nbr,
DLY.DIM_DT_ID,attribute_type
FROM FACT_MDM_DAILY_INT DLY
WHERE SCENARIO_TYPE = 'VOLTAGE'
AND ATTRIBUTE_TYPE = 'Phase_A_Average_RMS_Voltage';
The to_date() function in Denodo must have at least 2 parameters:
The date format of your string field (look at java SimpleDateFormat)
The string you want to convert to a date.
Thus, your parameters appear to be transposed, and you must use a capital M for month... since lower case m means minutes.
to_date('yyyy-MM-dd','2016-01-28')
I must be doing something wrong. I'm trying to search the database on a OrderDate column which is stored as a string ex. "7/21/2016 9:13:31 PM"
AND I want to convert OrderDate to a unix timestamp for the query statement :
UNIX_TIMESTAMP(STR_TO_DATE(OrderDate, '%e/%c/%Y %r')) >=
'1471496400' AND UNIX_TIMESTAMP(STR_TO_DATE(OrderDate, '%e/%c/%Y
%r')) <= '1471669199'
There are plenty of results in the table that should match this. But I'm not getting any of them. What am I doing wrong?
I've read the manual and searched the internet and cannot figure out what I am doing so please don't tell me to RTM
This worked for me
UNIX_TIMESTAMP(STR_TO_DATE(OrderDate, '%c/%e/%Y')) >= '$timestamp1'
AND UNIX_TIMESTAMP(STR_TO_DATE(OrderDate, '%c/%e/%Y')) <= '$timestamp2'
I've got a table with 9 million records. Each one has a "BirthDate" field that is stored as a varchar. I'm trying to select all where the birthdate is 25 years or less (all people 25 or under). It's failing, because somewhere in this monstrosity of a table, there is an invalid value.
select COUNT(*) from LeadSplit where CAST(LeadSplit.Birthdate as datetime) > DATEADD(yy, -26, getdate()) and Birthdate is not null
The error is:
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I'm at a loss as to how to find the row with the invalid value, and also, how to deal with it. I would like to just ignore it or fix it.
You could try to find the offending rows by doing something like:
SELECT (some ID), Birthdate
FROM dbo.YourTable
WHERE ISDATE(Birthdate) = 0
This might not work for all cases - but it might give you a starting point.
The error you are getting is that one or more of the dates cannot be parsed given the DateFormat setting.
Set DateFormat MDY
GO
Select Count(*)
From LeadSplit
Where Case
When IsDate(BirthDate) = 0 Then 0
When BirthDate >= DateAdd(yyyy,-25,CURRENT_TIMESTAMP) Then 1
End = 1
Try to use CONVERT instead of CAST and explicit specify date format. MSDN Article about CAST and CONVERT