I have a requirement to return all the 'Message' objects that are in the same 'Subject'(s) as a given Message.
A Message can be in 1 or more subjects, and there is a table called MessageSubject that contains a MessageID, SubjectID for each subject that message is in.
My requirement is to return the 'top' 20 message candidates that are in the same subject(s) as the message given.
The Table 'Message' also has a field called 'Subjects', and this field is a comma delimited list of subjects;
The 'easy' requirement was to return all the Messages that have an exact match as a source message ID, this SQL is below ( Message_GetInContextRecommendations_Exact ):
In cases where there are not 20 messages that 'exactly' match the same subject combination, my next requirement is to get all the messages out of any of the subjects that the source message is in;
Where I am stuck is how to rank the matches as a % of match.
If the source message in Four Subjects (Subject1, Subject2, Subject3, Subject4) then a message that is in:
One Subject (25%), Two Matches (50%), Three Matches (75%), Four Matches (100%)
If the source message in Two Subjects (Subject1, Subject2) then a message that is in:
One Subject (50%), Two Matches (100%), etc.
And Then I would like to order on the 'MatchPercent' in descending order (I can do this part!)
Could someone tell me if this is possible and how to go about it?
I will name a hospital after you one day if you can help.
Thanks for any help, I think better in C# than I do in SQL
Corby
Create PROCEDURE [dbo].[Message_GetInContextRecommendations_Exact] -- Parameter for UserID, SourceMessageID @UserID int, @SourceMessageID int = 0 AS BEGIN --Variable to hold the subjects Declare @Subjects nchar(255) -- Set the value for @Subjects Set @Subjects = (Select [Subjects] From Message Where MessageID = @SourceMessageID) -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON -- Begin Select Statement Select Top 20[ClientType],[CreatedDate],[FileDownloadUrl],
[HasLinkOrAttachment],[ImageHeight],[ImagePreviewUrl],
[LastReplyDate],[LastReplyID],[LikesCount],[LinkDisplayUrl],
[LinkFileName],[LinkID],[LinkIsImage],[LinkUrl],[MessageID],
[MessageSource],[MessageText],[NetworkID],[RepliesCount],
[Score],[Subjects],[SubjectsCount],[SynxiMessageID],[UserID],
[WebUrl] -- From Table From [Message] -- Do Not Return Any Messages That Have Been Recommended To This User Already Where [MessageID] Not In (Select MessageID From MessageRecommendationHistory
Where UserID = @UserID) -- Do Not Return Any Messages Created By This User And [UserID] != @UserID -- Do Not Return The MessageID And [MessageID] != @SourceMessageID -- Only return messages that match the Subjects given And [Subjects] = @Subjects -- Order By [Message.Score] and [Message.CreatedDate] in reverse order Order By [Score] desc, [CreatedDate] desc END
CREATE TABLE [dbo].[Message]( [SynxiMessageID] [int] IDENTITY(1,1) NOT NULL, [UserID] [int] NULL, [MessageID] [int] NULL, [NetworkID] [int] NULL, [Subjects] [nchar](255) NULL, [SubjectsCount] [int] NULL, [RepliesCount] [int] NULL, [LikesCount] [int] NULL, [CreatedDate] [datetime] NULL, [LastReplyDate] [datetime] NULL, [LastReplyID] [int] NULL, [MessageText] [nvarchar](max) NULL, [HasLinkOrAttachment] [bit] NULL, [LinkDisplayUrl] [nchar](255) NULL, [LinkUrl] [nchar](255) NULL, [LinkID] [int] NULL, [LinkIsImage] [bit] NULL, [LinkFileName] [nchar](80) NULL, [ImagePreviewUrl] [nchar](128) NULL, [FileDownloadUrl] [nchar](128) NULL, [Score] [decimal](10, 6) NULL, [ClientType] [int] NULL, [ImageHeight] [int] NULL, [WebUrl] [nchar](128) NULL, [MessageSource] [int] NULL)
CREATE TABLE [dbo].[Subject]( [SynxiSubjectID] [int] IDENTITY(1,1) NOT NULL, [SubjectID] [int] NULL, [NetworkID] [int] NULL, [UserID] [int] NULL, [Name] [nchar](255) NULL, [Level] [int] NULL, [CreatedDate] [datetime] NULL, [IsDeleted] [bit] NULL, [Frequency] [numeric](12, 8) NULL, [SharepointLinks] [int] NULL, [Score] [decimal](10, 6) NULL, [SubjectType] [int] NULL, [WebUrl] [nchar](128) NULL)
CREATE TABLE [dbo].[MessageSubject]( [MessageSubjectID] [int] IDENTITY(1,1) NOT NULL, [MessageID] [int] NOT NULL, [SubjectID] [int] NOT NULL)