凝聚行通过评论栏

2023-09-08 11:11:23 作者:别失去你的笑容

我有数据称为表 LogIndex 其中,它的一个片段是这样的:

I have a table of data called LogIndex where a slice of it looks like this:

rowid   fruit   test    cat_id  start_time          end_time            result  comment
165     Apple   Peel    2590110 1/23/2014 0:20:35   1/23/2014 0:21:09   Remove  rotten
166     Apple   Peel    2590211 1/23/2014 0:26:35   1/23/2014 0:27:09   Remove  rotten
167     Apple   Peel    2590310 1/23/2014 0:32:35   1/23/2014 0:33:09   Remove  rotten
168     Apple   Peel    2590409 1/23/2014 0:38:35   1/23/2014 0:39:09   Remove  rotten
169     Apple   Peel    2590509 1/23/2014 0:44:35   1/23/2014 0:45:09   Remove  rotten
170     Apple   Peel    2590608 1/23/2014 0:50:35   1/23/2014 0:51:09   Remove  rotten
252     Apple   Peel    3516509 1/24/2014 1:56:35   1/24/2014 1:57:09   Remove  rotten
253     Apple   Peel    3516607 1/24/2014 2:02:35   1/24/2014 2:03:09   Remove  rotten
463     Apple   Peel    5485587 1/26/2014 22:56:35  1/26/2014 22:57:09  Remove  rotten
464     Apple   Peel    5485690 1/26/2014 23:02:35  1/26/2014 23:03:09  Remove  rotten
465     Apple   Peel    5485791 1/26/2014 23:08:35  1/26/2014 23:09:09  Remove  rotten
466     Apple   Peel    5485899 1/26/2014 23:14:35  1/26/2014 23:15:09  Remove  rotten
467     Apple   Peel    5486006 1/26/2014 23:20:35  1/26/2014 23:21:09  Remove  rotten
468     Apple   Peel    5486118 1/26/2014 23:26:35  1/26/2014 23:27:09  Remove  rotten
469     Apple   Peel    5486227 1/26/2014 23:32:35  1/26/2014 23:33:09  Remove  rotten
470     Apple   Peel    5486334 1/26/2014 23:38:35  1/26/2014 23:39:09  Remove  rotten
471     Apple   Peel    5486436 1/26/2014 23:44:35  1/26/2014 23:45:09  Remove  rotten
472     Apple   Peel    5486535 1/26/2014 23:50:35  1/26/2014 23:51:09  Remove  rotten
473     Apple   Peel    5486636 1/26/2014 23:56:35  1/26/2014 23:57:09  Remove  rotten
474     Apple   Peel    5486747 1/27/2014 0:02:35   1/27/2014 0:03:09   Remove  rotten
475     Apple   Peel    5486857 1/27/2014 0:08:35   1/27/2014 0:09:09   Remove  rotten

其中, ROWID =自动递增整数PK和 cat_id 是FK用于不同的表。其中,该数据来自全台有更多的行,由水果列和测试列有序对于自动递增。可以有任何数量的水果的值,但只有永远八种测试值。还可以有评论的无限量值(它们是人类输入)虽然这是非常可能的一些值可以是相同的,在数据​​的各个点。

Where rowid = auto increment integer PK and cat_id is a FK for a different table. The whole table where this data comes from has many more rows and is ordered by the fruit column and the test column in respect to the auto increment. There can be any number of fruit values, but there is only ever eight types of test values. There can also be an infinite amount of comment values (they are human-entered) though it is very likely that some values may be the same at various points in the data.

基本上,我试图写一个观点,即会凝结下来这些行的东西,看起来像这样:

Basically, I am trying to write a view that would condense these rows down to something that looks like this:

rowid   fruit   test    cat_id  start_time              end_time                result  comment
165     Apple   Peel    2590110 1/23/2014 0:20:35       1/23/2014 0:51:09       Remove  rotten
252     Apple   Peel    3516509 1/24/2014 1:56:35       1/24/2014 2:03:09       Remove  rotten
463     Apple   Peel    5485587 1/26/2014 22:56:35      1/27/2014 0:09:09       Remove  rotten

如果我愿意的 MIN() ROWID 时, MIN( ) cat_id 的 MIN() START_TIME MAX() END_TIME 哪里有行的连续的块,其中评论的值都是一样的。

Where I'd want the MIN() of rowid, the MIN() of cat_id, the MIN() of start_time, the MAX() of end_time where there is a consecutive block of rows where the comment values are all the same.

我知道使用DLookup(评论,LogIndex,ROWID =&放大器; ROWID + 1)会给我的下一行的注释值进行比较,所以我尝试这样的查询:

I know that DLookUp("comment", "LogIndex", "rowid = " & rowid + 1) will give me the next row's comment value to compare, so I tried a query like this:

SELECT rowid, fruit, type, MIN(cat_id), MIN(start_time), MAX(end_time), result, comment
FROM LogIndex
AND comment IS NOT NULL
AND result IS NOT NULL
GROUP BY fruit, type, rowid, result, comment, start_time, end_time
HAVING DLookup("comment", "LogIndex", "rowid = " & rowid + 1) = comment
ORDER BY fruit, type

除了它叶子掉在上面的示例中的最后一行(可能是因为ROWID 171比170空或不同的意见),这并不凝结什么。

Which doesn't condense anything except it leaves off the last row in the above sample (likely because rowid 171 has a blank or different comment than 170).

我有了一个价值每年为另一种查询的几乎的工作原理与另外一列从另一个表加入( TSV ) ,月,日和24小时的每一行。一个例子看起来像 2014012300 为2014年1月23日00:00:00。查询是这样的:

I have another query that almost works with the addition of a column joined from another table (tsv) that has a value for each year, month day and 24 hour for each row. An example looks like 2014012300 for "1/23/2014 00:00:00". The query looks like:

SELECT li.rowid, li.fruit, li.type, MIN(li.cat_id), MIN(li.start_time),MAX(tsv.end_time), li.result as vetting_results, li.comment
FROM LogIndex as li
INNER JOIN tsv ON tsv.cat_id = li.cat_id
WHERE li.result IS NOT NULL 
AND li.comment IS NOT NULL
GROUP BY tsv.collection_id, li.fruit, li.type, li.result, li.comment, tsv.utc_start_datehr
ORDER BY tsv.collection_id, li.fruit, MIN(tsv.start_time), MAX(tsv.end_time);

这个查询的唯一的事情是,如果存在不都是在相同的 utc_start_datehr 值的任何意见,那么他们出现每小时分割行。在两个单独的行463至473和474至475:例如,将组中的下列行id在一起。我想463进行分组到475,因为它们都按顺序(由ROWID)具有相同的值评论

The only thing about this query is that if there are any comments that aren't all under the same utc_start_datehr value, then they appear as hourly-divided rows. For example, it would group the following rowids together: 463 to 473 and 474 to 475 in two separate rows. I'd want 463 to be grouped down to 475 because they all sequentially (by rowid) have the same value for comment.

如果对我有一些方法来标志 LogIndex 表与评论团块分组的唯一编号的每个单独的行的值,那么我假设我能集团通过该方法。

If there were some way for me to flag each individual row of the LogIndex table with a unique number grouped by clumps of comment values, then I am assuming that I could GROUP BY that method.

我接受使用VBA,如果这是比较容易的,但我想避免访问外部使用外部的依赖,除非它是不可能做什么,我试图做的。

I'm open to using VBA if that is easier, but I'd like to avoid using an external dependency outside of Access unless it is impossible to do what I am attempting to do.

推荐答案

您正在寻找这里的措辞是你想要的数据的连续的块。这也可以称为差距和孤岛问题,因为你正在寻找一些共享属性数据的孤岛。有与相邻的块在SQL处理有好的页面:http://gcbenison.word$p$pss.com/2011/09/26/queries-that-group-tables-by-contiguous-blocks/

The wording you're looking for here is that you want a "contiguous block" of data. This can also be called a "gaps and islands" problem as you are looking for "islands" of data with some shared property. There's a good page on dealing with contiguous blocks in SQL here: http://gcbenison.wordpress.com/2011/09/26/queries-that-group-tables-by-contiguous-blocks/

一个简单的GROUP BY语句不会在这里做的,因为我们不只是想一个组匹配我们的标准都行,我们要preserve行的顺序和找到的第一个和最后一行每个连续的块。因此,如何做到这一点,因为没有看下一行或看最后一排功能,在SELECT语句?关键是要加入表本身,由一个行偏移。

"A simple GROUP BY statement will not do here, because we do not just want a single group of all rows matching our criteria; we want to preserve the order of the rows and find the first and last row of each contiguous block. So how to do that, given that there is no "look at next row" or "look at last row" function in a SELECT statement? The key is to join the table to itself, offset by one row."

现在这个确切的方法可能不适合您的问题的工作,但在这读了应该帮助你的答案到达。正如在这里他们的解决方案,你可能需要加入表本身进行更多的分析。如果失败则使用Google的SQL连续的块或SQL差距和孤岛将带领你更接近一个解决方案。

Now this exact approach may not work for your problem, but reading up on this should help you to arrive at an answer. As with their solution here you may well need to join the table to itself and perform more analysis. If that fails then googling for "SQL contiguous blocks" or "SQL gaps and islands" will lead you closer to a solution.

相关推荐