我必须创建一个报告,总结一个人有一个案例分配给他们的日子,唯一的问题是有多行。我需要在分配给其他人时计算差异。会有很多案例,而且我不确切地知道每个案件被转移到不同的人的次数。
数据示例
CaseId,CaseCounter,OldValue,NewValue,ChangeDate
399547,1, Brad, Eric, 9/18/2016
399547,2, Meghan, Joni, 9/19/2016
399547,3, Eric, Pam, 9/20/2016
399547,4, Pam, Meghan, 9/22/2016
399547,5, Joni, Eric, 9/25/2016
399547,6, Andrea, Team, 9/30/2016
399547,7, Team, Admin, 10/3/2016
399547,8, Admin, Brad, 10/19/2016
399547,9, Eric, Joni, 10/21/2016 - since this is the last record it should compare this to current date
460771,1, Meghan, Cheryl, 10/1/2016
460771,2, Admin, Meghan, 10/6/2016
460771,3, Admin, Cheryl, 10/6/2016
460771,4, Cheryl, Meghan ,10/9/2016
460771,5, Meghan, Admin, 10/11/2016
CREATE TABLE #Records (CaseID INT, caseCounter INT, oldValue VARCHAR(50),NewValue VARCHAR(50), changeDate DATETIME)
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (399547,1,'Brad','Eric','19/18/2016')
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (399547,2,'Meghan','Joni','9/19/2016')
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (399547,3,'Eric','Pam','9/20/2016')
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (399547,4,'Pam','Meghan','9/22/2016')
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (399547,5,'Joni','Eric','9/25/2016')
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (399547,6,'Andrea','Team','9/30/2016')
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (399547,7,'Team','Admin','10/3/2016')
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (399547,8,'Admin','Brad','10/19/2016')
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (399547,9,'Eric','Joni','10/21/2016')
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (460771,1,'Meghan','Cheryl','10/1/2016')
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (460771,2,'Admin','Meghan','10/6/2016')
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (460771,3,'Admin','Cheryl','10/6/2016')
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (460771,4,'Cheryl','Meghan','42652')
INSERT INTO #Records ( CaseID ,caseCounter ,oldValue ,NewValue , changeDate)VALUES (460771,5,'Meghan','Admin','10/11/2016')
预期结果是
CaseID UseID DayCount
399547 Eric 6
399547 Pam 2
399547 Joni 13 (1 + diff (10/21-today) =12)
答案 0 :(得分:1)
你走了。代码首先通过使用公用表表达式(CTE)创建#Records表的临时视图,以允许将表与自身进行比较。 CTE与#Records
上的CaseID
以及caseCounter
上的caseCounter
相连,CTE中的少于1(即caseCounter
2加入#Records
1 datediff
表),从而允许在旧记录上填充下一个日期。然后执行一个简单的IF object_id('tempdb..#Records') is not null
drop table #records
CREATE TABLE #Records (CaseID INT, caseCounter INT, oldValue VARCHAR(50),NewValue VARCHAR(50), changeDate DATE)
INSERT INTO #Records
VALUES
(399547,1,'Brad','Eric','9/18/2016'),
(399547,2,'Meghan','Joni','9/19/2016'),
(399547,3,'Eric','Pam','9/20/2016'),
(399547,4,'Pam','Meghan','9/22/2016'),
(399547,5,'Joni','Eric','9/25/2016'),
(399547,6,'Andrea','Team','9/30/2016'),
(399547,7,'Team','Admin','10/3/2016'),
(399547,8,'Admin','Brad','10/19/2016'),
(399547,9,'Eric','Joni','10/21/2016'),
(460771,1,'Meghan','Cheryl','10/1/2016'),
(460771,2,'Admin','Meghan','10/6/2016'),
(460771,3,'Admin','Cheryl','10/6/2016'),
(460771,4,'Cheryl','Meghan','10/9/2016'),
(460771,5,'Meghan','Admin','10/11/2016');
IF object_id('tempdb..#temp') is not null
drop table #temp; -- drops temp table if already exists, improves ability to re-run code.
-- CTE created, whic his just a copy of the table to compare against itself
WITH CTE
AS (
SELECT
CaseID
,caseCounter
,oldValue
,NewValue
,changeDate AS NewDate
FROM #Records
)
-- CTE Is left joined because eventually you run out of case counters to join on
SELECT
R.CaseID
,R.caseCounter
,R.oldValue
,R.NewValue AS Name
,changeDate
,CASE WHEN newDate IS NULL THEN CAST(GETDATE() AS DATE) ELSE NewDate END AS HeldUntil -- case statement puts today in where you would have a null new date from the left join
,DATEDIFF(DD,changeDate,CASE WHEN newDate IS NULL THEN CAST(GETDATE() AS DATE) ELSE NewDate END) AS DaysHeld -- date diff between change (i.e. start date) and the changeover date
INTO #temp -- place this into a temp table
FROM #Records AS R
LEFT JOIN cte AS C
ON r.CaseID = C.CaseID
AND r.caseCounter = C.caseCounter - 1;
-- simple summary result set from the above
SELECT
CaseID
,Name
,SUM(DaysHeld) AS DaysHeld
FROM #temp
GROUP BY CaseID
,Name
ORDER BY CaseID, Name ASC;
来获取它所持有的日期,然后计算摘要。
tapply(df$day, df$ID, function(x) 1:max(x))
答案 1 :(得分:0)
这是一条评论/问题,但在评论部分中放置的时间太长。
我不理解你的预期结果。
这是案例399547的数据:
CaseId CaseCounter OldValue NewValue ChangeDate 399547 1 Brad Eric 9/18/2016 399547 2 Meghan Joni 9/19/2016 399547 3 Eric Pam 9/20/2016 399547 4 Pam Meghan 9/22/2016 399547 5 Joni Eric 9/25/2016 399547 6 Andrea Team 9/30/2016 399547 7 Team Admin 10/3/2016 399547 8 Admin Brad 10/19/2016 399547 9 Eric Joni 10/21/2016
订购的数据:
CaseId CaseCounter OldValue NewValue ChangeDate 399547 1 Brad Eric 9/18/2016 399547 3 Eric Pam 9/20/2016 399547 4 Pam Meghan 9/22/2016 399547 2 Meghan Joni 9/19/2016 399547 5 Joni Eric 9/25/2016 399547 9 Eric Joni 10/21/2016 399547 6 Andrea Team 9/30/2016 399547 7 Team Admin 10/3/2016 399547 8 Admin Brad 10/19/2016
显然,最多有三个人同时处理此案。
每人399547案件的总时间:
这与您的预期结果非常不同。那我在哪里错了?您可以通过什么规则获得输出?