﻿CREATE PROCEDURE [BO_GetPublicKnoks_OwnData]
	@UserId BIGINT
AS
	SELECT u.HasAvatar, u.UserName, u.FirstName, u.LastName, 
		ISNULL(follow.UserFollowers, 0) UserFollowers, rank.RankValue KnokserRatingPoints, NULL KnokserRating, ISNULL(activeKnoks.Knoks, 0) UserActiveKnoks
	FROM [dbo].[Users] u 
	LEFT JOIN (select count(f.UserId) UserFollowers, f.FollowUserId userId from [dbo].[UserFollows] f group by f.FollowUserId) follow on follow.userId = u.UserId
	LEFT JOIN (select count(f.KnokId) Knoks, f.CreatorUserId  from [dbo].[Signals] f left join [_KnokStatuses] s 
	on s.KnokStatusId=f.KnokStatusId where s.IsPublished=1 and s.HasResult = 0 group by f.CreatorUserId) activeKnoks on activeKnoks.CreatorUserId = u.UserId
	CROSS APPLY API_CalcKnokserRanks(u.UserId) rank
	WHERE u.UserId = @UserId
RETURN 0
