SQL Server中的完全外部联接问题

时间:2016-06-22 11:00:29

标签: sql sql-server tsql

当我单独使用2个以下查询时,我成功输出

查询#1

DECLARE @ID VARCHAR(1000) 
SET @ID = '' 

SELECT @ID = @ID + CAST(int_ID AS VARCHAR(1000)) 
FROM MATRIX_RELATIONSHIP 

SELECT COUNT(int_ID) [Count], YEAR(dt_5841_OF) [date] 
FROM TRANS_IDENTIFICATIONS 
WHERE smallint_209_ME = 4 
  AND CHARINDEX(CONVERT(VARCHAR, int_ID), ('''' + Replace((@ID),',',''',''') + '''')) > 0 
GROUP BY YEAR(dt_5841_OF);

输出:

        Count | year
          02    2016

查询#2

SELECT 
    COUNT(*) [Count1], YEAR(dt_modifiedOn) [date]
FROM 
    MATRIX_RELATIONSHIP 
GROUP BY 
    YEAR(dt_modifiedOn);

输出:

        Count | year
          02    2016

但是当我使用两个查询来形成如下所示的full outer join时:

SELECT 
    tab1.Count, tab2.Count1, tab1.date 
FROM
    (DECLARE @ID VARCHAR(1000) 
     SET @ID = '' 

     SELECT @ID = @ID+CAST(int_ID AS VARCHAR(1000)) 
     FROM MATRIX_RELATIONSHIP 

     SELECT COUNT(int_ID) [Count], YEAR(dt_5841_OF) [date] 
     FROM TRANS_IDENTIFICATIONS 
     WHERE smallint_209_ME = 4 
       AND CHARINDEX(convert(varchar,int_ID),  ('''' + Replace((@ID),',',''',''') + '''')) > 0 
     GROUP BY YEAR(dt_5841_OF)) tab1
FULL OUTER JOIN 
    (SELECT COUNT(*) [Count1], YEAR(dt_modifiedOn) [date]
     FROM MATRIX_RELATIONSHIP 
     GROUP BY YEAR(dt_modifiedOn)) tab2 ON tab1.date = tab2.date;

我收到这些错误:

  

Msg 156,Level 15,State 1,Line 2
  关键字' DECLARE'附近的语法不正确。

     

Msg 102,Level 15,State 1,Line 4
  ')'附近的语法不正确。

     

Msg 102,Level 15,State 1,Line 7
  “&tag;'”附近的语法不正确。

预期结果

Count | Count1 | year 
  02      02     2016

修改

我已经尝试过放置声明并设置在查询之外但仍存在问题

DECLARE  @ID VARCHAR(1000) SET @ID=''
 SELECT tab1.Count,tab2.Count1,tab1.date from
( SELECT @ID= @ID+CAST(int_ID AS VARCHAR(1000)) 
from MATRIX_RELATIONSHIP SELECT COUNT(int_ID)[Count],YEAR(dt_5841_OF)[date] FROM TRANS_IDENTIFICATIONS WHERE smallint_209_ME=4 and CHARINDEX(convert(varchar,int_ID),  ('''' + Replace((@ID),',',''',''') + '''')) > 
0 GROUP BY YEAR(dt_5841_OF)) tab1
FULL OUTER JOIN 
(SELECT COUNT(*)[Count1],YEAR(dt_modifiedOn)[date]
 FROM MATRIX_RELATIONSHIP GROUP BY YEAR(dt_modifiedOn)) tab2
 ON tab1.date=tab2.date;

导致另一组错误:

  

Msg 102,Level 15,State 1,Line 3
  ' ='附近的语法不正确。

     

Msg 102,Level 15,State 1,Line 5
  ')'附近的语法不正确。

     

Msg 102,Level 15,State 1,Line 8
  “&tag;'”附近的语法不正确。

5 个答案:

答案 0 :(得分:1)

这不是一个有效的 这是两个选择陈述

from
( DECLARE  @ID VARCHAR(1000) 
  SET @ID='' SELECT @ID= @ID+CAST(int_ID AS VARCHAR(1000)) 
             from MATRIX_RELATIONSHIP 

  SELECT COUNT(int_ID)[Count],YEAR(dt_5841_OF)[date] 
  FROM TRANS_IDENTIFICATIONS 
  WHERE smallint_209_ME=4 
  and CHARINDEX(convert(varchar,int_ID), ('''' + Replace((@ID),',',''',''') + '''')) > 0 
  GROUP BY YEAR(dt_5841_OF)
)

答案 1 :(得分:1)

    DECLARE  @ID VARCHAR(1000) 
    SET @ID='';

    SELECT @ID = @ID + CAST(int_ID AS VARCHAR(1000)) from MATRIX_RELATIONSHIP;

    SELECT MAX(Count) AS Count, MAX(Count1) AS Count1, date
    FROM
    (

        SELECT COUNT(int_ID)[Count], null AS [Count1], YEAR(dt_5841_OF)[date] 
        FROM TRANS_IDENTIFICATIONS 
        WHERE smallint_209_ME = 4 
        and CHARINDEX(convert(varchar,int_ID),  ('''' + Replace((@ID),',',''',''') + '''')) > 
        0 GROUP BY YEAR(dt_5841_OF)
        UNION ALL
        SELECT NULL AS [Count],  COUNT(*)[Count1],YEAR(dt_modifiedOn)[date]
        FROM MATRIX_RELATIONSHIP 
        GROUP BY YEAR(dt_modifiedOn)
    )AS temp 
    GROUP BY [date]

答案 2 :(得分:0)

你的DECLARE和SET必须在SELECT之外。但它的目的是什么?

答案 3 :(得分:0)

删除DECLARE @ID VARCHAR(1000);
 来自select语句的SET @ID='' 同时在语句的顶部声明变量 而是编写两个单独的select语句在int_id上使用内连接,然后执行外连接它将起作用

答案 4 :(得分:0)

DECLARE @ID VARCHAR(1000)      SET @ID =''      选择*来自      ( SELECT @ ID + CAST(MATRIX_RELATIONSHIP.int_ID AS varchar(1000))AS count1,COUNT(TRANS_IDENTIFICATIONS.int_ID)AS count2,YEAR(TRANS_IDENTIFICATIONS.dt_5841_OF)AS date1 来自MATRIX_RELATIONSHIP INNER JOIN                       MATRIX_RELATIONSHIP.int_ID = TRANS_IDENTIFICATIONS.int_ID上的TRANS_IDENTIFICATIONS WHERE(TRANS_IDENTIFICATIONS.smallint_209_ME = 4)AND(CHARINDEX(CONVERT(varchar,TRANS_IDENTIFICATIONS.int_ID),''''+ REPLACE(@ID,',',''',''')+'''')&gt ; 0) GROUP BY YEAR(TRANS_IDENTIFICATIONS.dt_5841_OF),MATRIX_RELATIONSHIP.int_ID 联盟 ( SELECT''作为count1,COUNT(*)[Count2],YEAR(dt_modifiedOn)[date1]      来自MATRIX_RELATIONSHIP      GROUP BY YEAR(dt_modifiedOn)  )  )作为tbl

我希望这适合你...让我知道