Sqlite Join(或其某些变体)

发布于 2024-12-08 03:17:10 字数 724 浏览 0 评论 0原文

给定这样的结果集:

Col1    Col2
============
BAML    491
BARC    362
BDPT    1
BNP     9

(select Col1, count(some_col) as Col2 from Table where another_col='SomeCondition" group by Col1)

和另一个这样的结果集:

Col3    Col2
============
BAML    494
BARC    366
BDPT    1
BNP     10
CALY    3

(select Col3, count(some_col) as Col2 from Table where another_col='SomeOTHERCondition " group by Col3)

如何“合并”这两个查询以形成:

BAML    491       494
BARC    362       366
BDPT    1         1
BNP     9         10
CALY              3

请注意,前两个查询在同一个表上运行。我可以在这里看到 Join 帮助的一些变体(还没有太多地使用 sql)。我只是不知道如何将这两个查询放入单个查询中以获取合并的 reusltset。

Given a resultset like this:

Col1    Col2
============
BAML    491
BARC    362
BDPT    1
BNP     9

(select Col1, count(some_col) as Col2 from Table where another_col='SomeCondition" group by Col1)

and another like this:

Col3    Col2
============
BAML    494
BARC    366
BDPT    1
BNP     10
CALY    3

(select Col3, count(some_col) as Col2 from Table where another_col='SomeOTHERCondition" group by Col3)

How do I "merge" these two queries to form:

BAML    491       494
BARC    362       366
BDPT    1         1
BNP     9         10
CALY              3

Please note that the first two queries operate on the same table. I can see some variant of Join helping here (haven't worked with sql all that much). I just can't figure out how to put those two queries into a single query to get the merged reusltset.

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

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

发布评论

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

评论(1

太阳男子 2024-12-15 03:17:10

试试这个:

select b.col1, b.col2, a.col2
from (
    select Col3, count(some_col) as Col2
    from Table
    where another_col = 'SomeOTHERCondition'
    group by Col3
) as a
left outer join (
    select Col1, count(some_col) as Col2
    from Table
    where another_col = 'SomeCondition'
    group by Col1
) as b
on a.col3 = b.col1

我不确定 SQLite 是否支持这一点。


根据评论,上面的方法确实有效,但你确实想要一个完整的外部连接,而 SQLite 不喜欢这样。不过,您可以尝试用两个 LEFT OUTER JOIN 和一个 UNION 来伪造它:

select b.col1, b.col2, a.col2
from ... as a left outer join ... as b ...
UNION
select b.col1, b.col2, a.col2
from ... as b left outer join ... as a ...

Try this:

select b.col1, b.col2, a.col2
from (
    select Col3, count(some_col) as Col2
    from Table
    where another_col = 'SomeOTHERCondition'
    group by Col3
) as a
left outer join (
    select Col1, count(some_col) as Col2
    from Table
    where another_col = 'SomeCondition'
    group by Col1
) as b
on a.col3 = b.col1

I'm not sure if SQLite supports that though.


Based on the comments the above does work but you really want a FULL OUTER JOIN and SQLite doesn't like that. You could try faking it with two LEFT OUTER JOINs and a UNION though:

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