我需要选择顾客看过最多的产品。我尝试使用“ group by”和“ order by”语句,但是我猜错了。有人可以帮忙吗?
请参见下面的示例:
答案 0 :(得分:0)
首先使用*.skeleton
进行汇总以获取计数。然后,您可以使用count()
来确定用户数最多的(a)行。
row_number()
当然,您的DBMS必须支持SELECT y.id,
y.email,
y.firstname,
y.sku
FROM (SELECT x.id,
x.email,
x.firstname,
x.sku,
row_number() OVER (PARTITION BY x.id,
x.email,
x.firstname
ORDER BY x.views DESC) rn
FROM (SELECT t.id,
t.email,
t.firstname,
t.sku,
count(*) views
FROM elbat t
GROUP BY t.id,
t.firstname,
t.sku) x) y
WHERE y.rn = 1;
才能起作用。而且您没有提到DBMS,所以我不知道是否是这种情况。
答案 1 :(得分:0)
您可以使用聚合和窗口功能:
select id, email, firstname, sku
from (select id, email, firstname, sku, count(*) as cnt,
row_number() over (partition by id order by count(*)) desc) as seqnum
from t
group by id, email, firstname, sku
) x
where seqnum = 1;