左连接语句没有“客户”列

时间:2021-07-09 01:56:40

标签: sql sql-server database sql-server-2008

我正在尝试在右侧附加一列

SELECT
  'abc' as client,
  sum(nested.freq) as freq,
FROM
  (
    SELECT
      uh.route
      COUNT(uh.route) as freq
    FROM
      employee AS up,
      hist AS uh
    where
      up.id = uh.eID
      AND uh.PrhEEBankRoute = '123'
    GROUP BY
      uh.route
  ) AS nested
  LEFT JOIN (
    SELECT
      'abc' as client,
      sum(raw.freq) as total_trans
    FROM
      (
        SELECT
          uh.route,
          COUNT(uh.route) as freq
        FROM
        employee AS up,
        hist AS uh

        where
          up.id = uh.eID
        GROUP BY
          uh.route
      ) AS raw
  ) raw2 ON raw2.client = nested.client;

预期的结果是这样的

client | freq | total_trans
abc   | 2  | 100

但我收到以下错误:

<块引用>

left join 语句没有列 'client'

1 个答案:

答案 0 :(得分:1)

第一个子查询,别名为“嵌套”是:

SELECT
       uh.route                 --<< no column called "client" 
     , COUNT(uh.route) AS freq  --<< no column called "client" 
FROM employee AS up
    , hist AS uh
WHERE up.id = uh.eID
    AND uh.PrhEEBankRoute = '123'
GROUP BY uh.route

在连接条件的下一个子查询中,您引用了nested.client

) raw2 ON raw2.client = nested.client;

嵌套子查询中不存在该列,因此错误信息是准确的。

相关问题