根据条件计算和排除

时间:2013-09-12 05:01:25

标签: 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

我想要做的是计算每个会员ID的数量是的,我已经知道该怎么做 DidYouBuy ='1' 但我还要做的是计算No的数量,这有点棘手'DidYouBuy = 0'

正如您在上表中所看到的,对于相同的memberID和样本ID(这是他们正在营销的样本的ID),有多个No的条目,这是因为每次有人选择No答案时网站,问题仍然存在,每次他们点击否它注册该样本。但是,当他们点击“是”时,问题就会消失,并且该特定成员的该样本已经没有注册。

我想计算尚未转为是的唯一编号。我知道这听起来很混乱,所以当你有时间给我们一个喊叫时,我无法弄明白这一点,它是否使用条件陈述?

我可以毫无问题地得到“是”但是要计算没有选择的“否”的数量是的,这是一个我无法弄清楚的问题。我觉得需要使用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

当我查询No时,我希望它看起来像是什么,请注意那些有No但后来回答是的人被排除在结果之外

5 个答案:

答案 0 :(得分:0)

嗯,你的问题的复制是计算唯一的MemberId

SELECT COUNT(*) FROM (
   SELECT * FROM SampleDidYouBuy
   WHERE SampleID = {YourSampleID}
   GROUP BY MemberID
) AS sample;

答案 1 :(得分:0)

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

答案 2 :(得分:0)

如果我理解了您的问题,您可以尝试此查询,以便找到尚未转为“是”的唯一编号

仅限会员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 |
+----------+----------+----------+

答案 3 :(得分:0)

尝试以下查询: -

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;

答案 4 :(得分:0)

更新:

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;

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