I need help with select statement in SQL Server / T-SQL
My table looks like this:
Id (int)
QuestionId (int)
GenreId (int)
QuestionScore (int)
I want to select random N rows from this table so that maximum number of same GenreId in the result set is less than X for all GenreId-s except one. For that one GenreId, I need row count with that GenreId to be equal to Y.
UPDATE
I made up this query from suggestions below, it works exactly as i wanted (except for one genre, but thats no problem, let it be this way, ill have 2 queries)
select top @N * from
(select Id,GenreId,Rank() over (Partition BY GenreId order by newId()) as Rank,QuestionScore from Questions) t
where t.Rank <= @X
order by newId()
now i need to select rows so that average QuestionScore is between 1.7 AND 2.3
how can i do that? i need all columns returned in result set.
thanks in advance :)