我需要排除与至少有一个条件适用的实例的客户ID相关的所有数据。例如:
这是我所有的有条件的数据
+-------------+----------------+
| Customer ID | Condition |
+-------------+----------------+
| 1 | Contacted |
| 1 | No Answer |
| 1 | Left Voicemail |
| 1 | Spoke to |
| 2 | No Answer |
| 2 | Left Voicemail |
| 3 | Spoke to |
| 3 | No Answer |
| 4 | Contacted |
| 4 | Left Voicemail |
+-------------+----------------+我需要排除条件等于“已联系”的数据。目前,我正在使用下面的代码,并得到以下结果:
SELECT a.customerID,
c.condition
FROM Tablea a
JOIN Tablec c ON
c.customerID = a.customerID
WHERE c.condition NOT LIKE 'Contacted'+-------------+----------------+
| Customer ID | Condition |
+-------------+----------------+
| 1 | No Answer |
| 1 | Left Voicemail |
| 1 | Spoke to |
| 2 | No Answer |
| 2 | Left Voicemail |
| 3 | Spoke to |
| 3 | No Answer |
| 4 | Left Voicemail |
+-------------+----------------+但是,如果客户ID符合条件,我想排除所有客户ID行。理想情况下,代码将生成以下代码:
+-------------+----------------+
| Customer ID | Condition |
+-------------+----------------+
| 2 | No Answer |
| 2 | Left Voicemail |
| 3 | Spoke to |
| 3 | No Answer |
+-------------+----------------+任何帮助都是非常感谢的!
发布于 2019-08-31 02:52:58
这会给你带来你想要的结果:
Select customerid, condition from
Tablec c
WHERE customerid NOT IN
(
Select customerid from
Tablec
WHERE condition LIKE 'Contacted'
)在上面的示例中,我不确定为什么要连接到Tablea,因为除了已经在Tablec中的customerID之外,您没有从该表中提取任何数据。
但是,如果您想进行连接,则可以执行以下操作:
Select a.customerID, c.condition from Tablea a
JOIN Tablec c ON c.customerID = a.customerID
WHERE c.customerid NOT IN
(
Select customerid from
Tablec
WHERE condition LIKE 'Contacted'
)发布于 2019-08-31 02:37:19
以下是使用not exists的一种选择
select customerid, condition
from Tablec c
where not exists (
select 1
from Tablec c2
where c.customerid = c2.customerid and c2.condition = 'Contacted'
)这只使用了Tablec,因为它似乎同时具有customerid和condition。如果需要,您可以随时将其连接回TableA。
发布于 2019-08-31 02:38:44
这有点老生常谈,但我不确定你的原始数据集是什么样子的。
SELECT a.customerID,
c.condition
FROM Tablea a
INNER JOIN Tablec c ON
c.customerID = a.customerID
WHERE c.customerID NOT IN
(
SELECT a2.customerID
FROM Tablec a2
WHERE a2.condition = 'Contacted'
)https://stackoverflow.com/questions/57731650
复制相似问题