我的代码有什么问题(子查询--sql server)

时间:2017-09-07 05:25:19

标签: sql sql-server

我在')'附近的语法不正确,c有人建议我做错了什么?我想让自己熟悉子查询并提前感谢!

附件是我写的sql,并试图在这里修改SQL script

(
  SELECT 
    APP_ID,
    MAX(CASE WHEN CUST_TYPE_ORD_NUM = 0 THEN CUST_APP_ID END) AS PRI_CUST_APP_ID,
    MAX(CASE WHEN CUST_TYPE_ORD_NUM = 1 THEN CUST_APP_ID END) AS SEC_CUST_APP_ID,
    .... 
    ....
  FROM 
  (
    SELECT 
      APP_ID,
      CUST.CUST_ID,
      CUST_TYPE_ORD_NUM
      .. 
    FROM CDM_CUST_APP_MTRX CUST_APP_MTRX 
    LEFT JOIN CDM_CUST CUST ON CUST_APP_MTRX.CUST_ID = CUST.CUST_ID
  ) CUST_MTRX
  GROUP BY APP_ID 
) ABC 
LEFT JOIN CDM_CUST_ADR_DETL CUST_ADR_DETL
       ON ABC.PRI_CUST_APP_ID = CUST_ADR_DETL.CUST_APP_ID 
LEFT JOIN CDM_CUST_ADR_DETL CUST_ADR_DETL2
       ON ABC.PRI_CUST_APP_ID = CUST_ADR_DETL2.CUST_APP_ID

1 个答案:

答案 0 :(得分:1)

这是一个语法问题。在查询的第一行中的第一个括号之前再写一个SELECT * FROM。

SELECT * FROM (
  SELECT 
    APP_ID,
    MAX(CASE WHEN CUST_TYPE_ORD_NUM = 0 THEN CUST_APP_ID END) AS PRI_CUST_APP_ID,
    MAX(CASE WHEN CUST_TYPE_ORD_NUM = 1 THEN CUST_APP_ID END) AS SEC_CUST_APP_ID
  FROM 
  (
    SELECT 
      APP_ID,
      CUST.CUST_ID,
      CUST_TYPE_ORD_NUM,
      .. 
    FROM CDM_CUST_APP_MTRX CUST_APP_MTRX 
    LEFT JOIN CDM_CUST CUST ON CUST_APP_MTRX.CUST_ID = CUST.CUST_ID
  ) CUST_MTRX
  GROUP BY APP_ID 
) ABC 
LEFT JOIN CDM_CUST_ADR_DETL CUST_ADR_DETL
       ON ABC.PRI_CUST_APP_ID = CUST_ADR_DETL.CUST_APP_ID 
LEFT JOIN CDM_CUST_ADR_DETL CUST_ADR_DETL2
       ON ABC.PRI_CUST_APP_ID = CUST_ADR_DETL2.CUST_APP_ID