即使存在值,外连接也会返回NULL

时间:2015-01-10 19:55:10

标签: null sql-server-2012 outer-join

我试图创建一个表格,在那里我可以看到机场代码及其相应的国家和地区等。

我编制了一张包含所有机场代码,国家和地区的表格;但是我似乎得到了一些空值(即使我知道它们包含在连接表中。

例如机场代码' HKG'将拉动香港'在一行中然后突然又在另一行(也就是' HKG')它加入国家而不是香港......

非常感谢任何帮助

这是我的加入:

     FROM
      [dbo].[GS_TB_2G] [Data]
             Left Outer JOIN [dbo].[Gateway_Detail] [OrigC]
                ON [Data].[OrigGateway] = [OrigC].[code]
             Left Outer JOIN [dbo].[Gateway_Detail] [DestC]
                ON [Data].[DestGateway] = [DestC].[code]
            --Left Outer JOIN [dbo].[AWB_SECT_INFO] [Carrier]
            --  ON [Data].[awb_seq] = [Carrier].[awb_seq]

完整查询见下文

        set @report_yr = 2014 --Enter the Year you wish to run the report for
        set @report_Mth = 12  --Enter the Month you wish to run the report for

        SELECT
         Table1.[awb_prefix],
         Table1.[Origin],
         Table1.[Origin_2],
         Table1.[Origin_3],
         [Origin_4] = 'WW',
         Table1.[Destination],
         Table1.[Destination_2],
         Table1.[Destination_3],
         [Destination_4] = 'WW',
         Table1.[Mth],
         Table1.[yr],
         Table1.[Customer],
         sum (Table1.[Gross_Rev]) as [Gr Revenue],
         sum (Table1.[CHW]) as [Ch Weight]

        FROM (
         SELECT
          CASE
            WHEN [Data].[OrigGateway] IS NULL THEN [Data].[overall_orig] ELSE [Data].[OrigGateway]
          END AS [Origin],
          [OrigC].[country] AS [origin_2],
          [OrigC].[region] AS [origin_3],
          CASE
            WHEN [Data].[DestGateway] IS NULL THEN [Data].[overall_dest] ELSE [Data].[DestGateway]
          END AS [Destination],
          [DestC].[country] AS [Destination_2],
          CASE 
            WHEN [DestC].[region] IS NULL THEN 'OTHER' ELSE [DestC].[region] 
          END AS [destination_3],
          [Data].[Mth],
          [Data].[yr],
          [Data].[Forwarder] AS [Customer],
          CASE
            WHEN [Data].[Cal_Gross_Revenue] <0 THEN 0 ELSE [Data].[Cal_Gross_Revenue]
          END AS [Gross_Rev],
          [Data].[charge_weight] AS [CHW],
          CASE
            WHEN LEFT([Data].[AirWayBill] , 3) in ('403','369') THEN LEFT([Data].[AirWayBill],3) ELSE 'OTHER'
          END AS [awb_prefix]

         FROM
          [dbo].[GS_TB_2G] [Data]
                 Left Outer JOIN [dbo].[Gateway_Detail] [OrigC]
                    ON [Data].[OrigGateway] = [OrigC].[code]
                 Left Outer JOIN [dbo].[Gateway_Detail] [DestC]
                    ON [Data].[DestGateway] = [DestC].[code]
                --Left Outer JOIN [dbo].[AWB_SECT_INFO] [Carrier]
                --  ON [Data].[awb_seq] = [Carrier].[awb_seq]
         WHERE 
            [IATA_code] NOT IN ('0508634','0514616')
            AND (
                    ([mth] between 1 and @report_Mth AND [yr] = @report_yr)
                    OR ((@report_Mth < 4) AND [Mth] between 9+@report_Mth and 12 AND [yr] = @report_yr-1)
                )
            --AND (
            --      ([Carrier].[carrier] in ('PO','3S','9S','K4','5Y','Y8') AND LEFT([Data].[AirWayBill] , 3) in ('403','369'))
            --      OR ([Carrier].[carrier] in ('PO'))
            --  )
        ) AS Table1

        Group By
         [awb_prefix],
         [Customer],
         [yr],
         [Mth],
         [Origin],
         [Origin_2],
         [origin_3],
         [destination],
         [destination_2],
         [destination_3]

1 个答案:

答案 0 :(得分:0)

我对您的数据了解不多,所以请尝试用以下内容替换您的JOIN,看看是否有效?

FROM
      [dbo].[GS_TB_2G] [Data]
             Left Outer JOIN [dbo].[Gateway_Detail] [OrigC]
                ON RTRIM(LTRIM(CAST(REPLACE([Data].[OrigGateway] COLLATE Latin1_General_BIN, CHAR(0), '') AS VARCHAR(100)))) = RTRIM(LTRIM(CAST(REPLACE([OrigC].[code] COLLATE Latin1_General_BIN, CHAR(0), '') AS VARCHAR(100))))
             Left Outer JOIN [dbo].[Gateway_Detail] [DestC]
                ON RTRIM(LTRIM(CAST(REPLACE([Data].[DestGateway] COLLATE Latin1_General_BIN, CHAR(0), '') AS VARCHAR(100)))) = RTRIM(LTRIM(CAST(REPLACE([DestC].[code] COLLATE Latin1_General_BIN, CHAR(0), '') AS VARCHAR(100))))
            --Left Outer JOIN [dbo].[AWB_SECT_INFO] [Carrier]
            --  ON [Data].[awb_seq] = [Carrier].[awb_seq]