SQL: JOIN syntax error

前端 未结 2 1389
囚心锁ツ
囚心锁ツ 2021-01-28 23:45

I am attempting to eliminate unwanted duplicate query results. The gist is that the field [CUSIP] exists in all tables in question, however, the field [4DTYR] exists in all tabl

2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-29 00:23

    In the FROM phase we specify table or tables that need to be queried, and if table operators are specified, this phase processes those operators from left to right. Join (Left / Right) are table operators and each table operator operates on one or two input tables and returns an output table. The result of a table operator is used as the left input to the next table operator—if one exists—and as the input to the next logical query processing phase otherwise. In your query i find two issues:

    1. DO you want entire 'AND' condition(Line 15) to be added as a filter condition in ON clause when left joining with DATA_SP?
    2. In Last line you are trying to specify a left join on field [4DTYR] of DATA_Footnotes table which is not right as the Left join operator expects a table to be supplied.

    I think a pseudo like below might help:

    select IDX_FS.CUSIP , DATA_BS.[4DTYR] 
    FROM 
      IDX_FS
      LEFT JOIN DATA_BS 
      ON IDX_FS.CUSIP = DATA_BS.CUSIP
      LEFT JOIN DATA_Footnotes 
      ON IDX_FS.CUSIP = DATA_Footnotes.CUSIP AND DATA_BS.CUSIP = DATA_Footnotes.CUSIP  AND DATA_BS.[4DTYR] = DATA_Footnotes.[4DTYR]
      LEFT JOIN DATA_IS 
      ON IDX_FS.CUSIP = DATA_IS.CUSIP AND DATA_BS.CUSIP = DATA_IS.CUSIP AND DATA_BS.[4DTYR] = DATA_IS.[4DTYR]
      LEFT JOIN DATA_SP 
      ON IDX_FS.CUSIP = DATA_SP.CUSIP AND DATA_BS.CUSIP = DATA_SP.CUSIP AND DATA_BS.[4DTYR] = DATA_SP.[4DTYR]
      -- LEFT JOIN DATA_Footnotes.[4DTYR] no need.
    

    Hope this helps!!!

提交回复
热议问题