计算从一个状态到另一个状态所花费的天数:SQL

时间:2014-11-06 09:50:19

标签: sql sql-server sql-server-2008

请大家注意我们数据库的当前结构。

enter image description here

我们的DBA目前正在接下来的两周内,我的SQL知识非常有限,我喜欢使用UI和中间层。

我们想要弄清楚的是我们如何才能做到以下几点,我们需要编写一个查询来计算所有佣金从“已验证”过渡到“付费”的平均时间(以天为单位)经销商,目前的状态是

  1. 创建
  2. 验证
  3. 拒绝
  4. 等待付款
  5. 付费
  6. 退款
  7. 我认为这个查询需要直接针对佣金历史表?

    由于我对SQL的了解有限,我不确定如何编写此类查询...

    任何帮助都会很棒。

2 个答案:

答案 0 :(得分:1)

这是一种实现您所追求目标的方法,尽管它可能不是最有效的方法。在我看来,它更像是一个你想要运行的一次性查询,而不是你要经常运行以影响数据库性能的东西。

测试表设置:

CREATE TABLE Commission
(
    CommissionId INT,
    DealerId INT
)

CREATE TABLE CommissionHistory
(
    CommissionId INT,
    ActionDate DATETIME,
    NewPaymentStatusId INT
)

插入虚拟数据 - 1个经销商的5个佣金:

INSERT INTO dbo.Commission
        ( CommissionId ,
          DealerId
        )
VALUES ( 1 , 1 ),
       ( 2 , 1 ),
       ( 3 , 1 ),
       ( 4 , 1 ),
       ( 5 , 1 ),

INSERT INTO dbo.CommissionHistory
        ( CommissionId ,
          ActionDate ,
          NewPaymentStatusId
        )
VALUES ( 1 , GETDATE() -25, 1 ),
       ( 1 , GETDATE() -21, 2 ),
       ( 1 , GETDATE() -18, 3 ),
       ( 1 , GETDATE() -16, 4 ),
       ( 1 , GETDATE() -5, 5 ),
       ( 2 , GETDATE() -10, 1 ),
       ( 2 , GETDATE() -9, 2 ),
       ( 2 , GETDATE() -8, 3 ),
       ( 2 , GETDATE() -7, 4 ),
       ( 2 , GETDATE() -6, 5 ),
       ( 3 , GETDATE() -10, 1 ),
       ( 3 , GETDATE() -8, 2 ),
       ( 3 , GETDATE() -6, 3 ),
       ( 3 , GETDATE() -4, 4 ),
       ( 3 , GETDATE() -2, 5 ),
       ( 3 , GETDATE() -25, 6 ),
       ( 4 , GETDATE() -10, 1 ),
       ( 4 , GETDATE() -7, 2 ),
       ( 4 , GETDATE() -6, 3 ),
       ( 4 , GETDATE() -4, 4 ),
       ( 4 , GETDATE() -1, 5 ),
       ( 5 , GETDATE() -1, 1 ),
       ( 5 , GETDATE() -1, 2 )

因此,对于虚拟数据,佣金1,2和4被归类为有效记录,因为它们具有状态2和5. 3被排除,因为它被退还,5被排除,因为它没有被支付。

为了生成平均值,我写了以下查询:

-- set the required dealer id
DECLARE @DealerId INT = 1

-- return all CommissionId's in to a temp table that have statuses 2 and 5, but not 6
SELECT DISTINCT CommissionId
INTO #DealerCommissions
FROM dbo.CommissionHistory t1      
WHERE CommissionId IN (SELECT CommissionId 
                       FROM dbo.Commission 
                       WHERE    DealerId = @DealerId)
AND NOT EXISTS (SELECT CommissionId 
                FROM dbo.CommissionHistory t2 
                WHERE t2.NewPaymentStatusId = 6 AND t2.CommissionId = t1.CommissionId)
AND EXISTS (SELECT CommissionId 
            FROM dbo.CommissionHistory t2 
            WHERE t2.NewPaymentStatusId = 2 AND t2.CommissionId = t1.CommissionId)
AND EXISTS (SELECT CommissionId 
            FROM dbo.CommissionHistory t2 
            WHERE t2.NewPaymentStatusId = 5 AND t2.CommissionId = t1.CommissionId)

-- use the temp table to return average difference between the MIN & MAX date
;WITH cte AS (
    SELECT CommissionId FROM #DealerCommissions
)
SELECT  AVG(CAST(DaysToCompletion AS DECIMAL(10,8)))
FROM    (
         SELECT DATEDIFF(DAY, MIN(ch.ActionDate), MAX(ch.ActionDate)) DaysToCompletion
         FROM cte
         INNER JOIN dbo.CommissionHistory ch ON ch.CommissionId = cte.CommissionId
         GROUP BY ch.CommissionId
) AS averageDays

-- remove temp table
DROP TABLE #DealerCommissions

答案 1 :(得分:0)

对于历史记录表中的每个佣金,您可以获得最长验证日期和最低付款日期,假设付款日期总是晚于验证日期。然后,您可以将佣金表加入经销商ID分组,以获得平均持续时间。

with comm as(
select 
    commissionid,
    max(case NewPamentStatus when 'Verified' then ActionDate else null end) as verified_date,  
    min(case NewPamentStatus when 'Paid' then ActionDate else null end) as paid_date
        --using max or min just incase that same status will be recorded more than one time.
from 
    CommissionHistory
group by 
    commistionid
)
select 
    c.DealerId,
    avg(datediff(day,comm.verified_date,comm.paid_date))
from
    comm
inner join
    commission c
on  c.commissionid = comm.commissionid
where 
    datediff(day,comm.verified_date,comm.paid_date)>0
    -- to get rid off the commissions with paid date before the verified date or in same day
group by
    c.DealerId