问问题
110 次
5 回答
0
好吧,您的问题的简化是计算唯一的 MemberId
选择计数(*)从( 从 SampleDidYouBuy 中选择 * WHERE SampleID = {YourSampleID} 按会员 ID 分组 ) 作为样本;
于 2013-09-12T05:06:57.563 回答
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
于 2013-09-12T05:21:09.303 回答
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 |
+----------+----------+----------+
于 2013-09-12T05:41:40.820 回答
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;
于 2013-09-12T06:08:05.430 回答
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;
小提琴演示。
要获取所有列的行,
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;
从您的预期输出中不太清楚,应该显示哪一个重复行。
于 2013-09-12T06:13:01.703 回答