SQL Server 2016虚拟表-不正确的语法'AS'

时间:2019-01-25 03:19:26

标签: sql-server

假设表level包含列level_idlevel_name

我在SQL Server 2016查询中有一个虚拟表:

这不是一个真正的查询-我已经尽可能简化了它,以说明错误

SELECT
    LEVEL .level_id,
    LEVEL .level_name
FROM
    LEVEL,
    ((SELECT
          LEVEL_ID AS lev_sum_level_id
      FROM
          LEVEL
      GROUP BY
          level_id) AS lev_sum
     JOIN
         (SELECT
              LEVEL_ID AS lev_det_level_id
          FROM
              LEVEL
          GROUP BY
              level_id) AS lev_det ON (lev_sum_level_id = lev_det_level_id)
  ) AS totals

语法错误位于AS totals行。

  

第15层状态1的第35行的消息156
  关键字“ AS”附近的语法不正确

为什么SQL Server不允许这种语法?如果我简化虚拟表查询,似乎很好。 Postgres允许它原样

1 个答案:

答案 0 :(得分:1)

我认为您可以尝试添加选择,以便您可以完成语句的语法

请尝试

SELECT
T1.MiscID,
T2.lev_sum_level_id
FROM
Miscs AS T1,
(
    SELECT * FROM
    (
        SELECT
            MiscID AS lev_sum_level_id
        FROM
            Miscs
        GROUP BY
            MiscID
    ) AS lev_sum
    JOIN
    (
        SELECT
            MiscID AS lev_det_level_id
        FROM
            Miscs
        GROUP BY
            MiscID
    ) AS lev_det 
    ON( lev_sum_level_id = lev_det_level_id )
) AS T2