使用 XOR 语句连接子句

Join Clause With a XOR Statement

我正在做一个连接,我似乎无法使这个 XOR 正常工作。

SELECT t1.COMPANY, t1.MILES,
   CASE WHEN t2.MILES IS NULL THEN t3.MILES
        ELSE t2.MILES
   END AS MILES2,
   CASE WHEN t2.MILES = t1.MILES AND t2.MILES != 9999 THEN t2.FLATRATE
        ELSE t3.RATEBASIS
   END AS RATE           
FROM TABLE1 AS t1
    LEFT JOIN TABLE2 AS t2 
        ON t1.[COMPANY] = t2.[COMPANYCODE] AND (t1.[MILES] = t2.[MILES])
    INNER JOIN (
         SELECT TOP 1 TRUCKERCODE, MILES, RATEBASIS, FLATRATE FROM TABLE2 WHERE MILES = 9999
               ) AS t3 
   ON t1.[COMPANY] = t3.[COMPANYCODE]

如果英里数相同,我需要加入 ON 子句,然后获取匹配的给定字段,否则我需要从第二个 table 中获取的默认数据是英里数等于 9999 的地方. 现在有了那个 ON 子句,我得到了许多额外的行,其中 MILES 等于 45,它从 TABLE2 中获取数据,其中英里等于 45,所有数据中英里等于 9999。我需要它来做一个或另一个但不是两个都。这就是我的 table 的样子

TABLE1                          TABLE2
ID COMPANY MILES ETC            ID COMPANYCODE MILES RATE
1  ILLINI  50                   1  ILLINI      50    3.2
2  ILLINI  110                  2  ILLINI      110   5.2
3  ILLINI  150                  3  ILLINI      150   2.4
4  ILLINI  200                  4  ILLINI      200   1.9
5  ILLINI  250                  5  ILLINI      9999  1.5
6  ILLINI  300                       
7  ILLINI  350
8  ILLINI  400
9  ILLINI  450
10 ILLINI  500

Desired Output
COMPANY MILES MILES2 RATE
ILLINI  50    50    3.2
ILLINI  110   110   5.2
ILLINI  150   150   2.4
ILLINI  200   200   1.9
ILLINI  250   9999  1.5
ILLINI  300   9999  1.5                    
ILLINI  350   9999  1.5
ILLINI  400   9999  1.5
ILLINI  450   9999  1.5
ILLINI  500   9999  1.5

我想这会给你想要的:

SELECT t1.COMPANY, t1.MILES,
       CASE WHEN t2.MILES IS NULL THEN t3.MILES
            ELSE t2.MILES
       END AS MILES2,
       CASE WHEN t2.MILES IS NULL THEN t3.RATE
            ELSE t2.RATE
       END AS RATE           
FROM TABLE1 AS t1
LEFT JOIN TABLE2 AS t2 
   ON t1.[COMPANY] = t2.[COMPANYCODE] AND (t1.[MILES] = t2.[MILES])
INNER JOIN (
   SELECT TOP 1 COMPANYCODE, MILES, RATE FROM TABLE2 WHERE MILES = 9999
) AS t3 
   ON t1.[COMPANY] = t3.[COMPANYCODE]    

如果 MILES 中有匹配项,则输出包含 TABLE1TABLE2 中的两条记录。否则,输出包含来自左侧 table 的记录,即 TABLE1 和来自 TABLE2specific 记录和 MILES = 9999.

请注意,在最后一个子查询中使用 TOP 1 以防万一 多个记录 MILES = 9999 存在于 TABLE2.

输出:

COMPANY MILES   MILES2  RATE
----------------------------
ILLINI  50      50      3,2
ILLINI  110     110     5,2
ILLINI  150     150     2,4
ILLINI  200     200     1,9
ILLINI  250     9999    1,5
ILLINI  300     9999    1,5
ILLINI  350     9999    1,5
ILLINI  400     9999    1,5
ILLINI  450     9999    1,5
ILLINI  500     9999    1,5

SQL Fiddle Demo here

我会基于双左连接..一次匹配,如果没有,则默认为 9999 实例

SELECT
      T1.*,
      coalesce( T2.miles, T3.miles ) as Miles2,
      coalesce( T2.rate, T3.rate ) as MileageRate
   FROM
      TABLE1 T1
         LEFT JOIN TABLE2 T2
            on T1.Company = T2.CompanyCode
           AND T1.Miles = T2.Miles
         LEFT JOIN TABLE2 T3
            on T1.Company = T3.CompanyCode
           AND T3.Miles = 9999

确认工作at this SQL Fiddle link