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

时间:2017-10-04 10:50:28

标签: sql-server join cross-join

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

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

此查询提供了一个表格,其中包含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段的表格。

现在,我想要的是一张表,其中包含2017-01-01至2017-08-31的每个日期,每个SITA和每个Market_Segment(因此每个日期将有30 x 18行)以及所有其他列从第一个查询也; B,C,客房,S​​tayDays为RN,AMTRoom为Rev。

当然,由于所有日期和market_Segment / Seg组合都不在第一个表格中,我想输出RN和Rev为0,但B,C,房间是固定的每个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  

1 个答案:

答案 0 :(得分:2)

您需要使用cross join列表distinct sita, B, C, Rooms, datearrived SEGleft join原始查询。我无法确定您的查询中的列来源,因此我只是将表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

返回:

+-------+----+----+-------+-------------+----------------+----+-----+
| 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 |
+-------+----+----+-------+-------------+----------------+----+-----+