1

我有一个类似的查询:

select table_one.x, sum(table_one.y)
(select foo from table_two where table_one.x = table_two.x) as item_description
from table_one
inner join table_two
on table_one.x = table_two.x
-- where table_2 row has various attributes
group by table_one.x

分组意味着我需要一个子选择来访问表二中的 foo 。现在,如果我想从表二中选择第二列,有没有办法在没有第二个子选择的情况下访问它?

数据库是 DB2。

编辑:连接是多对一的,即将 table_one 视为订单,table_b 视为包含项目的信息。

4

2 回答 2

2

table_two 对于每个 table_one.x 是否有多行,或者它是一个一对二的连接...如果它是一对二,那么这不是你想要的......因为你的子选择是在与您的加入条件相同

SELECT
   table_one.x, table_two.foo, sum(table_one.y)
FROM table_one
   INNER JOIN table_two
       ON table_one.x = table_two.x 
GROUP BY
   table_one.x, table_two.foo
于 2009-04-20T08:53:58.327 回答
0
SELECT  *
FROM    (
        SELECT  x, SUM(y)
        FROM    table_one
        GROUP BY
                x
        ) AS t1
INNER JOIN
        table_two t2
ON      t2.x = t1.x
-- where table_2 row has various attributes
于 2009-04-20T08:58:07.100 回答