将多个选择语句添加到SQL中的表

发布于 2025-01-25 19:57:20 字数 257 浏览 5 评论 0原文

我有2个单独的选择语句,我想在SQL的结果表中将它们添加为2个单独的列,以便我可以比较它们。他们俩都是从同一张桌子中选出的,所以我只是想知道如何处理。

select distinct(varA) from table1 
        where cond = '0.0000' 

select distinct(varA) from table1 
        where cond > '0.0000' 
order by varA

I have 2 separate select statements and I wanted to add them as 2 separate columns in the results table in SQL so I can compare them. They're both being selected from the same table so I just wanted to know how to go about it.

select distinct(varA) from table1 
        where cond = '0.0000' 

select distinct(varA) from table1 
        where cond > '0.0000' 
order by varA

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

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

发布评论

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

评论(1

深巷少女 2025-02-01 19:57:20

因此,如果您的VARA值为“ A”,为什么您要使用A和A的两个列?您不想比较实际的“ cond”值?无论如何,下面的两个解决方案:

创建表和插入值:

    create table table1 (
      varA varchar(10), 
      cond numeric
    );

    insert into table1 values 
    ('a', 0.0000), 
    ('b', 0.0000), 
    ('c', 0.02), 
    ('a', 0.4), 
    ('c', 0.0000);

查询如果只想返回vara值,这在我看来是完全无济于事的。如果您的DBM不支持通用表表达式,则将其更改为JOIN。

    with cte1 as (
      select distinct varA
      from table1
      where cond = 0.0000
    )
    select distinct c.varA as zero_var, 
    t.varA as over_zero_var
    from table1 t
    join cte1 c
      on t.varA = c.varA
    where t.cond > 0.0000
    order by 1,2

结果,似乎是无助的。

    zero_var    over_zero_var
    a           a
    c           c

查询如果返回COND值,这更有意义,并假设这是您真正想要的。

    with cte1 as (
      select distinct varA, cond
      from table1
      where cond = 0.0000
    )
    select distinct c.varA as varA, 
    c.cond, t.cond
    from table1 t
    join cte1 c
      on t.varA = c.varA
    where t.cond > 0.0000
    order by 1,2

结果:

    vara    cond    cond
    a       0.0000  0.4
    c       0.0000  0.02

So if your varA value is "A", why would you want two columns with A and A? Wouldn't you rather compare the actual "cond" values? Regardless, both solutions below:

Create table and insert values:

    create table table1 (
      varA varchar(10), 
      cond numeric
    );

    insert into table1 values 
    ('a', 0.0000), 
    ('b', 0.0000), 
    ('c', 0.02), 
    ('a', 0.4), 
    ('c', 0.0000);

Query if wanting to return just the varA values, which is completely unhelpful in my opinion. If your DBMS does not support Common Table Expression, then change that to a join.

    with cte1 as (
      select distinct varA
      from table1
      where cond = 0.0000
    )
    select distinct c.varA as zero_var, 
    t.varA as over_zero_var
    from table1 t
    join cte1 c
      on t.varA = c.varA
    where t.cond > 0.0000
    order by 1,2

Results, again which are seemingly unhelpful.

    zero_var    over_zero_var
    a           a
    c           c

Query if returning the COND values, which makes much more sense, and assume this is what you actually want.

    with cte1 as (
      select distinct varA, cond
      from table1
      where cond = 0.0000
    )
    select distinct c.varA as varA, 
    c.cond, t.cond
    from table1 t
    join cte1 c
      on t.varA = c.varA
    where t.cond > 0.0000
    order by 1,2

Results:

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