sql - 如何合并结果中的两行而不是所有结果?

标签 sql sql-server database sql-server-2008 tsql

我有以下查询:

-- Compare current period to historical data
select  Name ,
        avg(TimeProcessing + TimeRendering + TimeDataRetrieval) / 1000  as 'Current Month' ,
        isnull(count(TimeProcessing), 0)                                as 'Sample' ,
        min(l2.[Avg_Exec_Time_Previous_Month])                          as 'Previous Month' ,
        isnull(min(l2.[Executions_Last_Month]), 0)                      as 'Sample' ,
        min(l3.[Avg_Exec_Time_Two_Months_Ago])                          as 'Two Months ago' ,
        isnull(min(l3.[Executions_Two_Months_Ago]), 0)                  as 'Sample'
from    marlin.report_execution_log l
        inner join marlin.report_catalog c on l.ReportID = c.ItemID
        left outer join ( 
                        select    
                            l2.ReportID ,
                            (
                            avg(l2.TimeProcessing + l2.TimeRendering 
                            + l2.TimeDataRetrieval) / 1000 
                            ) as 'Avg_Exec_Time_Previous_Month' ,
                            count(l2.TimeProcessing) as 'Executions_Last_Month'
                        from    
                            marlin.report_execution_log l2
                        where   
                            TimeEnd between dateadd(MONTH, -2, getdate())
                                    and     dateadd(MONTH, -1, getdate())
                        group by  
                            l2.ReportID
                        ) l2 on l.ReportID = l2.ReportID
        left outer join ( 
                        select    
                            l3.ReportID ,
                            (
                            avg(l3.TimeProcessing + l3.TimeRendering + l3.TimeDataRetrieval) / 1000 
                            ) as 'Avg_Exec_Time_Two_Months_Ago' ,
                            count(l3.TimeProcessing) as 'Executions_Two_Months_Ago'
                        from  
                            marlin.report_execution_log l3
                        where 
                            TimeEnd between dateadd(MONTH, -3, getdate())
                                    and     dateadd(MONTH, -2, getdate())
                        group by  
                            l3.ReportID
                        ) l3 on l.ReportID = l3.ReportID
group by    l.ReportID ,
            Name
order by    2 desc

这带来了以下结果:

Results

不幸的是,我们的一份报告在整个月内更改了名称,随后我需要合并这两行。这可能吗?如何合并两行?例如,如何使用第一行报告名称让第一行和第二行显示附加结果?

最佳答案

如果我理解得很好,你只需要在你的选择和你的组中有一个案例陈述......就像

select  case when Name = 'Project1' then 'Project1'
             when Name = 'Project2' then 'Project1'
             else Name
        end as NAME
.......
group by case when Name = 'Project1' then 'Project1'
             when Name = 'Project2' then 'Project1'
             else Name
        end

如果你的case是现在是Project 1,一个月前是Project 2,你可能需要在case statement中加上日期(以防万一)

 select  case when Name = 'Project1' and TimeEnd = getdate()  then 'Project1'
                 when Name = 'Project2' and TimeEnd = dateadd(MONTH, -1, getdate()) then 'Project1'
                 else Name
            end as NAME
    .......
    group by case when Name = 'Project1' and TimeEnd = getdate()  then 'Project1'
                 when Name = 'Project2' and  TimeEnd = dateadd(MONTH, -1, getdate()) then 'Project1'
                 else Name
        end

就是这个想法。

编辑。

我认为如果他们重复出现,您可以选择,但我一点都不喜欢

SELECT NAME, AVG(Current Month) as Current Month, count(Sample) as Sample, min(Previous Month) as Previous Month, min(Sample2) as Sample2, min(Two Months ago) as Two Months ago,
min(Sample3) as Sample3
FROM
(
select  Name ,
        avg(TimeProcessing + TimeRendering + TimeDataRetrieval) / 1000  as 'Current Month' ,
        isnull(count(TimeProcessing), 0)                                as 'Sample' ,
        min(l2.[Avg_Exec_Time_Previous_Month])                          as 'Previous Month' ,
        isnull(min(l2.[Executions_Last_Month]), 0)                      as 'Sample2' ,
        min(l3.[Avg_Exec_Time_Two_Months_Ago])                          as 'Two Months ago' ,
        isnull(min(l3.[Executions_Two_Months_Ago]), 0)                  as 'Sample3'
from    marlin.report_execution_log l
        inner join marlin.report_catalog c on l.ReportID = c.ItemID
        left outer join ( 
                        select    
                            l2.ReportID ,
                            (
                            avg(l2.TimeProcessing + l2.TimeRendering 
                            + l2.TimeDataRetrieval) / 1000 
                            ) as 'Avg_Exec_Time_Previous_Month' ,
                            count(l2.TimeProcessing) as 'Executions_Last_Month'
                        from    
                            marlin.report_execution_log l2
                        where   
                            TimeEnd between dateadd(MONTH, -2, getdate())
                                    and     dateadd(MONTH, -1, getdate())
                        group by  
                            l2.ReportID
                        ) l2 on l.ReportID = l2.ReportID
        left outer join ( 
                        select    
                            l3.ReportID ,
                            (
                            avg(l3.TimeProcessing + l3.TimeRendering + l3.TimeDataRetrieval) / 1000 
                            ) as 'Avg_Exec_Time_Two_Months_Ago' ,
                            count(l3.TimeProcessing) as 'Executions_Two_Months_Ago'
                        from  
                            marlin.report_execution_log l3
                        where 
                            TimeEnd between dateadd(MONTH, -3, getdate())
                                    and     dateadd(MONTH, -2, getdate())
                        group by  
                            l3.ReportID
                        ) l3 on l.ReportID = l3.ReportID
group by    l.ReportID ,
            Name
)
group by  Name
order by    2 desc

关于sql - 如何合并结果中的两行而不是所有结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8844474/

相关文章:

php - 我该如何解决这个错误?弃用 : mysql_escape_string(): This function is deprecated; use mysql_real_escape_string() instead

sql - 使用 Rails 和 SQL 在数据库中保存多条温度曲线(同时测量)的最佳方法是什么

sql - MySQL 搜索查询

c# - Reader 没有读取 DB 值

SQL Server : error creating table with multiple primary keys

android - 在数据库更改 Android 时得到通知

database - 停止来自 Progress data server for oracle 的索引相关提示和条款

php - 在不使用某些函数的情况下组合两个查询

sql - 为什么不显式 COLLATE 覆盖数据库排序规则?

database - 建模多个多对多关系的最自然方式