[英]Counting and Excluding based on conditions
基本上,我在一个表中(仅前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
我想做的事是对每个会员ID的Yes进行计数,我已经知道该怎么做DidYouBuy ='1'但是我也想做的是计算No's的数目,这有点棘手'DidYouBuy = 0'
如您在上表中所见,对于相同的memberID和Sample ID(这是他们正在营销的样品的ID),存在多个“否”条目,这是因为每次有人在网站上选择“否”时,问题仍然存在,并且每次单击“否”都会为该示例注册。 但是,当他们单击“是”时,该问题消失,并且不再有该特定成员的该样本的“否”注册。
我想计算尚未转换为“是”的唯一编号的数量。 我知道这听起来很混乱,所以当您有时间给我们大喊时,我无法弄清楚,是使用条件语句吗?
我可以得到没有问题的“是”,但是要计算没有选择“是”的“否”的数量,这是我无法解决的问题。 我有感觉需要使用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
这是我希望在查询“否”时看起来的样子,请注意将具有“否”但后来回答“是”的人从结果中排除的方式
好吧,您的问题的简化就是计算唯一的MemberId
SELECT COUNT(*) FROM ( SELECT * FROM SampleDidYouBuy WHERE SampleID = {YourSampleID} GROUP BY MemberID ) AS sample;
MySQL允许在聚合函数中使用表达式。 看看这个: http : //dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count
因此,您的SQL语句应如下所示(如果我正确解决了您的问题?):
SELECT MemberID, COUNT(DidYouBuy=0) as 'No-s', COUNT(DidYouBuy=1) as 'Yes-s'
FROM SampleDidYouBuy
GROUP BY MemberID
如果您希望收到每个样本的No-s数:
SELECT SampleID, MemberID, COUNT(DidYouBuy=0)
FROM SampleDidYouBuy
GROUP BY SampleID, MemberID
如果我理解您的问题,则可以尝试执行此查询,以查找尚未转换为“是”的唯一编号
仅针对会员ID
SELECT
`MemberID`, COUNT(*)
FROM `SampleDidYouBuy`
GROUP BY `MemberID`
HAVING MAX(`DidYouBuy`) = 0
输出:
+----------+----------+
| MemberID | COUNT(*) |
+----------+----------+
| 23 | 1 |
| 32 | 2 |
| 40 | 2 |
+----------+----------+
对于MemberID和sampleID
SELECT
`MemberID`, `sampleID`, COUNT(*)
FROM `SampleDidYouBuy`
GROUP BY `MemberID`, `sampleID`
HAVING MAX(`DidYouBuy`) = 0
输出量
+----------+----------+----------+
| MemberID | sampleID | COUNT(*) |
+----------+----------+----------+
| 23 | 23182 | 1 |
| 24 | 23214 | 2 |
| 27 | 23184 | 3 |
| 27 | 23224 | 2 |
| 27 | 23254 | 2 |
| 27 | 23259 | 2 |
| 32 | 23184 | 2 |
| 40 | 23181 | 2 |
+----------+----------+----------+
请尝试以下查询:-
select
(select count(MemberID) from SampleDidYouBuy where DidYouBuy='1' group by MemberID) as yesCount,
(select count(MemberID) from SampleDidYouBuy where DidYouBuy='0' group by MemberID) as noCount
from SampleDidYouBuy;
更新:
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;
小提琴演示。
要获得包含所有列的行,
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;
从预期的输出尚不清楚,应该显示重复的行之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.