Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT Statement Combining Multiple Rows

Tags:

sql

I am currently working with an audit log table similar to:

ID  |  EventTime           |  AccessID  |  AccessType  |  Status  |  Final
------------------------------------------------------------------------------
1   | 2013/02/10 01:01:01  |  NULL      |  123         |  335     |   OK
2   | 2013/02/10 01:01:01  |  985521    |  NULL        |  66      |   OK
....
41  | 2013/02/10 07:07:07  |  NULL      |  456         |  335     |   OK
42  | 2013/02/10 07:07:07  |  113228    |  NULL        |  66      |   OK

What I need to select is the AccessID and AccessType into one row, i.e.:

AccessID  |  AccessType
------------------------
985521    |  123
113228    |  456

The AccessID is always unique and are only available in the row where Status = 66. The AccessType is only available where the Status = 335.

The EventTime is always shared for status 335 and 66.

I have tried grouping the data by event time, using sub selects etc but haven't quite managed to get the end result I need.

Additional Info

There are approximately 100000 rows a day added. For each EventTime there are approximately 40 rows all with different information (there are more columns than the example provided below)

like image 502
lethalMango Avatar asked Jan 13 '23 23:01

lethalMango


1 Answers

Try this way:

select max(AccessID) as AccessID,  max(AccessType) as AccessType
from audit_log 
where Status in (335,66)
group by EventTime
like image 79
Robert Avatar answered Jan 18 '23 13:01

Robert