你能在一个 table 上有多个 select 状态然后加入它们吗?

Can you have multiple select statemets on a single table then join them?

请注意,我不是开发人员,因此我们将不胜感激。

我的问题: 我在 Oracle Responsys 中有一个列表,其中包含预订旅游的所有客户。我想找到 FIRST_NAME(可能是 Tba/Tbc/Null)缺失值的所有记录,这样我就可以向主要预订者发送一封电子邮件,要求他们更新其他客人的姓名详细信息。

主要预订者的 CUSTOMER_ID_ 值始终以“-1”结尾。缺少名字值的记录不会。

我想要做的是 select 列表中缺少名字值的所有记录,将它们按 BOOKING_LOCATOR 分组(主要预订者和他们的附加值共享客人)- 然后只有 return 主要预订者(记录 CUSTOMER_ID_ 以 '-1' 结尾。

到目前为止,我已经通过查看其他帖子拼凑出我所追求的粗略想法,但它 return 与我所追求的完全不同:

SELECT $A$.BOOKING_LOCATOR, $A$.CUSTOMER_ID_, $A$.EMAIL_ADDRESS_
FROM
(
  SELECT $A$.BOOKING_LOCATOR FROM $A$ WHERE ($A$.FIRST_NAME IN ('TBA', 'TBC', 'Tba', 'Tbc') OR $A$.FIRST_NAME IS NULL) AND $A$.POLAR_BOOKING_STATUS != 'C' AND $A$.BOOKING_STATUS != 'Waitlist' AND $A$.EMBARK_DATE >= SYSDATE+1
  GROUP BY $A$.BOOKING_LOCATOR
) $A$
INNER JOIN (
  SELECT $A$.BOOKING_LOCATOR AS BOOKINGNUM, $A$.CUSTOMER_ID_, $A$.EMAIL_ADDRESS_ FROM $A$ WHERE $A$.CUSTOMER_ID_ LIKE '%-1'
) $A$ ON $A$.BOOKING_LOCATOR = $A$.BOOKINGNUM

有人可以指导我正确的方向吗?

为 sub-selects 命名:

SELECT A.BOOKING_LOCATOR, B.CUSTOMER_ID_, B.EMAIL_ADDRESS_
FROM
(
  SELECT $A$.BOOKING_LOCATOR FROM $A$ WHERE ($A$.FIRST_NAME IN ('TBA', 'TBC', 'Tba', 'Tbc') OR $A$.FIRST_NAME IS NULL) AND $A$.POLAR_BOOKING_STATUS != 'C' AND $A$.BOOKING_STATUS != 'Waitlist' AND $A$.EMBARK_DATE >= SYSDATE+1
  GROUP BY $A$.BOOKING_LOCATOR
) A
INNER JOIN (
  SELECT $A$.BOOKING_LOCATOR AS BOOKINGNUM, $A$.CUSTOMER_ID_, $A$.EMAIL_ADDRESS_ FROM $A$ WHERE $A$.CUSTOMER_ID_ LIKE '%-1'
) B ON A.BOOKING_LOCATOR = B.BOOKINGNUM;

是的,你可以 it.You 可以使用连接或外部应用。

SELECT A.BOOKING_LOCATOR, B.CUSTOMER_ID_, B.EMAIL_ADDRESS_
FROM
(
 SELECT $A$.BOOKING_LOCATOR FROM $A$ WHERE ($A$.FIRST_NAME IN ('TBA', 'TBC', 'Tba', 
'Tbc') OR $A$.FIRST_NAME IS NULL) AND $A$.POLAR_BOOKING_STATUS != 'C' AND 
$A$.BOOKING_STATUS != 'Waitlist' AND $A$.EMBARK_DATE >= SYSDATE+1
GROUP BY $A$.BOOKING_LOCATOR
) A
INNER JOIN (
SELECT $A$.BOOKING_LOCATOR AS BOOKINGNUM, $A$.CUSTOMER_ID_, $A$.EMAIL_ADDRESS_ FROM 
$A$ WHERE $A$.CUSTOMER_ID_ LIKE '%-1'
) B ON A.BOOKING_LOCATOR = B.BOOKINGNUM;

--2ND 选项是通过使用 OuterAPPLY。

SELECT A.BOOKING_LOCATOR, B.CUSTOMER_ID_, B.EMAIL_ADDRESS_
FROM
(
  SELECT $A$.BOOKING_LOCATOR FROM $A$ WHERE ($A$.FIRST_NAME IN ('TBA', 'TBC', 'Tba', 
 'Tbc') OR $A$.FIRST_NAME IS NULL) AND $A$.POLAR_BOOKING_STATUS != 'C' AND 
$A$.BOOKING_STATUS != 'Waitlist' AND $A$.EMBARK_DATE >= SYSDATE+1
 GROUP BY $A$.BOOKING_LOCATOR
) A
 Outer APPLY(
   SELECT $A$.BOOKING_LOCATOR AS BOOKINGNUM, $A$.CUSTOMER_ID_, $A$.EMAIL_ADDRESS_ FROM 
$A$ WHERE $A$.CUSTOMER_ID_ LIKE '%-1'
) B ON A.BOOKING_LOCATOR = B.BOOKINGNUM;

如果你想要预订定位和主要客户,那么想到EXISTS

SELECT . . .   -- whatever columns you want
FROM $A$ a1
WHERE a.CUSTOMER_ID_ LIKE '%-1' AND
      EXISTS (SELECT 1
              FROM $A$ a
              WHERE a.FIRST_NAME IN ('TBA', 'TBC', 'Tba', 'Tbc') OR a.FIRST_NAME IS NULL) AND  
                    a.POLAR_BOOKING_STATUS <> 'C' AND  
                    a.BOOKING_STATUS <> 'Waitlist' AND  
                    a.EMBARK_DATE >= SYSDATE+1 AND
                    a.BOOKING_LOCATOR = a1.BOOKING_LOCATOR
              );

这 returns 每个主要客户只有一行。