This is similar to this question and this question, but mine is a little different and I can't get it to work.
I'm using SQL Server 2016. My existing query is below, and it works fine. It selects the Interest
row and orders them by the number of Post
s that interest has.
-- this is in a stored procedure, variables are valid
select i.[Id], i.[Name], i.[Description],
(
select count(*) from [Post] p where p.[InterestId] = i.[Id] and p.[CreatedAt] >= @AfterTime
) as PostCount
from Interest i
order by PostCount desc
offset (@Start - 1) rows fetch next (@ReturnNum) rows only
I now want to change this. There's another column in the Interest table with InterestCategoryId
; that's a foreign key for a 1:M relationship with InterestCategory
. I want to get the same info as before, but limited to only a single InterestCategoryId
--I'd only get one result per category. How can I accomplish this? I'm trying this code, but it throws an error that PostCount
isn't a valid column.
select [Id], [Name], [Description], PostCount
from
(
select [Id], [Name], [Description],
(
select count(*) from [Post] po where po.[InterestId] = [Id] and po.[CreatedAt] >= @AfterTime
) as PostCount,
row_number() over(partition by [InterestCategoryId] order by PostCount desc) rn
from [Interest]
) i
where rn = 1
order by PostCount desc
offset (@Start - 1) rows fetch next (@ReturnNum) rows only
Example interest data is below. If we assume that interest ID 1 has more posts than 2, I would want interests 1, 3, and 4 returned.
ID | Name | Description | InterestCategoryId
---|------|-------------|-------------------
1 | Test | Test int | 1
2 | Tes2 | Test int2 | 1
3 | Tes3 | Test int3 | 2
4 | Tes4 | Test int4 | 3