两个查询缺少值的联接

时间:2020-08-14 14:04:45

标签: sql sql-server

我需要帮助加入这两个查询。这是带有查询的第一个数据集:

Terminal    Inbound Shipments   Inbound Revenue
Albany            21               2785.51
Austin             7               1115.23

   SELECT

   t.[Origin Terminal Name] as 'Terminal',

   COUNT(t.[Pro Number]) as 'Inbound Shipments',

   SUM(t.[Total Revenue]) as 'Inbound Revenue'


 
  FROM [AX2cTest].[dbo].[AAATRANSPORTTABLE_V] t

  where t.[Initial Rated Date/Time] >= '2020-03-19 02:00:00.000' and  t.[Initial Rated Date/Time] <= '2020-03-20 01:59:00.000'

  GROUP BY t.[Origin Terminal Name]

  ORDER BY t.[Origin Terminal Name] ASC

第二:

Terminal    Outbound Shipments  Outbound Revenue
Abilene              4             1609.53
Albany              12             4215.04
Asheville           15             2258.05
Augusta             10             1592.53
Austin               4             1198.11

SELECT
   
       t.[Destination Terminal Name] as 'Terminal',

       COUNT(t.[Pro Number]) as 'Outbound Shipments',
    
       SUM(t.[Total Revenue]) as 'Outbound Revenue'
     
  FROM [AX2cTest].[dbo].[AAATRANSPORTTABLE_V] t

  where t.[Initial Rated Date/Time] >= '2020-03-19 02:00:00.000' and  t.[Initial Rated Date/Time] <= '2020-03-20 01:59:00.000'

  GROUP BY t.[Destination Terminal Name]

  Order by t.[Destination Terminal Name]

我的查询产生以下结果:

Terminal    Inbound Shipments   Inbound Revenue Outbound Shipments  Outbound Revenue
Albany            21                2785.51          12                4215.04
Austin             7                1115.23           4                1198.11

我需要返回为

Terminal    Inbound Shipments   Inbound Revenue Outbound Shipments  Outbound Revenue
Abilene            0                  0                 4               1609.53
Albany            21                2785.51             12             4215.04
Asheville          0                   0                15             2258.05
Augusta            0                   0                10             1592.53
Austin             7                 1115.23             4             1198.11

我不知道为什么我的查询遗漏了没有入站数据的记录。我相信正确的加入是正确的。使用正确的Terminal字段似乎是一个问题。我认为第二双眼睛会有所帮助。

   SELECT
   
       t2.Terminal as 'Terminal',

       COUNT(t.[Pro Number]) as 'Inbound Shipments',
    
       SUM(t.[Total Revenue]) as 'Inbound Revenue',

       max(t2.[Outbound Shipments]) as 'Outbound Shipments',

       max(t2.[Outbound Revenue]) as 'Outbound Revenue'

     
  FROM [AX2cTest].[dbo].[AAATRANSPORTTABLE_V] t

  RIGHT JOIN  (SELECT
   
       t2.[Destination Terminal Name] as 'Terminal',

       COUNT(t2.[Pro Number]) as 'Outbound Shipments',
    
       SUM(t2.[Total Revenue]) as 'Outbound Revenue'
     
  FROM [AX2cTest].[dbo].[AAATRANSPORTTABLE_V] t2

  where t2.[Initial Rated Date/Time] >= '2020-03-19 02:00:00.000' and  t2.[Initial Rated Date/Time] <= '2020-03-20 01:59:00.000'

  GROUP BY t2.[Destination Terminal Name]) t2 on t.[Origin Terminal Name] = t2.Terminal

  where t.[Initial Rated Date/Time] >= '2020-03-19 02:00:00.000' and  t.[Initial Rated Date/Time] <= '2020-03-20 01:59:00.000'

  GROUP BY t2.Terminal

  ORDER BY t2.Terminal ASC

1 个答案:

答案 0 :(得分:2)

您似乎想要:

select d2.terminal,
       coalesce(d1.inbound_shipments, 0), coalesce(d1.inbound_revenue, 0) as outbound_revenue,
       coalesce(d2.outbound_shipments, 0), coalesce(d2.outbound_revenue, 0) as outbound_revenue      
from dataset2 d2 left join
     dataset1 d1 
     on d2.terminal = d1.terminal ;

您的问题可能是将where条件合并到外部联接中。我强烈建议使用LEFT JOIN first 表上的条件在WHERE子句中。 second (及后续表)的条件放在ON子句中。

我不确定您的代码与您所说的问题有什么关系。