通过返回额外的行进行分组

问题描述 投票:0回答:1

我有一个观点,返回 6 列

Select DataSourceName,StartedDateTime, StoppedDateTime , 
    LastStateName, ErrorString , ProtectionGroup 
from [dbo].[vw_RPT_BackupJobsSummaryLast24Hours]

数据返回方式如下

但我想要一个额外的行(在屏幕截图下方)按 DataSourceName 分组并基于 LastStateName(成功或失败)。 如何实现?

我尝试了以下方法,但我需要一些帮助。

select DataSourceName, StartedDateTime, StoppedDateTime , LastStateName, ErrorString
from
(
    select DataSourceName, StartedDateTime, StoppedDateTime, LastStateName, ErrorString
    from [dbo].[vw_RPT_BackupJobsSummaryLast24Hours]
    union
    select DataSourceName, 'Over All Status', case when LastStateName ='Success' then 'Success' else 'Failure' end as Value, '', ''
    from [dbo].[vw_RPT_BackupJobsSummaryLast24Hours]
    group by DataSourceName
) a
sql sql-server
1个回答
0
投票

我已经这样解决了

select DataSourceName, StartedDateTime, StoppedDateTime , LastStateName, ErrorString
from
(
    select DataSourceName, StartedDateTime, StoppedDateTime, LastStateName, ErrorString
    from [dbo].[vw_RPT_BackupJobsSummaryLast24Hours]
    union
    select DataSourceName, 'Over All Status', case when LastStateName ='Success' then 'Success' else 'Failure' end as Value, case when LastStateName ='Success' then 'Success' else 'Failure' end as Value, ''
    from [dbo].[vw_RPT_BackupJobsSummaryLast24Hours]
    group by DataSourceName, LastStateName
) a
© www.soinside.com 2019 - 2024. All rights reserved.