English 中文(简体)
储存和计算历史数据的数据库问题
原标题:A database question for storing and computing with historical data

我的申请在不同时间对不同的生物体进行污染,并根据重量计算每双生物的植被数量。 投票每几分钟就进行,结果就会被篡改,并添加到我的SQL桌上。 下表载有以下三栏。 例子显示有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

申请需要产生一份库存历史报告,这将涉及在每一时间点计算当时所有双目物品的总数。 举例来说,报告将只包含下面的时间栏和上列栏(最后一栏)(其他栏仅显示计算)。

当时有1个A被污染,有8个植被。 B和C没有受到污染。 因此,总数为8个。

当时,B被污染,有7个植被。 因此,总数为17

当时有3个C被污染,有4个植被。 因此,总数为19个。

当时,有4人再次被污染,只有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查询

最佳回答

我认为,这将满足你们的需要。 内部查询发现,最近每个班次都更换过。

SELECT m.timestamp, SUM(w.Widget_Count) AS CountHistory
FROM (
    SELECT a.timestamp, b.bin, MAX(b.timestamp) AS intMostRecentTimestamp
    FROM WidgetTable a
        CROSS JOIN WidgetTable b
    WHERE a.timestamp >= b.timestamp
    GROUP BY a.timestamp, b.bin, a.Widget_Count
) m 
    INNER JOIN WidgetTable w ON m.intMostRecentTimestamp = w.timestamp
GROUP BY m.timestamp
ORDER BY m.timestamp
问题回答

如果没有实际模型(DDL)和数据(包括预期产出),我不敢肯定我会试图提供实际的Syntax-checked样本。 虽然我确实认为,这在适用法或与一位治疗者打交道方面是容易的,但我认为,这可以在一个单一问题上做到。

也就是说,像这一100%的伪装一样:

SELECT
 timestamp,
 bin,
 widget_count,
 (SELECT A that is most recent and <= current row s timestamp) A,
 (SELECT B that is most recent and <= current row s timestamp) B,
 (SELECT C that is most recent and <= current row s timestamp) C,
 (SELECT sum of last three columns) CountHistory
FROM
 widget_bin_table

也许可以通过加入而不是按次顺序进行,加入就会提高效率。 但是,你们有了这个想法。


Edit: ok,问题涉及大脑血流,在我满意解决之前,我 could。 然后,我回到了最新情况的后面,保罗已经用一个更精干的解决办法了。

这里,我的解决办法是,如果有人关心的话,就与工作(利用服务器)一道工作:

SELECT
    timestamp, bin, widget_count, A, B, C, A + B + C CountHistory
FROM
    (
     SELECT
        wb.timestamp,
        wb.bin,
        wb.widget_count,
        ISNULL((SELECT TOP 1 widget_count FROM widget_bin wb1 WHERE wb1.bin =  A  
                AND wb1.timestamp <= wb.timestamp ORDER BY wb1.timestamp DESC), 0) A,
        ISNULL((SELECT TOP 1 widget_count FROM widget_bin wb1 WHERE wb1.bin =  B  
                AND wb1.timestamp <= wb.timestamp ORDER BY wb1.timestamp DESC), 0) B,
        ISNULL((SELECT TOP 1 widget_count FROM widget_bin wb1 WHERE wb1.bin =  C  
                AND wb1.timestamp <= wb.timestamp ORDER BY wb1.timestamp DESC), 0) C
     FROM
        widget_bin wb
    ) sub




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签