Now I get the average datediff respecting this sequence
If tclosed IS NOT NULL AND tcompleted IS NOT NULL AND tigx IS NOT NULL the AVG is ROUND(AVG(DATEDIFF(tclosed, tdate)),1)
If tclosed IS NULL AND tcompleted IS NULL AND tigx IS NULL the AVG is ROUND(AVG(DATEDIFF(CURDATE(), tdate)),1)
But if at least one of the fields tigx or tclosed or tcompleted is null I have to take the first populated field from tigx or tclosed or tcompleted ⦠⦠and using this first populated field for get the average datediff.
Here I am stuck⦠any suggestion?
My query below
SELECT
tkt,
tdate,
tstate,
tigx,
tclosed,
tcompleted,
CASE
WHEN ( tclosed IS NOT NULL AND tcompleted IS NOT NULL AND tigx IS NOT NULL ) THEN
ROUND( AVG( DATEDIFF( tclosed, tdate )), 1 )
WHEN ( tclosed IS NULL AND tcompleted IS NULL AND tigx IS NULL ) THEN
ROUND( AVG( DATEDIFF( CURDATE(), tdate )), 1 )
END AS `avg`
FROM
`tbl_c`
WHERE
NOT ( `tkt` LIKE '%I%' OR `tkt` LIKE '%L%' AND tstate IN ( 'Closed' ) AND tigx IS NULL AND tclosed IS NULL AND tcompleted IS NULL )
ORDER BY
tdate DESC;
the answer you seek will probably best be provided by judicious construction of a COALESCE function, listing columns in the appropriate sequence
what do you mean by average? you use AVG() which is an aggregate function, but youāre not aggregating⦠did you want to aggregate by date? average for all tickets for each date?
if so, you cannot show the individual row details, just the date and the aggregate(s)
p.s. you are going to have problems mixing ORs and ANDs that way
SELECT
tkt,
tdate,
tstate,
tigx,
tclosed,
tcompleted,
CASE
WHEN ( tclosed IS NOT NULL AND tcompleted IS NOT NULL AND tigx IS NOT NULL ) THEN
ROUND( AVG( DATEDIFF( tclosed, tdate )), 1 )
WHEN ( tclosed IS NULL AND tcompleted IS NULL AND tigx IS NULL ) THEN
ROUND( AVG( DATEDIFF( CURDATE(), tdate )), 1 ) ELSE ROUND(
AVG(
DATEDIFF( COALESCE ( tclosed, tcompleted, tigx ), tdate )),
1
END AS `avg`
FROM
`tbl_c`
WHERE
NOT ( `tkt` LIKE '%I%' OR `tkt` LIKE '%L%' AND tstate IN ( 'Closed' ) AND tigx IS NULL AND tclosed IS NULL AND tcompleted IS NULL )
GROUP BY
tkt
ORDER BY
tdate DESC;
actually your entire CASE expression can be done with one COALESCE
if you GROUP BY tkt then you cannot show tdate, tstate, tigx, tclosed, or tcompleted, nor can you sort by tdate ā on MySQL version 8 you should be getting an āonly full group byā error
this is the last sql, the GROUP BY is for tarea and not for tkt
what do you mean with
actually your entire CASE expression can be done with one COALESCE
SELECT
tarea,
tkt,
tdate,
tstate,
tigx,
tclosed,
tcompleted,
CASE
WHEN ( tclosed IS NOT NULL AND tcompleted IS NOT NULL AND tigx IS NOT NULL ) THEN
ROUND( AVG( DATEDIFF( tclosed, tdate )), 1 )
WHEN ( tclosed IS NULL AND tcompleted IS NULL AND tigx IS NULL ) THEN
ROUND( AVG( DATEDIFF( CURDATE(), tdate )), 1 ) ELSE ROUND(
AVG(
DATEDIFF( COALESCE ( tclosed, tcompleted, tigx ), tdate )),
1
END AS `avg`
FROM
`tbl_c`
WHERE
NOT ( `tkt` LIKE '%I%' OR `tkt` LIKE '%L%' AND tstate IN ( 'Closed' ) AND tigx IS NULL AND tclosed IS NULL AND tcompleted IS NULL )
GROUP BY
tarea
ORDER BY
tdate DESC;
The important thing here is that operators donāt have the same precedence.
Like * and + donāt have the same precedence, AND and OR donāt either.
X OR Y AND Z
is read by the engine as X OR (Y AND Z), because AND has a higher precedence than OR.
Now consider how that interacts with the NOT as well.
What your WHERE condition reads as:
Find the rows where tkt is LIKE %L% AND tstate IN ( āClosedā ) AND tigx IS NULL AND tclosed IS NULL AND tcompleted IS NULL
Find the rows where tkt is LIKE %I%.
Union those together.