RN LSD ED Aging
------------------------------------------
11111111 09-Feb-2017 06-Feb-2017 3
22222222 09-Feb-2017 02-Feb-2017 7
33333333 20-Jan-2017 29-Nov-2016 44
33333333 20-Jan-2017 07-Dec-2016 44
33333333 20-Jan-2017 29-Nov-2016 44
33333333 20-Jan-2017 30-Nov-2016 44
33333333 20-Jan-2017 29-Nov-2016 44
44444444 12-Dec-2016 19-Nov-2016 17
44444444 12-Dec-2016 19-Nov-2016 17
44444444 12-Dec-2016 25-Nov-2016 17
55555555 07-Feb-2017 04-Dec-2016 56
55555555 07-Feb-2017 04-Dec-2016 56
55555555 07-Feb-2017 13-Dec-2016 56
如何通过按aging
列分组,根据最后(最多)ED Date
列更新RN
列?
答案 0 :(得分:0)
我建议将日期格式更改为yyyy-mm-dd,因为它更容易计算和排序。然后你应该能够解决SQL查询的差异。
语法
DATEDIFF(datepart, startdate, enddate)
实施例
SELECT DATEDIFF(day, '2014-08-05', '2014-06-05') AS DiffDate
答案 1 :(得分:0)
我猜你可以试试这个。但我对你的要求并不清楚。您在下面找到的代码不是解决方案中最短的代码,但可以完成这项工作。
SELECT X.RN , X.LSD, X.ED , B.Aging
FROM X
LEFT JOIN
(
SELECT RN , DATEDIFF(day,LSD,ED) Aging
FROM
(
SELECT RN , MIN(LSD) LSD, MAX(ED) ED
FROM X
GROUP BY RN
)A
)B
ON X.RN = B.RN
答案 2 :(得分:0)
您的更新将如下所示:
const { expand, expanded } = require('expand-swagger-refs');
const schema = require('./api/swagger.json');
// Create a copy of the schema, with $ref values expanded:
const expandedSchema = expanded(schema);
// Or expand the schema object in-place (mutates the object):
expand(schema)
但您可以使用
在视图中动态执行此操作update tab as t1
set aging =
(select (DATEDIFF(day, MIN(startdate), MAX(enddate))
from tab as t2
where t1.rn = t2.rn
)
答案 3 :(得分:0)
你可以使用max和partition with CTE
;with cte as (
select *, DateDiff(day, ED, LSD) as Ag, Max(Ed) over (partition by rn) MaxEd from #yourAging
) Select a.RN, a.LSD, a.ED, Aging = sum(tmpSm) over(partition by a.rn)
from ( select *,case when max(ed) over (partition by rn)=ED then Ag else 0 end as TmpSm from cte ) a
答案 4 :(得分:0)
我认为实现目标的最佳方法是像贝洛一样
DECLARE @MyTable TABLE
(
RN int,
LSD date,
ED date,
Aging int
)
INSERT INTO @MyTable VALUES
('11111111' ,'09-Feb-2017' ,'06-Feb-2017' ,'3'),
('22222222' ,'09-Feb-2017' ,'02-Feb-2017' ,'7' ),
('33333333' ,'20-Jan-2017' ,'29-Nov-2016' ,'44'),
('33333333' ,'20-Jan-2017' ,'07-Dec-2016' ,'44'),
('33333333' ,'20-Jan-2017' ,'29-Nov-2016' ,'44'),
('33333333' ,'20-Jan-2017' ,'30-Nov-2016' ,'44') ,
('33333333' ,'20-Jan-2017' ,'29-Nov-2016' ,'44'),
('44444444' ,'12-Dec-2016' ,'19-Nov-2016' ,'17') ,
('44444444' ,'12-Dec-2016' ,'19-Nov-2016' ,'17'),
('44444444' ,'12-Dec-2016' ,'25-Nov-2016' ,'17'),
('55555555' ,'07-Feb-2017' ,'04-Dec-2016' ,'56') ,
('55555555' ,'07-Feb-2017' ,'04-Dec-2016' ,'56'),
('55555555' ,'07-Feb-2017' ,'13-Dec-2016' ,'56')
;with cteToUpdate AS
(
SELECT RN,DATEDIFF(D,MAX(ED),MIN(LSD)) as Aging FROM @MyTable
GROUP BY RN
)
UPDATE source
SET source.Aging = cteToUpdate.Aging
FROM @MyTable as source
JOIN cteToUpdate ON cteToUpdate.RN =source.RN