如何使用 Microsoft SQL 服务器完成两个 Select 语句的完全外部联接?

How do I complete a Full Outer Join on two Select Statements with Microsoft SQL Server?

我正在尝试在 Microsoft SQL Server 2014 上完全外部加入这两个 Select 语句,以便我有一个 table,其中包括日期、主要票证和次要票证。它说在 Outer Join 子句中 "on" 附近存在语法问题。请问有什么办法可以解决这个问题吗?

Select #mytable7.Date,#mytable9.Date, #mytable7.[Major Tickets],#mytable9.[Minor Tickets] 
from 
     (select convert(VARCHAR, EventDate1, 112) as Date, Count(FltNo1) as [Major Tickets] 
      from TicketCoupons
      Where PaxNo='1' 
            and EventDepart = 'DET' 
            and EventDate1 >= '20160601' 
            and EventDate1 <= '20160709'
      group by convert(VARCHAR, EventDate1, 112)) as #mytable7

      Full Outer Join

      (Select Date, [Minor Tickets] 
       from 
            (select convert(VARCHAR, EventDate1, 112) as Date, Count(FltNo1) as [Minor Tickets] 
             from TicketCoupons
             Where PaxNo='1' 
                   and EventArrive = 'DET' 
                   and EventDate1 >= '20160601' 
                   and EventDate1 <= '20160709'
             group by convert(VARCHAR, EventDate1, 112)) as #mytable9 
       on #mytable7.Date = #mytable9.Date
       order by #mytable7.Date

我是 SQL 的新手,所以如果这完全关闭或不可能,我提前道歉。

Full Outer Join 子查询中缺少括号:

(Select Date, [Minor Tickets] 
       from 
            (select convert(VARCHAR, EventDate1, 112) as Date, Count(FltNo1) as [Minor Tickets] 
             from TicketCoupons
             Where PaxNo='1' 
                   and EventArrive = 'DET' 
                   and EventDate1 >= '20160601' 
                   and EventDate1 <= '20160709'
             group by convert(VARCHAR, EventDate1, 112))  <---- here !!
 ) as #mytable9 
 on #mytable7.Date = #mytable9.Date

因为 ON 引用了 mytable7 和 mytable9

正如 Jeroen Mostert 指出的那样,这个查询可以简单地重写为:

Full Outer Join

        (select convert(VARCHAR, EventDate1, 112) as Date, Count(FltNo1) as [Minor Tickets] 
         from TicketCoupons
         Where PaxNo='1' 
               and EventArrive = 'DET' 
               and EventDate1 >= '20160601' 
               and EventDate1 <= '20160709'
         group by convert(VARCHAR, EventDate1, 112)) as #mytable9 
   on #mytable7.Date = #mytable9.Date
   order by #mytable7.Date

试试这个:

Select mytable7.Date,mytable9.Date, mytable7.[Major Tickets],mytable9.[Minor Tickets] 
from 
(
    select convert(VARCHAR, EventDate1, 112) as Date, Count(FltNo1) as [Major Tickets] 
    from TicketCoupons
    Where PaxNo='1' 
        and EventDepart = 'DET' 
        and EventDate1 >= '20160601' 
        and EventDate1 <= '20160709'
    group by convert(VARCHAR, EventDate1, 112)
) as mytable7

Full Outer Join

(
    Select Date, [Minor Tickets] 
    from 
    (
        select convert(VARCHAR, EventDate1, 112) as Date, Count(FltNo1) as [Minor Tickets] 
        from TicketCoupons
        Where PaxNo='1' 
            and EventArrive = 'DET' 
            and EventDate1 >= '20160601' 
            and EventDate1 <= '20160709'
        group by convert(VARCHAR, EventDate1, 112)
    ) as mytable9a
) as mytable9
on mytable7.Date = mytable9.Date
order by mytable7.Date

有两个问题..

  1. 使用 # 作为别名
  2. 不匹配的括号(所以现在你有一个 9a 和一个 9 table)

此外,我发现使用 WITH 编写查询更容易 - 我发现它使我的代码更易于阅读和调试。看看这个:

;With MyTable7 As
(
    select convert(VARCHAR, EventDate1, 112) as Date, Count(FltNo1) as [Major Tickets] 
    from TicketCoupons
    Where PaxNo='1' 
        and EventDepart = 'DET' 
        and EventDate1 >= '20160601' 
        and EventDate1 <= '20160709'
    group by convert(VARCHAR, EventDate1, 112)
),
MyTable9 As
(
    Select Date, [Minor Tickets] 
    from 
    (
        select convert(VARCHAR, EventDate1, 112) as Date, Count(FltNo1) as [Minor Tickets] 
        from TicketCoupons
        Where PaxNo='1' 
            and EventArrive = 'DET' 
            and EventDate1 >= '20160601' 
            and EventDate1 <= '20160709'
        group by convert(VARCHAR, EventDate1, 112)
    ) as MyTable9a
)
Select MyTable7.Date,MyTable9.Date, MyTable7.[Major Tickets],MyTable9.[Minor Tickets] 
From MyTable7
Full Outer Join MyTable9
    ON MyTable7.Date = MyTable9.Date
Order by MyTable7.Date