Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在子查询中使用主查询参数聚合的多联接表_Sql_Join_Where_Aggregate - Fatal编程技术网

Sql 在子查询中使用主查询参数聚合的多联接表

Sql 在子查询中使用主查询参数聚合的多联接表,sql,join,where,aggregate,Sql,Join,Where,Aggregate,快速简单的问题 假设我正在连接两张桌子。我有主查询和子查询。子查询为我的resultset提取一个额外的列LEFT JOIN说明如果表b中没有匹配的列,我仍然希望从表a中获取所有列 select a.*, b.sumb from ta a left join (select b.uid, sum(b.amount) as sumb from tb b group by b.uid) b on a.uid = b.uid wher

快速简单的问题

假设我正在连接两张桌子。我有主查询和子查询。子查询为我的resultset提取一个额外的列
LEFT JOIN
说明如果
表b中没有匹配的列,我仍然希望从
表a
中获取所有列

select 
    a.*, b.sumb
from 
    ta a 
left join
    (select 
        b.uid, sum(b.amount) as sumb
     from tb b
     group by b.uid) b on a.uid = b.uid
where 
    a.eid = 'value';
效果很好。问题我需要限制内部查询根据每<代码>年分组求和的结果列表。否则,查询将只对所有内容求和

诸如此类:

select 
    a.*, b.sumb
from 
    ta a 
left join
    (select
        b.uid, sum(b.amount) as sumb
     from tb b
     where b.year = a.year
     group by b.uid) b on a.uid = b.uid
where 
    a.eid = 'value';
不幸的是,这个
where
子句抛出了一个错误

无法绑定多部分标识符“a.year”


有专门知识的人能给我指出正确的方向吗?

你想要一个额外的
加入
分组方式列:

select a.*, b.sumb
from ta a left join
     (select b.uid, b.year, sum(b.amount) as sumb
      from tb b
      group by b.uid, b.year
     ) b
     on a.uid = b.uid and a.year = b.year
where a.eid = 'value';

假设一年中有一年,他的代码不应该工作吗?或者你必须通过外部应用或等效的方法来做类似的事情吗?谢谢。您必须使用
外部应用
/
交叉应用
。在
from
子句中,不能将相关子查询作为主子查询。谢谢。我想我从来没有试过,但现在我知道了。再来一次。我希望这对其他人也有用。