mysql - 根据条件计数和排除

标签 mysql sql count

基本上我在一个表中有以下内容(只有前 25 行),我通过执行这个简单的查询得到它。

select * from SampleDidYouBuy
order by MemberID, SampleID, DidYouBuy

SampleDidYouBuyID   SampleID    MemberID    DidYouBuy   DateAdded
-----------------------------------------------------------------
1217                23185       5           1           35:27.9
58458               23184       22          0           47:15.4
58459               23184       22          1           47:36.8
58457               23203       22          1           47:12.6
299576              23257       22          1           33:38.4
59470               23182       23          0           36:22.1
97656               23183       24          1           53:46.5
97677               23214       24          0           53:59.6
212732              23214       24          0           42:53.3
226583              23245       24          1           28:29.6
191718              23184       27          0           00:19.4
156363              23184       27          0           09:45.6
121106              23184       27          0           50:57.0
156362              23224       27          0           09:42.8
191716              23224       27          0           00:17.7
191715              23235       27          1           00:15.2
318100              23254       27          0           24:36.6
335410              23254       27          0           57:33.2
335409              23259       27          0           57:31.9
318099              23259       27          0           24:34.5
118989              23184       32          0           55:03.6
119013              23184       32          0           56:57.4
119842              23183       34          1           38:12.6
129364              23181       40          0           23:59.7
139977              23181       40          0           04:08.8

我想做的是计算每个成员(member) ID 的 Yes 数量,我已经知道该怎么做 DidYouBuy = ‘1’ 但我还想做的是计算否的数量,这有点棘手 ‘DidYouBuy = 0’

如您在上表中所见,同一 memberID 和 sample ID(这是他们正在营销的 sample 的 ID)有多个“否”条目,这是因为每次有人在网站,问题仍然存在,每次他们单击“否”时,它都会注册该样本。但是,当他们单击"is"时,问题就会消失,并且不再有针对该特定成员的该样本注册的“否”。

我想计算没有变成"is"的唯一否的数量。我知道这听起来很困惑,所以当你有空的时候告诉我们,我想不通,它是在使用条件语句吗?

我可以毫无问题地获得"is",但是计算没有选择"is"的“否”的数量是一个我无法弄清楚的问题。我觉得需要使用 group by 子句来完成吗?

预期输出

SampleDidYouBuyID   SampleID    MemberID    DidYouBuy   DateAdded
-----------------------------------------------------------------
59470       23182       23      0       36:22.1
212732      23214       24      0       42:53.3
121106      23184       27      0       50:57.0
191716      23224       27      0       00:17.7
335410      23254       27      0       57:33.2
318099      23259       27      0       24:34.5
119013      23184       32      0       56:57.4
139977      23181       40      0       04:08.8

这就是我在查询“否”时希望它看起来的样子,请注意那些有“否”但后来回答"is"的人如何被排除在结果之外

最佳答案

更新:

select a.MemberId,
       count(distinct a.SampleId) as unique_nos
  from SampleDidYouBuy a
 where a.DIdYouBuy = 0
   and not exists (select 1 
                     from SampleDidYouBuy b
                    where b.MemberId = a.MemberId
                      and b.SampleId = a.SampleId
                      and b.DidYouBuy = 1)
 group by a.MemberId;

演示在 fiddle .

获取包含所有列的行,

select SampleDidYouBuyID, sampleid, MemberID, DIdYouBuy, DateAdded
  from SampleDidYouBuy a
 where a.DIdYouBuy = 0
   and not exists (select 1 
                     from SampleDidYouBuy b
                    where b.MemberId = a.MemberId
                     and b.SampleId = a.SampleId
                     and b.DidYouBuy = 1)
 group by memberid, sampleid
 order by MemberID,sampleid;

从您的预期输出中并不清楚应该显示重复行中的哪一行。

关于mysql - 根据条件计数和排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18755700/

相关文章:

phpMyAdmin "Cannot load or save configuration"

c# - 使用 SSH.NET 库从 .NET 连接到 MySQL

sql - 为什么不直接从客户端连接到 SQL 服务器?为什么我们需要客户端-服务器模型中的应用服务器?

sql - 需要查询帮助

SQL:仅根据内容显示值

python缓存字典 - 计算命中次数

Mysql 乘法计数

mysql - 对 MYSQL 标签表进行排序

mysql - 在一列mysql中插入多个值?

MYSQL存储过程不检查where子句并返回整个表