Sql server SQL Server查询上次值和平均值

Sql server SQL Server查询上次值和平均值,sql-server,Sql Server,我需要为一个组(行id=1)获取“最后一个值”,为另一个组(行id=2)获取“平均值” 我正在尝试在SQLServer中编写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

我需要为一个组(行id=1)获取“最后一个值”,为另一个组(行id=2)获取“平均值”

我正在尝试在SQLServer中编写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 
     [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];

完整脚本:

你能不能至少正确地编排你的问题格式,解释一下你尝试过什么,还有什么不起作用?在标题中加上***紧急***只会让你的帖子被忽略(你不应该依靠问答网站上的陌生人来挽救你的脖子)。如果问题紧急,在问题文本中写一个合适的标题和解释。否则,当人们开始询问你的意思以便他们能够提供帮助时,你将浪费大量时间。如果是“紧急情况”,请花时间以可消费的格式提供样本数据,并发布你尝试过的内容;您可能会更快得到答案:。你肯定会有更多用户的问题,你会更乐意回答,因为我们不会将数据转换成可用的格式(尽管至少它不是图像)。@Rich不得不说,你成就了我的一天。
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];
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