当我没有所需的所有列时,如何进行交叉连接?

How do I do a Cross Join when I don't have all the columns I need?

我有两个查询,它们都创建了我想交叉连接的 table。

SELECT SITA, B, C, Rooms, Datearrived, Market_segment, StayDays as RN, AMTRoom as Rev
   FROM [UKRMC].[dbo].[revenue] rev
   JOIN [UKRMC].[dbo].[Contacts] contacts
   ON rev.hotel_id = contacts.id
   WHERE datearrived between '2017-01-01' and '2017-08-31' and C like '%GB%'
  ORDER BY sita

此查询给出了一个 table,其中每个日期从 2017-01-01 到 2017-08-31,每个 SITA(有 30 个 SITA)和每个 Market_segment(有 18 个不同的 Market_segments 但每个 SITA 都没有所有 Market_segments,这就是我的问题所在。

我的下一个查询是:

SELECT SEG       
FROM [UKRMC].[dbo].[Segmentation]
WHERE SEG IN ('RAC', 'BIT', 'BIQ', 'CBI', 'TOF', 'QOF', 'BOA', 'FIT', 'LYO', 'RER', 'OTH', 'NRG', 'XXX', 'CRW', 'BGR', 'BGO', 'LGR', 'LGS') 

所以这给出了 18 个片段中的 table。

现在,我想要的是一个 table,每个日期从 2017-01-01 到 2017-08-31,每个 SITA 和每个 Market_Segment(所以每个日期将有 30 x 18 行)以及第一个查询中的所有其他列; B、C、房间、StayDays 为 RN、AMTRoom 为 Rev.

当然,因为所有日期和 market_Segment/Seg 组合都不在第一个 table 中,我只想将 RN 和 Rev 输出为 0,但 B、C、Rooms 是固定的每个 SITA(所以我需要另一个连接来获得固定值吗?)

我希望我需要的是有意义的。任何帮助表示赞赏!这里分别是两个查询的示例数据。

 SITA    B   C    Rooms  Datearrived Market_segment  RN   Rev
ABZPD   PI  GB   150    2017-01-01   TOF            2    45                                                                      
ABZPD   PI  GB   150    2017-01-01   BAO            33   30.5                                                                       
ABZPD   PI  GB   150    2017-01-01   BGR            11   50                                                                     
ABZPD   PI  GB   150    2017-01-01   NRG            52   10                                                                       
ABZPD   PI  GB   150    2017-01-01   CRW            20   90                                                                       
ABZPD   PI  GB   150    2017-01-01   BIT            7    20                                                                     
ABZPD   PI  GB   150    2017-01-01   CBI            0    40                                                                      
ABZPD   PI  GB   150    2017-01-01   OTH            10   50                                                                       
ABZPD   PI  GB   150    2017-01-01   BIQ            11   60                                                                       
ABZPD   PI  GB   150    2017-01-01   RER            12   65      



**SEG**
RAC       
BIT       
BIQ       
CBI       
TOF       
QOF       
BOA       
FIT       
LYO       
RER       
OTH       
NRG       
XXX       
CRW       
BGR       
BGO       
LGR       
LGS  

您需要 cross join distinct sita, B, C, Rooms, datearrived 与您的 SEG 列表,以及 left join 您的原始查询。我无法确定您的查询中列的来源,因此我只是将 table t 视为您的原始查询。

select 
    d.SITA
  , d.B
  , d.C
  , d.Rooms
  , d.Datearrived
  , Market_segment = s.Seg
  , Rn  = isnull(t.rn,0)
  , Rev = isnull(t.Rev,0)
from (
  select distinct sita, B, C, Rooms, datearrived 
  from dbo.t -- whichever table has the above columns
  ) d
cross join (
  select SEG
  from dbo.Segmentation
  where SEG in ('RAC', 'BIT', 'BIQ', 'CBI', 'TOF', 'QOF', 'BOA', 'FIT', 'LYO', 'RER', 'OTH', 'NRG', 'XXX', 'CRW', 'BGR', 'BGO', 'LGR', 'LGS') 
  ) s
left join dbo.t 
  on d.datearrived = t.datearrived
 and d.sita = t.sita
 and s.seg = t.market_segment

rextester 演示:http://rextester.com/WSFUO57652

returns:

+-------+----+----+-------+-------------+----------------+----+-----+
| SITA  | B  | C  | Rooms | Datearrived | Market_segment | Rn | Rev |
+-------+----+----+-------+-------------+----------------+----+-----+
| ABZPD | PI | GB |   150 | 2017-01-01  | RAC            |  0 |   0 |
| ABZPD | PI | GB |   150 | 2017-01-01  | BIT            |  7 |  20 |
| ABZPD | PI | GB |   150 | 2017-01-01  | BIQ            | 11 |  60 |
| ABZPD | PI | GB |   150 | 2017-01-01  | CBI            |  0 |  40 |
| ABZPD | PI | GB |   150 | 2017-01-01  | TOF            |  2 |  45 |
| ABZPD | PI | GB |   150 | 2017-01-01  | QOF            |  0 |   0 |
| ABZPD | PI | GB |   150 | 2017-01-01  | BOA            |  0 |   0 |
| ABZPD | PI | GB |   150 | 2017-01-01  | FIT            |  0 |   0 |
| ABZPD | PI | GB |   150 | 2017-01-01  | LYO            |  0 |   0 |
| ABZPD | PI | GB |   150 | 2017-01-01  | RER            | 12 |  65 |
| ABZPD | PI | GB |   150 | 2017-01-01  | OTH            | 10 |  50 |
| ABZPD | PI | GB |   150 | 2017-01-01  | NRG            | 52 |  10 |
| ABZPD | PI | GB |   150 | 2017-01-01  | XXX            |  0 |   0 |
| ABZPD | PI | GB |   150 | 2017-01-01  | CRW            | 20 |  90 |
| ABZPD | PI | GB |   150 | 2017-01-01  | BGR            | 11 |  50 |
| ABZPD | PI | GB |   150 | 2017-01-01  | BGO            |  0 |   0 |
| ABZPD | PI | GB |   150 | 2017-01-01  | LGR            |  0 |   0 |
| ABZPD | PI | GB |   150 | 2017-01-01  | LGS            |  0 |   0 |
+-------+----+----+-------+-------------+----------------+----+-----+