sql server - selecting random rows in sql -
sql server - selecting random rows in sql -
this question has reply here:
select n random rows sql server table 13 answersi have list of records in tsql query. in situation want select random 5 rows output result.
here snapshot of result-
now if see image have many rows coming according query i've written-
declare @table1 table ( cnt bigint, vid bigint ) insert @table1(cnt,vid) select count(video_id)as counts,video_views.video_id video_views video_views.is_active='true' grouping video_views.video_id select videos.*,vid @table1 left bring together videos on videos.video_id=vid cnt< '100' order cnt desc
in query- managing video site, want implement section videos low views come. here in query table videos
, whenever video viewed view lists row in table video_views
. have written query in manner records have views less 100
come descending order.
as can see result shows me many records views less 100
. wanted select random 5 rows list.
how should approaching working?
the canonical way in sql server utilize order newid()
:
select top 5 videos.*, vid @table1 left bring together videos on videos.video_id=vid cnt < 100 order newid() desc ;
there alternatives if performance issue.
note: should utilize single quotes string , date constants. don't utilize single quotes integer constant such 100
.
sql sql-server tsql
Comments
Post a Comment