在交叉联接中需要帮助

时间:2018-08-08 07:03:35

标签: sql-server select subquery

UITabBarController

我遇到这些错误:

  

第156条消息,第15级,状态1,第4行
  关键字“ CROSS”附近的语法不正确。

     

第156条消息,第15级,状态1,第6行
  关键字“ CROSS”附近的语法不正确。

     

第156条消息,第15级,状态1,第8行
  关键字“ CROSS”附近的语法不正确。

     

第156条消息,第15级,状态1,第10行
  关键字“ CROSS”附近的语法不正确。

     

第15级州立1行12的消息102
  ')'附近的语法不正确。

2 个答案:

答案 0 :(得分:0)

子查询在括号中。大多数DBMS也要求使用它们的名称:

SELECT * 
FROM
  (SELECT SUM(CASE WHEN LOCAL_CCY_AMT < 0 THEN LOCAL_CCY_AMT END) AS DR
   FROM INSIGHTSOURCE.BS.RE_CRF_GL WHERE CONSOL_KEY10 = 'CUR' GROUP BY Consol_Key10) q1
CROSS JOIN
  (SELECT SUM(CASE WHEN LOCAL_CCY_AMT < 0 THEN LOCAL_CCY_AMT END) AS DR
   FROM INSIGHTSOURCE.BS.RE_CRF_GL WHERE CONSOL_KEY10 IN ('REG','WCL') GROUP BY Consol_Key10) q2
CROSS JOIN
  (SELECT SUM(CASE WHEN LOCAL_CCY_AMT < 0 THEN LOCAL_CCY_AMT END) AS DR
   FROM INSIGHTSOURCE.BS.RE_CRF_GL WHERE CONSOL_KEY10 IN ('OAM','SUB') GROUP BY Consol_Key10) q3
CROSS JOIN
  (SELECT SUM(CASE WHEN LOCAL_CCY_AMT < 0 THEN LOCAL_CCY_AMT END) AS DR
   FROM INSIGHTSOURCE.BS.RE_CRF_GL WHERE CONSOL_KEY10 IN ('DBT','LOS') GROUP BY Consol_Key10) q4
CROSS JOIN
  (SELECT SUM(CASE WHEN LOCAL_CCY_AMT < 0 THEN LOCAL_CCY_AMT END) AS DR
   FROM INSIGHTSOURCE.BS.RE_CRF_GL WHERE CONSOL_KEY10 IN ('CHO') GROUP BY Consol_Key10) q5;

答案 1 :(得分:0)

为每个子查询使用别名和括号。

SELECT * FROM (
(SELECT SUM(CASE WHEN LOCAL_CCY_AMT < 0 THEN LOCAL_CCY_AMT END) AS DR FROM INSIGHTSOURCE.BS.RE_CRF_GL WHERE CONSOL_KEY10 = 'CUR' GROUP BY Consol_Key10) AS t1
CROSS JOIN
(SELECT SUM(CASE WHEN LOCAL_CCY_AMT < 0 THEN LOCAL_CCY_AMT END) AS DR FROM INSIGHTSOURCE.BS.RE_CRF_GL WHERE CONSOL_KEY10 IN ('REG','WCL') GROUP BY Consol_Key10) AS t2
CROSS JOIN
(SELECT SUM(CASE WHEN LOCAL_CCY_AMT < 0 THEN LOCAL_CCY_AMT END) AS DR FROM INSIGHTSOURCE.BS.RE_CRF_GL WHERE CONSOL_KEY10 IN ('OAM','SUB') GROUP BY Consol_Key10) AS t3
CROSS JOIN
(SELECT SUM(CASE WHEN LOCAL_CCY_AMT < 0 THEN LOCAL_CCY_AMT END) AS DR FROM INSIGHTSOURCE.BS.RE_CRF_GL WHERE CONSOL_KEY10 IN ('DBT','LOS') GROUP BY Consol_Key10) AS t4
CROSS JOIN
(SELECT SUM(CASE WHEN LOCAL_CCY_AMT < 0 THEN LOCAL_CCY_AMT END) AS DR FROM INSIGHTSOURCE.BS.RE_CRF_GL WHERE CONSOL_KEY10 IN ('CHO') GROUP BY Consol_Key10) AS t5
)