流分析-如何使用参考数据进行完全外部联接?

时间:2019-01-24 00:46:40

标签: sql azure tsql join stream-analytics

我正在使用Stream Analytics的T-SQL对流输入和参考数据输入进行完全外部联接。

我尝试了以下查询,但始终收到错误消息:“在连接流和参考数据时,参考数据必须在右侧”。

我想对两者进行完全外部连接-如果参考数据永远不会在左侧,该怎么办?

With leftJoin AS (
    SELECT SA.[InputID], RD.[InputID]
    FROM StreamData AS SA
    LEFT JOIN ReferenceData AS RD
    ON SA.[InputID] = RD.[InputID]
),

rightJoin AS (
    SELECT SA.[InputID], RD.[InputID]
    FROM ReferenceData AS RD -- Causes the error 'cause it should be on the right
    LEFT JOIN StreamData AS SA
    ON SA.[InputID] = RD.[InputID]
)

SELECT *
INTO [StorageTable]
FROM leftJoin 
UNION 
SELECT *
FROM rightJoin 

我的预期输出是这样的:


SA.[InputID]   |  RD.[InputID]
------------------------------
1              |            1
2              |         null
null           |            3
4              |            4

2 个答案:

答案 0 :(得分:0)

这是注释部分不适合的注释。

要点是,任何表上的所有列名称必须始终具有唯一名称。一个表上不能有两个具有相同名称的列。

运行SELECT INTO时,实际上是在创建一个新表。所创建的表的每一列应具有不同的名称。否则,数据库引擎将拒绝创建该表。

我当时想您应该尝试给第二栏起一个不同的名字。例如:

With leftJoin AS (
    SELECT
      SA.[InputID], 
      RD.[InputID] as rd_inputid -- added an alias to the column
    FROM StreamData AS SA
    LEFT JOIN ReferenceData AS RD
    ON SA.[InputID] = RD.[InputID]
),    
rightJoin AS (
    SELECT
      SA.[InputID], 
      RD.[InputID] as rd_inputid -- added an alias to the column
    FROM ReferenceData AS RD
    LEFT JOIN StreamData AS SA
    ON SA.[InputID] = RD.[InputID]
)    
SELECT *
INTO [StorageTable]
FROM leftJoin 
UNION 
SELECT *
FROM rightJoin 

希望对您有帮助。

答案 1 :(得分:0)

样本数据可能会有所帮助,但是我认为根据您的输出,需要完全外部联接,并且应该提供输出。

 SELECT distinct SA.[InputID] as InputIDfromSA, RD.[InputID] as InputIDfromRD
 FROM ReferenceData AS RD  
 Full outer JOIN StreamData AS SA
 ON SA.[InputID] = RD.[InputID]