交易数据汇总

时间:2018-10-29 20:27:26

标签: sql-server

作为免责声明,我不能完全确定问题的标题是否是最好的,如果没有的话我会道歉。

我正在尝试计算个人的周期时间,但是偶尔会将文件从他们的工作队列中移出,并最终移回。没有唯一的交易ID,仅记录了日期和时间戳。

我尝试按功能查找聚合组,并被告知这不是sql-server具有的功能。

我首先尝试确定第一个和最后一个事务,然后打算从那里建立查询,但这并不太有用。任何见解都将非常有帮助。

更改日期是记录从一个人到另一个人的转移(年份,蛾子,白天)的时间

select a.claimId,
       a.claimincidentID,
       cast(a.changeDate as date) changedate,
       a.claimNum,
       a.Coverage,
       a.AssignedAdjID, 
       a.AssignedAdj,
       a.AssignedUnit,
       a.TransferedAdjID,
       a.TransferedAdj,
       a.TransferedUnit,
       a.usertypeid,
       a.ChangedBy,
       b.Feature_Create_Date,
       DATEDIFF(day, b.Feature_Create_Date, a.changedate) transfer1,
       cast(FIRST_VALUE(changeDate) OVER (ORDER BY changedate ASC)as date) AS firstchangedate,
       cast(LAST_VALUE(changeDate) OVER (ORDER BY a.changedate ASC)as date) AS lastchangedate 
from DB1.dbo.Assign_Transfer a
left join DB2.claimslist b on a.claimid=b.claimId 
group by a.claimId, a.claimincidentID, a.changeDate, a.claimNum, a.Coverage, a.AssignedAdjID, a.AssignedAdj, a.AssignedUnit, a.TransferedAdjID, a.TransferedAdj, a.TransferedUnit, a.usertypeid, a.ChangedBy, b.Feature_Create_Date

2 个答案:

答案 0 :(得分:0)

Think of each of these rows as a Start (because the most recent one hasn't ended)
We would need to generate the complement End for this person in the chain.
Then with pairs of Start/End one could create GrossDuration.

Even after we get an assignment's start and end date/time,
we will have workday (8-4, or 9-5, or noon-8, ...) considerations, 
also Sat/Sun/Hol and Vacation/out-of-office.  
All of which affect Duration--- For Each Person differently.
Which would need to be factored by workday/etc into AdjDuration.

Lets say we can sequence these 
 Row_Number() Over (Partition by claimID Order by changeDate) as tfrNum
Assigned is the prior, and Transfered is the next
1, 2, 3, ... thru  N
                   V
             a.changeDate                 -- NOW()
                   V                          V
a.AssignedAdjID,   |  a.TransferedAdjID,
a.AssignedAdj,     |  a.TransferedAdj,
a.AssignedUnit,    |  a.TransferedUnit,
                   |
             a.usertypeid,
             a.ChangedBy,

So, is tfrNum=1 or tfrNum=N the oddball??
Lets look at pairs: each pair goes StartFrom->EndTo
1-2, 2-3, 3-4, 4-5, 5-6, 6-Now

----
From row1 we get TransferredID Start(changeDate) and 
from row2 we get AssignedAdjID End  (changeDate)
-- 2-3, 3-4, 4-5, etc repeating
--except for
From row6 we get TransferredID Start(changeDate) and
from default  (still them)  End  (Now)
-- -- except again when TransferredUnit is "Closed"

获得这些对以及它们的开始和结束之后,我们可以进行工期计算。

在尝试运行某些sql之前,我需要形象化此问题。真实数据会有所帮助。

让我们从这里开始,稍后在它开始工作并查看一些数据之后,我将对其进行扩展-

With cte_tfrNum (claimID, changeDate, tfrNum, tfrMax) AS
(
    SELECT
         a.claimId
        ,a.changeDate
        ,ROW_NUMBER() Over ( Partition By a.claimId Order By a.changeDate) as tfrNum
        ,b.tfrMax
    FROM  DB1.dbo.Assign_Transfer a
    -- just for giggles, lets also get the max# of transfers for this claim
    Left Join 
          (SELECT claimId, COUNT(*) as tfrMax
           FROM  DB1.dbo.Assign_Transfer 
           Group By claimId
           ) as b
    On b.claimId = a.claimId
)

-- Statement using the CTE  
Select
    tfrTo.*
From cte_tfrNum as tfrTo

答案 1 :(得分:0)

谢谢!我能够接受您给我的内容,并添加一些内容以查看我的需求。

select 
case when abc.tfrMax > abc.tfrnum then datediff(day,lag(abc.changedate) over(partition by abc.claimID order by abc.claimId),abc.changeDate)
     when abc.tfrMax = abc.tfrnum then datediff(day,lag(abc.changedate) over(partition by abc.claimID order by abc.claimId),abc.changeDate)
end as test
, abc.*
from
(
SELECT
         a.claimId
        ,a.changeDate
        ,a.AssignedAdj
        ,a.TransferedAdj
        ,a.Coverage
        ,ROW_NUMBER() Over ( Partition By a.claimId Order By a.changeDate) as tfrNum
        ,b.tfrMax
    FROM  db1.dbo.Assign_Transfer a
    Left Join 
          (SELECT claimId, COUNT(*) as tfrMax
           FROM  db1.dbo.Assign_Transfer 
           Group By claimId
           ) as b
    On b.claimId = a.claimId
) abc   
group by 
         abc.claimId
        ,abc.changeDate
        ,abc.AssignedAdj
        ,abc.TransferedAdj
        ,abc.Coverage
        ,abc.tfrMax
        ,abc.tfrNum