基于Ridge Gallery视图:COUNT函数统计artistid异常
问题分析与解决方案
你的SQL里group by artistid, datesold是按艺术家ID+销售日期分组统计,所以得到的是每个艺术家在不同日期的销售次数,只有当同一个艺术家在同一天有多条销售记录时,count才会大于1。如果要统计每个艺术家的总销售次数,需要调整分组逻辑:
方案1:仅统计每个艺术家的总销售次数
去掉datesold字段和分组条件,只按artistid分组:
select artistid, count(artistid) as total_sales from tutorials.work w join( select workid, datesold from tutorials.trans t where datesold is not null )t on w.workid = t.workid group by artistid order by artistid;
方案2:保留销售日期同时显示总次数
如果需要保留datesold信息,同时展示每个艺术家的总销售次数,可以用窗口函数:
select artistid, datesold, count(artistid) over(partition by artistid) as total_sales from tutorials.work w join( select workid, datesold from tutorials.trans t where datesold is not null )t on w.workid = t.workid order by artistid;
内容的提问来源于stack exchange,提问作者Wei hao Sin
相关产品推荐
相关产品推荐

