选择符合SQL条件的所有非不同用户的列表

选择符合SQL条件的所有非不同用户的列表,sql,Sql,我有一个很大的人名单,他们的电子邮件和订阅状态-活动,取消或试用 有些人取消订阅,但再次激活,这将创建一个新记录。这给我留下了一个同时拥有已取消订阅和活动订阅的人员列表 我想联系所有订阅被取消的人,但如果我写一个简单的查询,它不会检查同一封电子邮件是否有不同的订阅处于活动状态 这里有一个简单的例子来说明我的意思: client_email ; subscription_status john@gmail.com - Cancelled john@gmail.com - Active fred@g

我有一个很大的人名单,他们的电子邮件和订阅状态-活动,取消或试用

有些人取消订阅,但再次激活,这将创建一个新记录。这给我留下了一个同时拥有已取消订阅和活动订阅的人员列表

我想联系所有订阅被取消的人,但如果我写一个简单的查询,它不会检查同一封电子邮件是否有不同的订阅处于活动状态

这里有一个简单的例子来说明我的意思:

client_email ; subscription_status
john@gmail.com - Cancelled
john@gmail.com - Active
fred@gmail.com - Cancelled
fred@gmail.com - Cancelled
terry@gmail.com - Active
lilly@gmail.com - Cancelled
我希望能回来fred@gmail.com及lilly@gmail.com.

到目前为止,我所能做的就是创建一个包含多个记录的所有帐户的列表:

SELECT *
FROM subscriptions
WHERE customers_email
IN (
  SELECT customers_email
  FROM subscriptions
  GROUP BY customers_email
  HAVING COUNT( customers_email ) >1)

你可以试试下面的方法-

SELECT customers_email
FROM subscriptions
group by customers_email
having min(subscription_status)='Cancelled' and max(subscription_status)='Cancelled'

你可以试试下面的方法-

SELECT customers_email
FROM subscriptions
group by customers_email
having min(subscription_status)='Cancelled' and max(subscription_status)='Cancelled'

您正在查找订阅表中不存在具有相同电子邮件和订阅状态=“活动”的记录的所有电子邮件

也就是说:

SELECT DISTINCT s.customers_email
FROM subscriptions s
WHERE NOT EXISTS(
    SELECT * 
    FROM subscriptions s2 
    WHERE s2.customers_email = s.customers_email AND s2.subscription_status = 'Active')

您正在查找订阅表中不存在具有相同电子邮件和订阅状态=“活动”的记录的所有电子邮件

也就是说:

SELECT DISTINCT s.customers_email
FROM subscriptions s
WHERE NOT EXISTS(
    SELECT * 
    FROM subscriptions s2 
    WHERE s2.customers_email = s.customers_email AND s2.subscription_status = 'Active')