在 SQL Anywhere 中使用 AS 后无法加入两个 table

Cannot join two table after using AS in SQL Anywhere

我对 SQL 比较陌生(在 SQL 任何地方)。我尝试连接两个表,但总是出错

SQL Anywhere Error -131: Syntax error near 'left' on line 17

我的左连接有没有错误?谢谢

(
SELECT 
    Text_ID, Asdate, BigUnitCode AS Big_Code, 
    (BigUnitCode +'@'+ BigUnitName) as Main_ID, Method, xt.[Comment], 
    SUM(FIvalue) AS CurrentValue 
FROM
    (SELECT * FROM dmf_dbo.main1
     WHERE Text_ID = 440168) AS xt
GROUP BY 
    Text_ID, Asdate, BigUnitCode, Method, xt.[Comment], BigUnitName
) AS gz
LEFT JOIN
    (SELECT
         (BigUnitCode +'@'+ BigUnitName) AS Main_ID, BigDep_code AS group_id, 
         BigDep_name AS group_name, SmallUnit_code AS sub_id, 
         SmallUnit_name AS sub_name 
     FROM
         dmf_dbo.main2 
     WHERE 
         ASOFDATE = '2020-07-11'
     GROUP BY 
         SmallUnit_code, SmallUnit_name) AS ho ON gz.Main_ID = ho.Main_ID

在没有看到您的数据或您的预期输出看起来像什么的情况下做起来有点棘手,所以我对您的输出不承担任何责任,但试试这个。 请记住,如果您格式化代码并使用(接近的)正确缩进,通常更容易看到发生了什么。

让我们看一下您的第一份声明。

(
SELECT 
    Text_ID
    ,Asdate
    ,BigUnitCode AS Big_Code
    ,(BigUnitCode +'@'+ BigUnitName) as Main_ID 
    ,Method
    ,xt.[Comment]
      FROM
          (
           SELECT * FROM dmf_dbo.main1
           WHERE Text_ID = 440168)
          )
) AS gz

您会注意到其中一个放错位置的圆括号(方括号)被移除,GROUP BYSUM 被移除,我们将在最后一步将它们放在最外层的查询中。这是因为您没有在第二个查询中使用 SUM,因此很难预见结果数据集在 JOIN.

之后的实际工作方式

现在我们来看第二个查询。 我不确定您为什么在这里使用 GROUP BY,因为似乎没有聚合。除非您试图删除重复项(如 DISTINCT),否则没有必要这样做。由于您没有提供上下文,我假设您不希望这样做。

  (
     SELECT
          (BigUnitCode +'@'+ BigUnitName) AS Main_ID
          ,BigDep_code AS group_id
          ,BigDep_name AS group_name
          ,SmallUnit_code AS sub_id
          ,SmallUnit_name AS sub_name 
         FROM
             dmf_dbo.main2 
         WHERE ASOFDATE = '2020-07-11'
 ) AS ho  

现在,让我们把它们放在一起。

    SELECT 
    gz.Text_ID
    ,gz.Asdate
    ,gz.Big_Code
    ,gz.Main_ID
    ,gz.Method
    ,gz.Comment 
    ,ho.Main_ID
    ,ho.group_id
    ,ho.group_name
    ,ho.sub_id
    ,ho.sub_name
    ,SUM(gz.FIvalue) AS CurrentValue 
      FROM 
          ( 
           (SELECT 
           Text_ID
           ,Asdate
           ,BigUnitCode AS Big_Code
           ,(BigUnitCode +'@'+ BigUnitName) as Main_ID 
           ,Method
           ,xt.[Comment]
             FROM
                (
                 SELECT * FROM dmf_dbo.main1
                 WHERE Text_ID = 440168)
                 )
         ) AS gz
          
           LEFT JOIN 
         
         (
           SELECT
           (BigUnitCode +'@'+ BigUnitName) AS Main_ID
           ,BigDep_code AS group_id
           ,BigDep_name AS group_name
           ,SmallUnit_code AS sub_id
           ,SmallUnit_name AS sub_name 
           FROM
           dmf_dbo.main2 
           WHERE ASOFDATE = '2020-07-11'
         ) AS ho  
        
         ON gz.Main_ID = ho.Main_ID
      
         )

GROUP BY 
  gz.Text_ID
    ,gz.Asdate
    ,gz.Big_Code
    ,gz.Main_ID
    ,gz.Method
    ,gz.Comment 
    ,ho.Main_ID
    ,ho.group_id
    ,ho.group_name
    ,ho.sub_id
    ,ho.sub_name