Sql-server – Adding two more columns to existing script (backup summary)

sql serversql server 2014t-sql

enter image description hereCan anyone help me to add two more columns to the existing code? I am not a core developer. I want backup_size in GBs and name from msdb.dbo.backupset table, where msdb.dbo.backupset.name = **'XXXXXXX'**. I need this because we are using a third party tool for backups. That tool name is reflecting in msdb.dbo.backupset name column, so I want to filter by that name. Please see code below.

SELECT name,
       recovery_model_desc,
       state_desc,
       d AS 'Last Full Backup',
       i AS 'Last Differential Backup',
       l AS 'Last log Backup'
 FROM (SELECT db.name,
              db.state_desc,
              db.recovery_model_desc,
              type,
              backup_finish_date
        FROM   master.sys.databases db
         LEFT OUTER JOIN msdb.dbo.backupset a ON a.database_name = db.name
       ) AS Sourcetable 
 PIVOT (MAX(backup_finish_date) FOR type IN ( D, I, L )) AS MostRecentBackup

Best Answer

SELECT name , recovery_model_desc , state_desc , d AS 'Last Full Backup' , i AS 'Last Differential Backup' , l AS 'Last log Backup' , backupname
, Cast(backupsizeGB AS DECIMAL(10, 2)) backupsizeGB
FROM 
( SELECT db.name , db.state_desc , db.recovery_model_desc , a.type , LastBackupFinishedDate, b.name backupname, (b.backup_size /1024 / 1024 /1024) as backupsizeGB
FROM master.sys.databases db 
LEFT OUTER JOIN 
(select database_name dBase, type, max(backup_finish_date) LastBackupFinishedDate From msdb.dbo.backupset group By database_name, type)
 a ON a.dBase = db.name 
    Left outer join msdb.dbo.backupset b on b.database_name=a.dBase and backup_finish_date= LastBackupFinishedDate
) AS Sourcetable 
PIVOT ( MAX(LastBackupFinishedDate) FOR type IN ( D, I, L ) ) AS MostRecentBackup

This cleaned it up for me.
Can't do a true pivot (Placing the dates of each on one line) because the filenames and sizes prevent it.