历史数据存储和计算的数据库问题
我有一个应用程序,可以在不同时间轮询不同的垃圾箱,并根据重量计算每个垃圾箱中的小部件数量。每隔几分钟进行一次轮询,结果带有时间戳并添加到 MySQL 表中。该表包含以下 3 列。该示例显示了 3 个容器(A、B 和 C),但同一小部件可能有 1 到 10 个容器。 (微小的小部件可能只在 1 或 2 个垃圾箱中,较大的小部件可能会占用更多的垃圾箱)
timestamp bin Widget_Count
--------------------------
1 A 8
2 B 7
3 C 4
4 A 1
5 B 3
6 C 5
7 A 6
8 B 7
9 C 2
应用程序需要生成“库存历史”报告 - 这将涉及在每个时间步计算总数量是多少。该时间步长中所有垃圾箱中的物品数量。对于此示例,报告将仅包含下面的时间戳列和 CountHistory 列(最后一列)(显示其他列只是为了显示计算结果)
在时间 1,A 已被轮询并具有 8 个小部件。 B 和 C 尚未进行民意调查。所以总数是 8。
在时间 2,B 已被轮询并且有 7 个小部件。所以总数是 17
在时间 3,C 已被轮询并且有 4 个小部件。所以总数是 19
在时间 4,A 再次轮询,只有 1 个小部件。所以总数现在是 1+4+7=12
..依此类推。
timestamp bin Widget_ A B C CountHistory
Count (stock flow)
--------------------------------------------------------
1 A 8 8 0 0 8
2 B 7 8 7 0 15
3 C 4 8 7 4 19
4 A 1 1 7 4 12
5 B 3 1 3 4 8
6 C 5 1 3 5 9
7 A 6 6 3 5 14
8 B 7 6 7 5 18
9 C 2 6 7 2 15
如果您能提供有关如何最好地解决此问题的帮助,我将不胜感激。我尝试创建一个临时表,使用游标滚动浏览每条记录,但无法获得正确的查询。
(我之前曾问过一个相关的问题,但我没有正确地提出问题,并且也搞砸了示例。 需要有关 Microsoft Access 中的查询(视图)的帮助)
I have an application that polls different bins at different times and computes the number of widgets in each bin based on the weight. Polling is done every few minutes and the result is timestamped and added to a MySQL table. The table contains the 3 columns below. The example shows 3 bins (A,B and C) but there could be anywhere from 1 to 10 bins for the same widget. (Tiny widgets could be just in 1 or 2 bins and larger widgets may take up more bins)
timestamp bin Widget_Count
--------------------------
1 A 8
2 B 7
3 C 4
4 A 1
5 B 3
6 C 5
7 A 6
8 B 7
9 C 2
The application needs to generate a 'stock history' report - this would involve computing at each timestep what is the total no. of items from all bins at that timestep. For this example, the report will contain only the timestamp column and the CountHistory column (last column) below (the other columns are shown just to show the computations)
At time 1, A has been polled and has 8 widgets. B and C have not been polled. So the total is 8.
At time 2, B has been polled and has 7 widgets. So the total is 17
At time 3, C has been polled and has 4 widgets. So the total is 19
At time 4, A polled again and has only 1 widget. So the total is now 1+4+7=12
..and so on.
timestamp bin Widget_ A B C CountHistory
Count (stock flow)
--------------------------------------------------------
1 A 8 8 0 0 8
2 B 7 8 7 0 15
3 C 4 8 7 4 19
4 A 1 1 7 4 12
5 B 3 1 3 4 8
6 C 5 1 3 5 9
7 A 6 6 3 5 14
8 B 7 6 7 5 18
9 C 2 6 7 2 15
I would appreciate any help on how best to go about this. I tried to create a temporary table, scroll through each record using a cursor but could not get the correct queries.
(I had asked a related question before but I did not frame the question correctly and goofed up the example as well. Need Help with queries (views) in Microsoft Access)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想这会满足你的需要。内部查询查找每个时间戳的每个 bin 更改的最新时间戳。
I think this will do what you need. The inner query finds the most recent timestamp at which each bin was changed for each timestamp.
如果没有实际的模型 (DDL) 和数据(包括预期的输出),我不确定是否会尝试提供实际的、经过语法检查的示例 SQL。尽管我确实认为这在应用程序代码或使用游标中很容易做到,但我认为它可以在单个查询中完成。
也就是说,它会是这样的——考虑这个 100% 伪代码:
它可能可以通过连接而不是子查询来完成,并且连接可能会更有效。但你明白了。
编辑:好吧,这个问题让我脑血沸腾,直到我满意地解决它为止我才能休息。然后我回来发布更新,保罗已经回答了一个更优雅的解决方案。 :)
这是我使用 SQL(使用 SQL Server)的解决方案,以防有人关心:
Without an actual model (DDL) and data (including expected output), I'm not sure I would attempt to provide actual, syntax-checked sample SQL. And although I do think this would be easy to do in application code or with a cursor, I think it could be done in a single query.
That said, it would be something like this--consider this 100% pseudocode:
It could probably be done with joins instead of subqueries, and joins would liekly be more efficient. But you get the idea.
Edit: ok, the question got the brain blood flowing, and I couldn't rest until I solved it to my satisfaction. Then I came back to post an update and Paul had already answwered with a more elegent solution. :)
Here is my solution with working SQL (using SQL Server) in case anyone cares: