POSTGRESQL COUNT()返回更多信息
我有以下SQL语句:POSTGRESQL COUNT()返回更多信息,sql,postgresql,Sql,Postgresql,我有以下SQL语句: select a.title, a.video_id, b.id from tzrat_community_videos a inner join tzrat_video_views b on a.video_id = b.videoid where b.cdate >= '2014-01-01 00:00:00' and b.cdate <= '2014-06-26 23:59:59' and a.video_i
select a.title, a.video_id, b.id
from
tzrat_community_videos a
inner join
tzrat_video_views b on a.video_id = b.videoid
where
b.cdate >= '2014-01-01 00:00:00' and
b.cdate <= '2014-06-26 23:59:59' and
a.video_id = 'dCflt1d2xPw' and
a.published = 1 and b.duration != 0 and
(
(a.percent is not null and (100*b.seconds)/b.duration >= a.percent ) or
((100*b.seconds)/b.duration >= 80)
)
group by b.videoid, a.title, a.video_id, b.id
选择a.title、a.video\u id、b.id
从…起
tzrat_社区_视频a
内连接
tzrat_video_在a上查看b.video_id=b.videoid
哪里
b、 cdate>=“2014-01-01 00:00:00”和
b、 cdate=a.0%)或
((100*b.秒)/b.持续时间>=80)
)
按b.videoid、a.title、a.video\u id、b.id分组
结果:
但是我只想要标题、视频id和它出现的次数(我想要这个视频的浏览次数)
因此,我谨此声明:
select a.title, a.video_id, count(b.id) as views
from
tzrat_community_videos a
inner join
tzrat_video_views b on a.video_id = b.videoid
where
b.cdate >= '2014-01-01 00:00:00' and
b.cdate <= '2014-06-26 23:59:59' and
a.video_id = 'dCflt1d2xPw' and
a.published = 1 and
b.duration != 0 and
(
(a.percent is not null and (100*b.seconds)/b.duration >= a.percent ) or
((100*b.seconds)/b.duration >= 80)
)
group by b.videoid, a.title, a.video_id
选择a.title、a.video\u id、count(b.id)作为视图
从…起
tzrat_社区_视频a
内连接
tzrat_video_在a上查看b.video_id=b.videoid
哪里
b、 cdate>=“2014-01-01 00:00:00”和
b、 cdate=a.0%)或
((100*b.秒)/b.持续时间>=80)
)
按b.videoid、a.title、a.video\u id分组
但这让我回想起:
所以。。。我做错了什么?是相同的语句,只是b.id->count(b.id)作为视图 在第二种情况下没有做的事情(当然,因为它是在聚合函数中) 所以改变
count(b.id)
借
如果您想避免重复的
b.id
计数。@user3770536没有问题。别忘了接受“结案”的答案
count(distinct b.id)