What I am trying to do is display the duration of a major incident record based on the following conditions:
if HPD.Closed Date = NULL, then give duration from current date.
if HPD.Closed Date has a date in it, then the incident has been closed and I want to see the duration from the reported date to the closed date.
My original “(DATEDIFF (‘ss’, HPD.Reported Date, currentDate())) As Duration,” gives the rolling duration, however it keeps adding time even when an incident is closed.
My SQL:
USING: BMC PostgresSQL
SELECT
HPD.`Incident Number`,
HPD.`Major Incident`,
HPD.`Reported Source`,
HPD.`Major Incident Accepted Date`,
HPD.`Closed Date`,
--(DATEDIFF ('ss', HPD.`Reported Date`, currentDate())) As Duration,
CASE WHEN HPD.`Closed Date` = NULL THEN
(DATEDIFF ('ss', HPD.`Reported Date`, currentDate()))
ELSE
(DATEDIFF ('ss', HPD.`Reported Date`, HPD.`Closed Date`)) As Duration,
HPD.`Last Resolved Date`
FROM
`HPD:Help Desk` HPD
Comment some lines in your SQL until you will be able to identify which line(s) is causing a problem. Than check PostgreSQL doc how correct syntax should looks like for that line.
I would say that at leat “= NULL” is not correct. I bet “IS NULL” is correct (but I’m not saying that’s the only one issue in your SQL - there can be much more - you have really terrible naming convention , so it is hard to read ).
I don’t know. Standard approach is to improving/developing SQL until it is not failing and it it is returning desired result. You have data, so only YOU can try it.
I don’t have access to full grafana postrgesql as this is BMC’s version of grafana
if you comment out the case and just use
(DATEDIFF (‘ss’, HPD.Reported Date, currentDate())) As Duration,
[ NOTE: Reported Date surrounded by single quotes, is the correct format for BMC grafana postgresql ]
everything works, so the SQL issue has to do with my case statement.
However this is not giving the the result I want. it will always give me duration based on current date even when the incident is closed.
Tried the querying tool and I get the following
"… “messageType”: “ERROR”,
“messageText”: "Failed to parse the SQL ",
“messageAppendedText”: “[line 11:59] mismatched input ‘As’ expecting END. SQL …”
thanks, for the suggestion. I fixed it by changing
ELSE
(DATEDIFF (‘ss’, HPD.Reported Date, HPD.Closed Date)) As Duration,
to
ELSE
(DATEDIFF (‘ss’, HPD.Reported Date, HPD.Closed Date))
END As Duration,
and that worked. @yosiasz . Thanks for pointing me in the right direction to solve this.
much appreciated.