我需要在特定时间段(datediff 18小时或22小时)内汇总行中的值(Amount2),还需要知道使用了多少行。这些值的总和必须在ID号内完成。有人可以帮忙吗?
表T1的第一行:
"scripts": {
...
"reactotron": "adb reverse tcp:9090 tcp:9090 && /opt/Reactotron/reactotron-app",
...
}
我需要的是一个脚本,该脚本将从PickupDate时间在18小时或22小时之内的行中求和Amount2,还计算求和的行(必须在ID中完成)。
ID PickupDateTime Amount2 DifferenceToPrevious
1 2019-04-02 04:44:19.000 6458 0d 00:13:17:000
1 2019-04-02 04:31:02.000 5385 0d 02:34:50:000
1 2019-04-02 01:56:12.000 1545 0d 18:06:18:000
1 2019-04-01 07:49:54.000 3466 1d 02:23:51:000
1 2019-03-31 05:26:03.000 7505 0d 00:13:53:000
1 2019-03-31 05:12:10.000 5080 0d 03:28:18:000
1 2019-03-31 01:43:52.000 1166 0d 18:02:49:000
1 2019-03-30 07:41:03.000 2991 1d 02:39:49:000
1 2019-03-29 05:01:14.000 7065 0d 00:13:11:000
2 2019-04-01 04:56:21.000 9518 1d 23:52:21:000
2 2019-03-30 05:04:00.000 9638 2d 00:34:29:000
2 2019-03-28 04:29:31.000 9499 1d 23:28:47:000
2 2019-03-26 05:00:44.000 10117 2d 00:03:41:000
2 2019-03-24 04:57:03.000 9933 1d 23:50:06:000
2 2019-03-22 05:06:57.000 9869 2d 00:25:01:000
答案 0 :(得分:1)
测试数据:
(?<=>)https:\/\/test.be\/assets\/kcfinder\/upload\/files\/[\w\.]*
查询以获取所需的结果。关键是使用带有适当declare @tbl table (ID int, PickupDateTime datetime2(3), Amount2 int)
insert into @tbl values
(1, '2019-04-02 04:44:19.000', 6458),
(1, '2019-04-02 04:31:02.000', 5385),
(1, '2019-04-02 01:56:12.000', 1545),
(1, '2019-04-01 07:49:54.000', 3466),
(1, '2019-03-31 05:26:03.000', 7505),
(1, '2019-03-31 05:12:10.000', 5080),
(1, '2019-03-31 01:43:52.000', 1166),
(1, '2019-03-30 07:41:03.000', 2991),
(1, '2019-03-29 05:01:14.000', 7065),
(2, '2019-04-01 04:56:21.000', 9518),
(2, '2019-03-30 05:04:00.000', 9638),
(2, '2019-03-28 04:29:31.000', 9499),
(2, '2019-03-26 05:00:44.000', 10117),
(2, '2019-03-24 04:57:03.000', 9933),
(2, '2019-03-22 05:06:57.000', 9869);
子句的子查询:
where
答案 1 :(得分:1)
@MichałTurczyn的查询将提供正确的数据,同时还需要进行5次扫描(1个主扫描+ 4个子查询)。您只需再进行一次扫描即可更有效地完成此操作:
declare @tbl table (ID int, PickupDateTime datetime2(3), Amount2 int)
insert into @tbl values
(1, '2019-04-02 04:44:19.000', 6458),
(1, '2019-04-02 04:31:02.000', 5385),
(1, '2019-04-02 01:56:12.000', 1545),
(1, '2019-04-01 07:49:54.000', 3466),
(1, '2019-03-31 05:26:03.000', 7505),
(1, '2019-03-31 05:12:10.000', 5080),
(1, '2019-03-31 01:43:52.000', 1166),
(1, '2019-03-30 07:41:03.000', 2991),
(1, '2019-03-29 05:01:14.000', 7065),
(2, '2019-04-01 04:56:21.000', 9518),
(2, '2019-03-30 05:04:00.000', 9638),
(2, '2019-03-28 04:29:31.000', 9499),
(2, '2019-03-26 05:00:44.000', 10117),
(2, '2019-03-24 04:57:03.000', 9933),
(2, '2019-03-22 05:06:57.000', 9869);
SELECT pickup.*, subCalc.*
FROM @tbl pickup
CROSS
APPLY (SELECT SUM(incl.include18) AS count18
, SUM(incl.include18 * sub.Amount2) AS amount18
, SUM(incl.include22) AS count22
, SUM(incl.include22 * sub.Amount2) AS amount22
FROM @tbl sub
CROSS
APPLY (SELECT IIF(DATEADD(HOUR, -18, pickup.pickupdatetime) <= sub.PickupDateTime, 1, 0) AS include18
, IIF(DATEADD(HOUR, -22, pickup.pickupdatetime) <= sub.PickupDateTime, 1, 0) AS include22
) incl
WHERE pickup.PickupDateTime >= sub.PickupDateTime
AND pickup.id = sub.id
) subCalc
ORDER BY pickup.PickupDateTime DESC
答案 2 :(得分:0)
Select sum(Amount2) as sum_amount_2, Count(*) as n_lines_summed
From table
WHERE PickupDateTime between DateDiff(HOUR, -22, GetDate()) and DateDiff(HOUR, -18, GetDate())
Group by Id
类似的东西?我认为很简单。
答案 3 :(得分:0)
您需要类似的东西吗?
select * from
( Select PickupDateTime sum(Amount2) as sum_amount_18, Count(*) as n_lines_summed18
From table
WHERE PickupDateTime <= DateDiff(HOUR, -18, GetDate())
Group by PickupDateTime )X join
( Select PickupDateTime sum(Amount2) as sum_amount_22, Count(*) as n_lines_summed22
From table
WHERE PickupDateTime <= DateDiff(HOUR, -22, GetDate())
Group by PickupDateTime )y on X.PickupDateTime = Y.PickupDateTime