我有一个观点,这是它的结果:
ID Type Time RowNum
18 2 2015-12-14 12:15:00.9330000 +00:00 214
18 6 2015-12-14 12:21:22.4330000 +00:00 215
18 2 2015-12-15 09:04:41.2870000 +00:00 216
18 6 2015-12-15 09:06:22.9400000 +00:00 217
18 5 2015-12-15 09:07:28.0130000 +00:00 218
我试图通过使用下面的查询来比较行和组之间的时差。我的问题是最后一行,我希望得到当前时间的差异,并计算差异的总和。这是我正在使用的查询。如何仅比较最后一行与当前时间并计算总和:
WITH rows AS (SELECT * FROM [dbo].[View])
SELECT p.ID ,
p.Type ,
SUM(DATEDIFF(SECOND, p.Time, q.Time))
FROM rows p
JOIN rows q ON p.RowNum = q.RowNum - 1
GROUP BY p.ID ,
p.Type
答案 0 :(得分:1)
如果您使用的是SQL Server 2012或更高版本,则可以使用
@Override
public void process(Exchange exchange) throws Exception {
XMLSerializer serializer = new XMLSerializer();
JSON json=serializer.read(exchange.getIn().getBody(String.class));
StringWriter writer=new StringWriter();
JsonConfig jsonConfig=new JsonConfig();
jsonConfig.registerJsonValueProcessor("entero1", new JsonValueProcessor() {
@Override
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return new BigDecimal(value.toString());
}
@Override
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
return new BigDecimal(value.toString());
}
});
JSONSerializer.toJSON(json, jsonConfig).write(writer);
writer.close();
exchange.getIn().setBody(writer.toString());
}
参考LEAD()
答案 1 :(得分:0)
使用LEFT JOIN
和ISNULL
功能:
DECLARE @t TABLE
(
ID INT ,
Type INT ,
Time DATETIMEOFFSET ,
RowNum INT
)
INSERT INTO @t
VALUES ( 18, 2, '2015-12-14 12:15:00.9330000 +00:00', 214 ),
( 18, 6, '2015-12-14 12:21:22.4330000 +00:00', 215 ),
( 18, 2, '2015-12-15 09:04:41.2870000 +00:00', 216 ),
( 18, 6, '2015-12-15 09:06:22.9400000 +00:00', 217 ),
( 18, 5, '2015-12-15 09:07:28.0130000 +00:00', 218 );
SELECT p.ID ,
p.Type ,
SUM(DATEDIFF(SECOND, p.Time, ISNULL(q.Time, GETDATE())))
FROM @t p
LEFT JOIN @t q ON p.RowNum = q.RowNum - 1
GROUP BY p.ID ,
p.Type
输出:
18 2 483
18 5 21186
18 6 74665