我有这样的疑问:
<Window.Resources>
<Storyboard x:Key="animLOAD" RepeatBehavior="Forever">...</Storyboard>
<Storyboard x:Key="animPREPARED" RepeatBehavior="Forever">...</Storyboard>
<Style x:Key="animStyle" TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=st, Mode=OneWay}" Value="LOAD">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource animLOAD}" />
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding Path=st, Mode=OneWay}" Value="PREPARED">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource animPREPARED}" />
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Image Style="{StaticResource animStyle}" />
我从查询中得到了这样的结果:
SELECT Stamp_date , Stamp_Action FROM FILES_TIME_STAMPS
WHERE Stamp_File_Id = @FileID AND Stamp_Action IN (5,15)
减去两条记录的最佳方法是什么?如果我想轻松地与它们一起维护,我怎么能将它们定义为变量呢?
答案 0 :(得分:1)
如果您只想减去这两个日期,可以使用DATEDIFF
和MIN
- MAX
SELECT DATEDIFF(MINUTE, MIN(Stamp_date) , MAX(Stamp_date)) FROM FILES_TIME_STAMPS
WHERE Stamp_File_Id = @FileID AND Stamp_Action IN (5,15)
您可以根据需要将MINUTE
更改为SECOND
或其他datepart
另外,如果你想根据Stamp_Action选择MIN和MAX,你可以使用它。
SELECT DATEDIFF(MINUTE,
MIN(CASE WHEN Stamp_Action = 5 THEN Stamp_date END) ,
MAX(CASE WHEN Stamp_Action = 15 THEN Stamp_date END ) )
FROM FILES_TIME_STAMPS
WHERE Stamp_File_Id = @FileID AND Stamp_Action IN (5,15)
答案 1 :(得分:1)
这是应用LAG
函数的好例子:
declare @x table([date] datetime, [value] int)
insert into @x values ('2017-12-04 12:56:37.293', 5), ('2017-12-04 15:40:02.593', 15)
select *,LAG([date], 1) over (order by [date]) [DateLag],
LAG([value], 1) over (order by [date]) [ValueLag],
DATEDIFF(minute, [date],LAG([value], 1) over (order by [date])) [DateDifference],
[value] - LAG([value], 1) over (order by [date]) [ValueDifference]
from @x
我尽可能多地包含了一些例子,所以你可以决定你需要什么,看看它是如何工作的:)