2

我已经声明了一个表变量“@t”,并正确执行了“INSERT-INTO-SELECT”。当我尝试通过一些额外的计算来查询表变量以进行每组行编号时,直接使用“@t”时出现“必须声明变量”或使用“@t”别名时出现“无效的对象名称”错误'。请多多指教。

SELECT 
    *,
    (SELECT COUNT(*) FROM "LTV" "COUNTER"
     WHERE 
        "COUNTER"."Collateral_ID" = "LTV"."Collateral_ID"
        AND
        "COUNTER"."m_il_no" = "LTV"."m_il_no"
        AND
        "COUNTER"."Ref_Key" <= "LTV"."Ref_Key"
     GROUP BY "COUNTER"."Collateral_ID", "COUNTER"."m_il_no"
    ) "MIL_IDX"

FROM @t AS LTV
4

1 回答 1

5

采用:

SELECT x.*,
       y.num
  FROM @t x
  JOIN (SELECT t.collateral_id,
               t.m_il_no,
               COUNT(*) AS num
          FROM @t t
      GROUP BY t.collateral_id, t.m_il_no) y ON y.collateral_id = x.collateral_id
                                            AND y.m_il_no = x.m_il_no
于 2010-11-23T03:18:37.860 回答