通过比较SQL Server中的两个行来计算列之间的平均值[英] Calculate Average between columns by comparing two rows in SQL Server

本文是小编为大家收集整理的关于通过比较SQL Server中的两个行来计算列之间的平均值的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有下表

BidID AppID AppStatus   StatusTime
    1   1   In Review   2019-01-02 12:00:00
    1   1   Approved    2019-01-02 13:00:00
    1   2   In Review   2019-01-04 13:00:00
    1   2   Approved    2019-01-04 14:00:00
    2   2   In Review   2019-01-07 15:00:00
    2   2   Approved    2019-01-07 17:00:00
    3   1   In Review   2019-01-09 13:00:00
    4   1   Approved    2019-01-09 13:00:00

我要做的是首先通过以下逻辑来计算刻度分钟的平均值

BIDID的第一组,然后通过Appid进行计算,然后计算审查和批准的AppStatus中的statustime

之间的时间差
eg
First Group BidID,Then group App ID

,然后首先检查评论状态并找到下一个批准的状态,然后必须计算日期之间的最小差异

BidID  AppID    AppStatus                                          BidAverage
1 ->   1,2  ->  For App ID 1(2019-01-02       1hour                  1.5
                15:48:42.000 - 2019-01-02 
                12:33:36.000)
                For App ID 2(2019-01-04       2hour
                10:33:12.000 - 2019-01-04 
                10:33:12.000)
2->    2  ->    For App ID 2(2019-01-04       1                       1
                10:33:12.000 - 2019-01-04 
                10:33:12.000)
3->     1->     No Calculation since no Approved           

4->     1->    No Calculation since no In Review before Approved

Final Average (1.5 + 1) / 2 = 1.25 for the table

不包括星期六的时间差,我已经弄清楚时间差异周末使用大卫的建议建议.

我不确定如何检查AppStatus是否首先在审核中进行批准,然后才能计算时间差,以及是否像Bidid 3中没有批准的时间差,然后在平均计算中不使用它,然后平均使用它穿过appid,然后是Bidid

谢谢

推荐答案

我认为您可以只使用min()和max(),以便简单地获得BID/APP对的时间.其余的只是聚集和更多的聚合.

您描述的处理似乎是:

select avg(avg_bid_diff)
from (select bid, avg(diff*1.0) as avg_bid_diff
      from (select bid, appid,
                   datediff(second, min(starttime), max(statustime)) as diff
            from t
            where appstatus in ('In Review', 'Approved')
            group by bid, appid
            having count(*) = 2
           ) ba
      group by bid
     ) b;

这做出了与所提供的数据一致的假设 - 状态对投标/应用程序对没有重复项,批准始终是在审查后.

本文地址:https://itbaoku.cn/post/2786481.html

问题描述

I have the below table

BidID AppID AppStatus   StatusTime
    1   1   In Review   2019-01-02 12:00:00
    1   1   Approved    2019-01-02 13:00:00
    1   2   In Review   2019-01-04 13:00:00
    1   2   Approved    2019-01-04 14:00:00
    2   2   In Review   2019-01-07 15:00:00
    2   2   Approved    2019-01-07 17:00:00
    3   1   In Review   2019-01-09 13:00:00
    4   1   Approved    2019-01-09 13:00:00

What I am trying to do is first to calculate the average of statusTime minutes difference by the following logic

First group by the BidID and then by AppID and then calculate the time difference between the StatusTime between In Review and Approved AppStatus

eg
First Group BidID,Then group App ID

, Then First Check for In Review Status and Find the Next Approved status and then have to calculate min difference between the dates

BidID  AppID    AppStatus                                          BidAverage
1 ->   1,2  ->  For App ID 1(2019-01-02       1hour                  1.5
                15:48:42.000 - 2019-01-02 
                12:33:36.000)
                For App ID 2(2019-01-04       2hour
                10:33:12.000 - 2019-01-04 
                10:33:12.000)
2->    2  ->    For App ID 2(2019-01-04       1                       1
                10:33:12.000 - 2019-01-04 
                10:33:12.000)
3->     1->     No Calculation since no Approved           

4->     1->    No Calculation since no In Review before Approved

Final Average (1.5 + 1) / 2 = 1.25 for the table

The time difference excluding saturday I have already figured out Time Dfference Exluding Weekend using David's suggestion.

I am not sure how to check if AppStatus is first in In Review and then Approved and then only calculate the time difference and if there is no Approved like in BidID 3 then don't use that in the average calculation and then average it across the APPId and then the BidID

Thanks

推荐答案

I think you can just use min() and max() for simplicity to get the times for the bid/app pairs. The rest is just aggregation and more aggregation.

The processing you describe seems to be:

select avg(avg_bid_diff)
from (select bid, avg(diff*1.0) as avg_bid_diff
      from (select bid, appid,
                   datediff(second, min(starttime), max(statustime)) as diff
            from t
            where appstatus in ('In Review', 'Approved')
            group by bid, appid
            having count(*) = 2
           ) ba
      group by bid
     ) b;

This makes assumptions that are consistent with the provided data -- that the statuses don't have duplicates for the bid/app pairs an that approval is always after review.