首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用子组限制创建行组

使用子组限制创建行组
EN

Stack Overflow用户
提问于 2018-06-30 23:41:46
回答 1查看 69关注 0票数 0

在sqlite数据库中有一个视图,如下所示:

SQL (http://www.sqlfiddle.com/#!5/ae95b/1)

这表示进入每个框的框和项的列表。项目总是由box_start排序,而范围box_startbox_end从不重叠。

如第一行所示,有一个框可以存储代码从1到3的项目。例如,方框A中的项目将是'a‘和'b’。

框项分组由着色表示,颜色相同的行表示它们是一个组。

也有没有指定框的项(框标签为空),例如项“c”、“f”、“g”、“h”。

如果可能的话,我需要编写一个查询,为没有框的项目创建临时框,并将连续项分组到相同的框中,如下所示:

如图像所示,项目'c‘及其相应的框被标记为'4-4’,而以前未赋值的项'f‘、'g’、'h‘现在被分组在标记为'8-10’的框下,该框对应于‘min( f,g,h的box_start)-max( f,g,h的box_end of f,g,h)’。

我不知道如何在SQLite中做到这一点。我考虑过在CTE中使用某种递归查询,但不知道如何实现。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-01 05:32:28

在做了一些工作之后,我有以下查询:

代码语言:javascript
运行
复制
select
    min(box_start) as box_start,
    box_end,
    box_label,
    is_box,
    item_code,
    item
from
    (
        select
            box_start,
            box_end,
            box_label,
            is_box,
            item_code,
            item
        from
            table1
        where
            box_label is not null

        union all

        select
            table1.box_start as box_start,
            table1.box_end as box_end,
            intervals.A || '-' || intervals.B as box_label,
            table1.is_box as is_box,
            table1.item_code as item_code,
            table1.item as item
        from
            (
                select
                    box_start,
                    box_end,
                    box_label,
                    is_box,
                    A,
                    B,
                    max(max_interval_size) as max_interval_size
                from
                    (
                        select
                            box_start,
                            box_end,
                            box_label,
                            is_box,
                            A,
                            B,
                            max(interval_size) as max_interval_size
                        from
                            (
                                select
                                  fixed_table.box_start as box_start,
                                  fixed_table.box_end as box_end,
                                  fixed_table.box_label as box_label,
                                  fixed_table.is_box as is_box,
                                  fixed_table.box_start as A,
                                  windowed_table.box_end as B,
                                  (windowed_table.box_end - fixed_table.box_start) as interval_size
                                from
                                  table1 fixed_table
                                  join table1 windowed_table on
                                    fixed_table.box_start <= windowed_table.box_end
                                where 
                                  interval_size >= 0
                                  and fixed_table.box_label is null
                                  and windowed_table.box_label is null
                                  and fixed_table.is_box = 'FALSE'
                                  and windowed_table.is_box = 'FALSE'

                                except

                                select
                                  without_a_box.*
                                from
                                    (
                                        select
                                          fixed_table.box_start as box_start,
                                          fixed_table.box_end as box_end,
                                          fixed_table.box_label as box_label,
                                          fixed_table.is_box as is_box,
                                          fixed_table.box_start as A,
                                          windowed_table.box_end as B,
                                          (windowed_table.box_end - fixed_table.box_start) as interval_size
                                        from
                                          table1 fixed_table
                                          join table1 windowed_table on
                                            fixed_table.box_start <= windowed_table.box_end
                                        where 
                                          interval_size >= 0
                                          and fixed_table.box_label is null
                                          and windowed_table.box_label is null
                                          and fixed_table.is_box = 'FALSE'
                                          and windowed_table.is_box = 'FALSE'
                                    ) as without_a_box
                                    ,
                                    (
                                        select distinct
                                            with_box.box_start as start_with_box
                                         from
                                            table1 with_box
                                         where
                                            with_box.is_box = 'FALSE'
                                            and with_box.box_label is not null
                                    ) as items_inside_a_box
                                where
                                    items_inside_a_box.start_with_box > without_a_box.A
                                    and items_inside_a_box.start_with_box < without_a_box.B
                            ) as without_intervals_that_intersect_boxed_items
                        group by
                            A
                    ) as final
                group by
                    B
            ) as intervals
            join table1 on
                table1.box_start >= intervals.A
                and table1.box_end <= intervals.B
                and table1.box_label is null
    )
group by
    box_label,
    is_box,
    item_code,
    item
order by
    box_start,
    item_code

SQL:http://www.sqlfiddle.com/#!7/4a643e/142

尽管它似乎在做它的工作,我不确定在所有情况下都是正确的,如果它不是一个性能瓶颈。

我希望有人有更好的解决方案

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51119365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档