SQL查询以找到所有具有标志的逻辑表达式的UUID

发布于 2025-02-10 06:17:46 字数 503 浏览 2 评论 0原文

我有一个带有UUID和一个标志列的SQLite数据库(在其他列中),我想返回所有满足标志逻辑表达式的UUID。

  UUID | flag | ...
   1   | "a"  |
   1   | "b"  |
   1   | "a"  |
   2   | "b"  |
   2   | "c"  | 
   3   | "a"  |

例如,我想返回所有在所有行上都具有标志(“ a”和“ b”或“ c”)的UUID。在上表中,只有uuid = 1满足该约束。

这是一个类似的问题 - 没有脱节---因此,那里的解决方案在这里不起作用。

编辑:@forpas 拥有sum解决方案是我想要的,但是我最终通过在看到它看到的用户定义的聚合函数来解决问题。

I have a SQLite database with ~30 million rows with UUIDs and a flag column (among other columns) and I want to return all UUIDs that satisfy a logical expression of the flags.

  UUID | flag | ...
   1   | "a"  |
   1   | "b"  |
   1   | "a"  |
   2   | "b"  |
   2   | "c"  | 
   3   | "a"  |

For example I want to return all UUIDs that have flag ("a" AND ("b" or "c")) over all rows. In the above table only UUID=1 satisfies that constraint.

This is a similar question but it only asks about the case of having all 4 flags set --- there is no disjunction --- so the solutions there don't work here.

edit: @forpas HAVING SUM solution is what I was looking for but I ending up solving the problem by creating a user defined aggregate function before I saw it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攒眉千度 2025-02-17 06:17:46

您可以使用具有子句中的条件的聚合:

SELECT UUID
FROM tablename
GROUP BY UUID
HAVING SUM(flag = 'a') > 0
   AND SUM(flag IN ('b', 'c')) > 0;

您可以添加任意多的条件。

请参阅 demo

You can use aggregation with the conditions in the HAVING clause:

SELECT UUID
FROM tablename
GROUP BY UUID
HAVING SUM(flag = 'a') > 0
   AND SUM(flag IN ('b', 'c')) > 0;

You can add as many conditions as you want.

See the demo.

清引 2025-02-17 06:17:46

使用min()和max()函数考虑以下方法:

select UUID
from Tbl
group by UUID having min(flag)="a" and min(flag)<>max(flag)

min()与字符串一起使用时,字符串是按字母顺序排列的,并且第一个返回。在您的情况下,具有a值的标志将是最小值,并且具有c值的标志将是最大值。 HASTER子句将确保组内有一个a值,并且该值不等于组的最大标志值(在这种情况下为b或c)。

请参阅在这里的演示。

Consider the following approach using min() and max() functions:

select UUID
from Tbl
group by UUID having min(flag)="a" and min(flag)<>max(flag)

When min() is used with strings, the strings are ordered alphabetically A-Z and the first one is returned. In your case flags with a value will be the minimum and flags with c value will be the maximum. The having clause will ensure that there's an a value within the group and that value is not equal to the maximum flag value of the group (in this case b or c).

See a demo from here.

星星的軌跡 2025-02-17 06:17:46

您可以使用存在,如果还有其他uUID的行,并且标志

SELECT
    UUID 
FROM
    mytable m
WHERE
    flag = 'a'
AND
    (EXISTS (
        SELECT 
            1 
        FROM 
            mytable
        WHERE 
           flag = 'b'
           and UUID = m.UUID

    ) OR
     EXISTS (
        SELECT 
            1 
        FROM 
            mytable
        WHERE 
           flag = 'c'
           and UUID = m.UUID
        )
    )

可以更简单地写

SELECT
    UUID 
FROM
    mytable m
WHERE
    flag = 'a'
AND
    EXISTS (
        SELECT 
            1 
        FROM 
            mytable
        WHERE 
           flag IN ('b','c')
           and UUID = m.UUID

    ) 
    

you can use EXISTS if there are other rows with the same UUID, and the flag

SELECT
    UUID 
FROM
    mytable m
WHERE
    flag = 'a'
AND
    (EXISTS (
        SELECT 
            1 
        FROM 
            mytable
        WHERE 
           flag = 'b'
           and UUID = m.UUID

    ) OR
     EXISTS (
        SELECT 
            1 
        FROM 
            mytable
        WHERE 
           flag = 'c'
           and UUID = m.UUID
        )
    )

or you can simpler write

SELECT
    UUID 
FROM
    mytable m
WHERE
    flag = 'a'
AND
    EXISTS (
        SELECT 
            1 
        FROM 
            mytable
        WHERE 
           flag IN ('b','c')
           and UUID = m.UUID

    ) 
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文