How to change the mysql queries to postgresql query - mysql

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;

Related

Retrieve rows from DB if Date-Difference condition apply

I need to retrieve rows from SQL database where a time-difference condition apply.
Basically I need to retrieve rows where date difference from Now() is < x minutes.
Column datetime has this format: 2022-12-05 15:01:43
I tried
SELECT * FROM copytrade WHERE DATEDIFF(minute, datetime, GETDATE() AS DateDiff) < 10
Using this select I get error "Incorrect parameter count in the call to native function 'DATEDIFF'
Is it possible in a single SQL line to achieve this select?
Thanks

date_sub function in node-red

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

MySQL converting time to string (Grafana)

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.

QUERY [VIRTUAL] [ERROR] in DBeaver while querying the table in Denodo

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')

Postgresql: Trying to get Average of Counts for the last 10 ten days

Select avg(last_10_count) AS last_10_avg
(Select count(*)
from dim_user
where effective_date ::date > current_date -10
group by effective_date ::date) AS last_10_count
When I just run the inline query, I get the desired result, but when I run the whole query it throws the following error:
ERROR: function avg(record) does not exist
LINE 1: Select avg(last_10_count) AS last_10_avg
HINT: NO function matches the given name and arguement types.
You might need to add explicit type casts.
************Error***************
ERROR: function avg(record) does not exit
SQL state: 42883
Try this one
Select avg(last_10_count) AS last_10_avg from
(Select count(*) as last_10_count
from dim_user
where effective_date::date > current_date -10
group by effective_date :: date) Z