我有一张这样的表:
Id-------Product---------date-------productID
1 a 2012 a1
1 a 2013 a1
1 b 2013 b1
2 a 2011 a1
2 c 2012 c1
它有近200万行没有主键。 我想创建一个视图,显示每个客户购买的产品数量。我该怎么办?谢谢你们!
答案 0 :(得分:2)
我将假设id
指的是客户。在这种情况下,您不想要join
,您想要group by
:
select id, count(*) as numPurchases, count(distinct product) as numProducts
from table t
group by id;
对于视图,您只需添加create view t as