Home > Blockchain >  Display one entry of each type SQL Query
Display one entry of each type SQL Query

Time:07-07

I have to get data from a table mentioned below. As you can see there are two types of Entry Type: complete data backup and log backup. I need to query only one entry of the latest success and error of either of them according to timestamp.

Table picture

So the output I need would be:

Success   complete data backup   2022-07-11 15:27:10(this is the latest entry of this type.)
Error     complete data backup   2022-07-06 15:28:37(this is the latest entry of this type.)
Success   log backup             2022-07-29 15:27:16(this is the latest entry of this type.)
Error     log backup             2022-07-28 15:28:29(this is the latest entry of this type.)

So far Ive tried this

"SELECT ENTRY_TYPE_NAME, Status, TimeStamp from M_BACKUP_CATALOG where Status='Success' OR Status='Error' order by TimeStamp DESC"

But it displays all entries in descending order. wheras i only want one success and one error of the two entry_type_name (complete data backup, and log backup)that i have in my table.

CodePudding user response:

You will have to use the group by combined with Max function.

SELECT ENTRY_TYPE_NAME, Status, MAX(TimeStamp) as TimeStamp from M_BACKUP_CATALOG Group by ENTRY_TYPE_NAME, Status
  •  Tags:  
  • sql
  • Related