日期:2014-05-17  浏览次数:20461 次

求一SQL分析相同产品购买用户
表结构如下
create table test
(userid int, --用户ID
 productid int --产品ID
)

需求,查询出购买相同5个以上产品的不同用户

------解决方案--------------------
SQL code
select userid,productid, count(1) from test group by userid,productid having count(1)>5

------解决方案--------------------
select userid,productid, count(1) from test group by userid,productid having count(1)>5
------解决方案--------------------
SQL code
;with TB as
(   
select userid,productid, (select count(1) from test a where a.userid =test.userid and a.productid=test.productid)as cs from test group by userid,productid
)
select * from TB where cs>5