SQL Server查询最后和平均值

时间:2018-04-27 11:48:08

标签: sql-server

我要求获得一个组的“最后一个值”(Row id = 1)和另一个组的“average value”(Row id = 2)。

我正在尝试在SQL Server中编写SQL查询。

以下是数据集和预期输出。

Name         ROWID  Type    Company   Date      KPI
Activity Plan   1   Low       MS    1/01/2018   10
Activity Plan   1   Low       MS    1/02/2018   20
Activity Plan   1   Low       MS    1/03/2018   30
Activity Plan   1   Low       MS    1/04/2018   NULL
Activity Plan   1   Low       MS    1/05/2018   NULL
Activity Plan   1   Low       MS    1/06/2018   NULL
Activity Plan   1   Low       MS    1/07/2018   NULL
Appointment     2   High      MS    1/01/2018   15
Appointment     2   High      MS    1/02/2018   25
Appointment     2   High      MS    1/03/2018   25
Appointment     2   High      MS    1/04/2018   NULL
Appointment     2   High      MS    1/05/2018   NULL
Appointment     2   High      MS    1/06/2018   NULL
Appointment     2   High      MS    1/07/2018   NULL

预期产出

 Name         ROWID   Type  Company   Date     KPI   Value  
Activity Plan   1     Low    MS     1/01/2018   10   10 
Activity Plan   1     Low    MS     1/02/2018   20   20 
Activity Plan   1     Low    MS     1/03/2018   30   30 
Activity Plan   1     Low    MS     1/04/2018  NULL  30   last value
Activity Plan   1     Low    MS     1/05/2018  NULL  30   Last Value
Activity Plan   1     Low    MS     1/06/2018  NULL  30   Last Value
Activity Plan   1     Low    MS     1/07/2018  NULL  30   Last Value
Appointment     2     High   MS     1/01/2018   12   12 
Appointment     2     High   MS     1/02/2018   35   35 
Appointment     2     High   MS     1/03/2018   44   44 
Appointment     2     High   MS     1/04/2018   NULL 30.33  AVG Value
Appointment     2     High   MS     1/05/2018   NULL 30.33  AVG Value
Appointment     2     High   MS     1/06/2018   NULL 30.33  AVG Value
Appointment     2     High   MS     1/07/2018   NULL 30.33  AVG Value

我的尝试:

*

select    f.name
           f.rowid,
           f.type,
           f.company,
           f.date,
           f.KPI,
           case when name ='Appointment' then avg( f2.KPI) 
          else max(f.KPI) OVER (PARTITION BY f.rowid order by f,date) end as result
   from table f 
inner join 
       table f2
 on f.rowid = f2.opwid
group by 
           f.name
           f.rowid,
           f.type,
           f.company,
           f.date,
           f.KPI

*

2 个答案:

答案 0 :(得分:0)

请用此来达到你的结果:

select 
     [name]     =   [name]      
    ,[rowid]    =   [rowid] 
    ,[type]     =   [type]          
    ,[company]  =   [company]     
    ,[date]     =   [date]      
    ,[kpi]      =   [kpi]   
    ,[value]    =   iif([rowid] = 1, isnull([kpi], [lvalue].[value]), isnull([kpi], [avgvalue].[value]))    
from
    #urgent as  [u]
outer apply
    (
        select top 1 [value] = [kpi] from #urgent where [rowid] = 1 and [kpi] is not null order by [date] desc 
    ) as [lvalue]
outer apply
    (
        select [value] = avg([kpi]) from #urgent where [rowid] = 2  and [kpi] is not null
    ) as [avgvalue];

完整脚本:https://pastebin.com/fJQB6wyP

答案 1 :(得分:0)

with cte as 
(    select Name, ROWID, Type, Company, Date, KPI 
          , max(kpi) over (partition by ROWID) as max 
          , avg(kpi) over (partition by ROWID) as avg
     from urgent  
)
select Name, ROWID, Type, Company, Date, KPI, isnull(KPI, max) as Value 
from cte 
where ROWID = 1 
union all
select Name, ROWID, Type, Company, Date, KPI, isnull(KPI, avg) as Value 
from cte 
where ROWID = 2