Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Nadermfr
Starting Member
4 Posts |
Posted - 2012-03-17 : 19:35:11
|
I need help with the following query Table 1: ThreadID, SectionID,OpenDate,CloseDateTable 2:MessageID, MessageTitle,MessageSender,MessageDate,MessageThread,MessageParentThe message has messageparent=0 for the main threadthe message which is a reply will have the messageparent is the message that the sender replied to Messagethread is the threadid that the message belongs toI want to display threadid, messagetitle, numberofreplies,lastpostdate for the main threadsThe following is to create the tables and insert information into themCREATE TABLE [ForumThread]([ThreadID] [int] IDENTITY(1,1) NOT NULL,[ThreadSection] [int] NOT NULL,[OpenDate] [datetime] NOT NULL,[CloseDate] [datetime] NOT NULL,[Deleted] [bit] NOT NULL CONSTRAINT [PK_ForumThread] PRIMARY KEY CLUSTERED ([ThreadID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 100) ON [PRIMARY]) ON [PRIMARY]-----------------------------------------------------CREATE TABLE[ThreadMessage]( [MessageID] [int] IDENTITY(1,1) NOT NULL, [MessageSender] [int] NOT NULL, [MessageThread] [int] NOT NULL, [MessageParent] [int] NOT NULL, [MessageTitle] [nvarchar](100) NOT NULL, [MessageDate] [datetime] NOT NULL, [MessageContent] [nvarchar](max) NOT NULL, [UpdateDate] [datetime] NOT NULL, [Deleted] [bit] NOT NULL, CONSTRAINT [PK_ThreadMessage] PRIMARY KEY CLUSTERED ( [MessageID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 100) ON [PRIMARY]) ON [PRIMARY]GOALTER TABLE [ThreadMessage] WITH CHECK ADD CONSTRAINT [FK_ThreadMessage_ForumThread1] FOREIGN KEY([MessageThread])REFERENCES [ForumThread] ([ThreadID])GOALTER TABLE [ThreadMessage] CHECK CONSTRAINT [FK_ThreadMessage_ForumThread1]GO-----------------------------------------------INSERT INTO [ForumThread] ([ThreadSection],[OpenDate],[CloseDate],[Deleted]) VALUES (1,'1/1/2011','2/2/2012',0)GOINSERT INTO [ForumThread] ([ThreadSection],[OpenDate],[CloseDate],[Deleted]) VALUES (1,'1/10/2011','2/10/2012',0)GO------------------------------------INSERT INTO [ThreadMessage]([MessageSender],[MessageThread],[MessageParent],[MessageTitle] ,[MessageDate],[MessageContent],[UpdateDate],[Deleted])VALUES (1,1,0,'Introduction','1/1/2012','Please Introduce yourself to the class','1/1/2012',0)GOINSERT INTO [ThreadMessage]([MessageSender],[MessageThread],[MessageParent],[MessageTitle] ,[MessageDate],[MessageContent],[UpdateDate],[Deleted])VALUES (2,1,1,'RE:Introduction','1/2/2012','My Name is Mark. I am interested in sports.','1/2/2012',0)GOINSERT INTO [ThreadMessage]([MessageSender],[MessageThread],[MessageParent],[MessageTitle] ,[MessageDate],[MessageContent],[UpdateDate],[Deleted])VALUES (3,1,1,'RE:Introduction','1/2/2012','My Name is Sou. I am interested in music.','1/2/2012',0)GOINSERT INTO [ThreadMessage]([MessageSender],[MessageThread],[MessageParent],[MessageTitle] ,[MessageDate],[MessageContent],[UpdateDate],[Deleted])VALUES (4,1,2,'RE:Introduction','1/3/2012','My Name is Sam. Mark I think we met before. I am also interested in sports.','1/2/2012',0)GO------------------------------------INSERT INTO [ThreadMessage]([MessageSender],[MessageThread],[MessageParent],[MessageTitle] ,[MessageDate],[MessageContent],[UpdateDate],[Deleted])VALUES (1,2,0,'Chapter1','1/10/2012','Mention What you benefit from chapter1 ','1/10/2012',0)GOThank you |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-18 : 13:02:37
|
use common table expressionsdeclare @ThreadID intset @ThreadID = 1 --just an example value;With msgtempAS(select ft.threadid, tm.messagetitle,tm.messagecontent,cast(1 as int) as numberofreplies,messagedate,tm.MessageIDfrom [ForumThread] ftinner join [ThreadMessage] tmON ft.ThreadID = tm.MessageThreadwhere tm.MessageParent = 0and ft.ThreadID = @ThreadIDunion allselect tm.MessageThread, tm.messagetitle,tm.messagecontent,1,tm.messagedate,tm.MessageIDfrom msgtemp t--inner join [ForumThread] ft-- on ft.ThreadID = t.ThreadIDinner join [ThreadMessage] tmON t.ThreadID = tm.MessageThreadand t.MessageID = tm.MessageParent)SELECT *FROM msgtempoption (maxrecursion 0)output------------------------------------------------------------------------------------------------------------------------------------------------------------------------threadid messagetitle messagecontent numberofreplies messagedate MessageID-------------------------------------------------------------------------------------------------------------------------------------------------------------------------1 Introduction Please Introduce yourself to the class 1 2012-01-01 00:00:00.000 11 RE:Introduction My Name is Mark. I am interested in sports. 1 2012-01-02 00:00:00.000 21 RE:Introduction My Name is Sou. I am interested in music. 1 2012-01-02 00:00:00.000 31 RE:Introduction My Name is Sam. Mark I think we met before. I am also interested in sports. 1 2012-01-03 00:00:00.000 4 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-18 : 13:06:20
|
for count just use count() to get count of articles. i dont know how you will display the titles as there are multiple titles involved.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2012-03-18 : 16:27:08
|
Thank you for your reply.We need to display only threadid and its main message title. that is only threadid , messagetitle for those with parentmessage=0 but at the same time find number of replies and max messagedate for all messages with threadmessage = to that thread.Thank you againsarah |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-18 : 16:36:14
|
then change the code todeclare @ThreadID intset @ThreadID = 1 --just an example value;With msgtempAS(select ft.threadid, tm.messagetitle,tm.messagecontent,cast(1 as int) as numberofreplies,messagedate,tm.MessageID,tm.MessageParent from [ForumThread] ftinner join [ThreadMessage] tmON ft.ThreadID = tm.MessageThreadwhere tm.MessageParent = 0and ft.ThreadID = @ThreadIDunion allselect tm.MessageThread, tm.messagetitle,tm.messagecontent,1,tm.messagedate,tm.MessageID,tm.MessageParent from msgtemp t--inner join [ForumThread] ft-- on ft.ThreadID = t.ThreadIDinner join [ThreadMessage] tmON t.ThreadID = tm.MessageThreadand t.MessageID = tm.MessageParent)select threadid,count(MessageID) AS ReplyCount,max(MessageDate) AS LatestMEssageDate,MAX(case when MessageParent = 0 then messagetitle end) as ParentTitlefrom msgtempgroup by threadidoption (maxrecursion 0)output--------------------------------------------------------------------------------------------------------threadid ReplyCount LatestMEssageDate ParentTitle--------------------------------------------------------------------------------------------------------1 4 2012-01-03 00:00:00.000 Introduction change the @ThreadID accordingly to get associated info------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|