sql - Can you have multiple select statemets on a single table then join them?
问题描述
Please note that I'm not a developer, so any help here would be greatly appreciated.
My problem: I have list in Oracle Responsys that contains all customers booked on a tour. I want to find all records that have missing values for FIRST_NAME (could be Tba/Tbc/Null), so I can send the main booker an email requesting that they update the name details for their additional guests.
The main booker will always have a CUSTOMER_ID_ value ending in '-1'. The records with missing first name values won't.
What I want to do, is select all records from the list that have missing first name values, group them by BOOKING_LOCATOR (a value that is shared by the main booker and their additional guests) - THEN only return the main bookers (records with CUSTOMER_ID_ ending in '-1'.
So far, I've pieced together this rough idea of what Im after from looking at other posts, but it doesn't return anything like what I'm after:
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
Can someone guide me in the right direction?
解决方案
为子选择命名:
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;
推荐阅读
- variables - 使用动态创建的变量的值
- r - 如何让用户在闪亮的应用程序中上传多个文件?
- python - 生成源后使用graphviz更改特定节点的颜色
- azure-active-directory - Azure AD B2C 自定义声明
- video - 为什么ffmpeg会跳过一些图像?
- tensorflow - Tensorflow 模型未加载 gunicorn
- php - html按钮用sql脚本加载页面,然后返回当前页面
- javascript - 如何使用组件的 onclick 事件重定向到 gatsby-react 页面?
- vue.js - 插槽中组件中的 Vue 渲染道具
- javascript - 在 chart.js 中创建不同宽度的类别