为什么使用了group by 之后数据还是重复????

select userid, substr(indate,1,10) as day,count(*) as products from ybdw_productInfo_unique_d
where sta = 0 and isshelvesnow = 1
group by substr(indate,1,10),userid;

    会出现这样的重复数据
    userid,  day,       products
    726651  2019/1/15   4
    726651  2019/1/15    4
    726651  2018/12/21  1
    726651  2018/12/21  1
    726651  2019/1/4      4
    726651  2019/1/4      4
    726651  2018/12/20  1
    726651  2018/12/20  1
sql

6个回答

你按照indate前十位分组肯定有重复咯、你可以把userid放前面试试

LiqianQ
LiqianQ 反过来也不行的,我现在解决了,不过用的是很笨的方法,我把这结果又group by 了一遍,然后就不重复了
一年多之前 回复

group by substr(indate,1,10),userid;

你是日期group by你看你日期是没有重复了吧

加上distinct就好了

LiqianQ
LiqianQ 加了也不管用,我尝试过了
一年多之前 回复

把groupby后的substr(indate,1,10) 替换成 day,这样:
select userid, substr(indate,1,10) as day,count(*) as products from ybdw_productInfo_unique_d
where sta = 0 and isshelvesnow = 1
group by day,userid;

LiqianQ
LiqianQ 这样我这边会报错,报表中没有day这个字段这样的错。然后我就只能写原来的
一年多之前 回复

group by substr(indate,1,10).

可能有空字符,可以试一下trim函数。select userid, trim(substr(indate,1,10) ) as day,count(*) as products from ybdw_productInfo_unique_d
where sta = 0 and isshelvesnow = 1
group by trim(substr(indate,1,10)),userid;

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐