栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

查询记录并按时间段分组

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

查询记录并按时间段分组

如果您确定这些运行是连续的并且不重叠,则应该可以使用Id字段来拆分组。查找仅相距1的ID字段以及大于相差某个阈值的日期创建的字段。从您的数据来看,一次运行中的记录看起来最多只能在1分钟内输入一次,因此安全阈值可能是1分钟或更长。

这将为您提供开始时间

SELECt mrtB.Id, mrtB.DateCreatedFROM MyReportTable AS mrtAINNER JOIN MyReportTable AS mrtB    ON (mrtA.Id + 1) = mrtB.IdWHERe DateDiff(mi, mrtA.DateCreated, mrtB.DateCreated) >= 1

我称它为DataRunStarts

现在,您可以使用它来获取有关组的开始和结束位置的信息

SELECt drsA.Id AS StartID, drsA.DateCreated, Min(drsB.Id) AS ExcludedEndIdFROM DataRunStarts AS drsA, DataRunStarts AS drsBWHERe (((drsB.Id)>[drsA].[id]))GROUP BY drsA.Id, drsA.DateCreated

我将其称为DataRunGroups。我将最后一个字段称为“已排除”,因为它所持有的ID仅用于定义将被拉出的ID集的结束边界。

现在我们可以使用DataRunGroups和MyReportTable来获取计数

SELECt DataRunGroups.StartID, Count(MyReportTable.Id) AS CountOfRecordsFROM DataRunGroups, MyReportTableWHERe (((MyReportTable.Id)>=[StartId] And (MyReportTable.Id)<[ExcludedEndId]))GROUP BY DataRunGroups.StartID;

我称它为DataRunCounts

现在,我们可以将DataRunGroups和DataRunCounts放在一起以获取开始时间和计数。

SELECt DataRunGroups.DateCreated, DataRunCounts.CountOfRecordsFROM DataRunGroupsINNER JOIN DataRunCounts    ON DataRunGroups.StartID = DataRunCounts.StartID;

Depending on your setup, you may need to do all of this on one query, but you
get the idea. Also, the very first and very last runs wouldn’t be included in
this, because there’d be no start id to go by for the very first run, and no
end id to go by for the very last run. To include those, you would make
queries for just those two ranges, and union them together along with the old
DataRunGroups query to create a new DataRunGroups. The other queries that use
DataRunGroups would work just as described above.




转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410951.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号