-1

I am using MySQL 5.7.38 version. I have the below query which is returning the correct sample data as below:

SELECT
   t.r_id,
   t.hostname,
   t.a_tmstamp,
   max(t.r_status) as r_status,
   max(t.message) as message,
   max(t.m_id) as m_id
FROM (
SELECT
   audit.r_id as r_id,
   nhost.host_name as hostname,
   meta.r_status as r_status,   
   meta.step as step,
   meta.id as m_id,
   meta.message as message,
   audit.a_timestamp as a_tmstamp,
   npr.nas_provider as nas_provider             
FROM audit
   INNER JOIN npr ON npr.nr_id = audit.r_id
   AND audit.a_timestamp BETWEEN now() - interval 30 DAY AND now()
   INNER JOIN nhost ON audit.r_id = nhost.nr_id      
   INNER JOIN meta ON audit.audit_m_id = meta.id                                  
   INNER JOIN nprw ON npr.pw_id = nprw.id   
   AND nprw.ap_step = meta.step                
   WHERE meta.r_status regexp 'FAIL'
   ORDER BY  a_timestamp DESC
     ) AS t 
 GROUP BY t.r_id, t.hostname, t.a_tmstamp
 ORDER BY a_tmstamp DESC;

Sample Data:

+-----------+----------+-----------------------+--------------+------------+---------+
| r_id      | hostname | a_tmstamp             |  r_status    |  message   |  m_id   |
+-----------+----------------------------------+--------------+------------+---------+
| a1ffadc9  | abc01    | 6/9/2022  6:15:17 PM  | PROGRESS     |  message1  | d759df3 |
| a1ffadc9  | abc02    | 6/9/2022  6:15:17 PM  | PROGRESS     |  message2  | d759df3 |
| a1ffadc9  | abc01    | 6/9/2022  6:14:17 PM  | PROGRESS     |  null      | fdb2f19 |
| a1ffadc9  | abc02    | 6/9/2022  6:14:17 PM  | PROGRESS     |  null      | fdb2f19 |
| 246cc315  | cad01    | 6/9/2022  4:43:52 PM  | FAILED       |  message3  | f5435f0 |
| 246cc315  | cad02    | 6/9/2022  4:43:52 PM  | FAILED       |  message4  | f5435f0 |
| 258e55ac  | cad01    | 6/9/2022  4:42:52 PM  | FAILED       |  reason1   | 432ca62 |
| 258e55ac  | cad02    | 6/9/2022  4:42:52 PM  | FAILED       |  reason2   | 432ca62 |
+-----------+----------+-----------------------+--------------+------------+---------+

How can I perform group by on max(t.a_tmstamp) so that I get below output?

Desired Output:

+-----------+----------+-----------------------+--------------+------------+---------+
| r_id      | hostname | a_tmstamp             |  r_status    |  message   |  m_id   |
+-----------+----------------------------------+--------------+------------+---------+
| a1ffadc9  | abc01    | 6/9/2022  6:15:17 PM  | PROGRESS     |  message1  | d759df3 |
| a1ffadc9  | abc02    | 6/9/2022  6:15:17 PM  | PROGRESS     |  message2  | d759df3 |
| 246cc315  | cad01    | 6/9/2022  4:43:52 PM  | FAILED       |  message3  | f5435f0 |
| 246cc315  | cad02    | 6/9/2022  4:43:52 PM  | FAILED       |  message4  | f5435f0 |
+-----------+----------+-----------------------+--------------+------------+---------+
meallhour
  • 13,921
  • 21
  • 60
  • 117

1 Answers1

2

You can employ some trickery to get the status/message/m_id for the latest a_tmpstamp:

SELECT
   t.r_id,
   t.hostname,
   max(t.a_tmstamp) max_tmstamp,
   substr(max(concat(t.a_tmstamp,t.r_status)),24) as r_status,
   substr(max(concat(t.a_tmstamp,t.message)),24) as message,
   substr(max(concat(t.a_tmstamp,t.m_id)),24) as m_id
FROM (
SELECT
   audit.r_id as r_id,
   nhost.host_name as hostname,
   meta.r_status as r_status,   
   meta.step as step,
   meta.id as m_id,
   meta.message as message,
   audit.a_timestamp as a_tmstamp,
   npr.nas_provider as nas_provider             
FROM audit
   INNER JOIN npr ON npr.nr_id = audit.r_id
   AND audit.a_timestamp BETWEEN now() - interval 30 DAY AND now()
   INNER JOIN nhost ON audit.r_id = nhost.nr_id      
   INNER JOIN meta ON audit.audit_m_id = meta.id                                  
   INNER JOIN nprw ON npr.pw_id = nprw.id   
   AND nprw.ap_step = meta.step                
   WHERE meta.r_status regexp 'FAIL'
   ORDER BY  a_timestamp DESC
     ) AS t 
 GROUP BY t.r_id, t.hostname
 ORDER BY max_tmstamp DESC;
ysth
  • 96,171
  • 6
  • 121
  • 214
  • thanks a lot. what do you think about using `substring_index(group_concat(t.message order by t.a_tmstamp desc), ',', 1) as message,`? – meallhour Jun 10 '22 at 01:59
  • 1
    doesn't work if message contains a comma or is longer than the group_concat_max_len setting (and gives a warning if it is too long); the timestamp is a fixed length, easier just to concat and then remove it. I guess if you might change the type of the timestamp, you could use `length(max(t.a_tmstamp))+1` instead of 24 – ysth Jun 10 '22 at 05:11