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 |
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2006-11-06 : 01:55:28
|
Hi,I have the following query, which is running fine however I'd like to add a bit of functionality on to it. This selects the current INBOUND for each user. I want to add 2 more columns, OUTBOUND which is generated the same way with a JOIN onto IM.messageFromID instead of IM.messageToID. The total could be derived from adding these two together, or doing a seperate query. The first idea is probably much more efficient. I'm having a tough time figuring this one out but I don't think it should really be that hard =Thanks a bunch for any tips! I have included the SPROC below and table structure. Any questions please let me knowthanks once again,mike123CREATE procedure dbo.select_Most_InstantMessages_InBound AS SET NOCOUNT ONSELECT top 100 nameOnline, userID, COUNT(IM.instantMessageID) AS IMCount_InboundFROM tblUserDetails UDINNER JOIN tblinstantmessage IM ON UD.userID = IM.messageToIDGROUP BY nameOnline, UD.userIDORDER BY IMCount_Inbound descGOHere is the tableCREATE TABLE [dbo].[tblInstantMessage] ( [InstantMessageID] [int] IDENTITY (1, 1) NOT NULL , [MessageToID] [int] NULL , [MessageFromID] [int] NULL , [Message] [varchar] (500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [Date] [smalldatetime] NOT NULL , [Checked] [tinyint] NULL , [Mobile] [tinyint] NULL , [deletedbySender] [int] NULL , [deletedbyRecipient] [int] NULL ) ON [PRIMARY]GO |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-11-06 : 02:12:47
|
[code]CREATE procedure dbo.select_Most_InstantMessages_InBoundAs SELECT top 100 nameOnline, userID, ( Select COUNT(IM.instantMessageID) From tblinstantmessage IM ON UD.userID = IM.messageToID )AS IMCount_Inbound( Select COUNT(IM.messageFromID) From tblinstantmessage IM ON UD.userID = IM.messageFromID )AS IMCount_outboundFROM tblUserDetails UDGROUP BY nameOnline, UD.userIDORDER BY IMCount_Inbound desc[/code]Chiraghttp://chirikworld.blogspot.com/ |
 |
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2006-11-06 : 02:44:25
|
Hi, Chiragkhabariathanks for the tip, I get this error when creating the SPROC. I think "JOIN" is supposed to be in there?Also, I'd like to add a SUM(IMCount_outbound + IMCount_inbound) AS IMCount_total and then ORDER BY IMCount_total thanks very much for your help!!mike123Server: Msg 156, Level 15, State 1, Procedure select_Most_InstantMessages_Total, Line 5Incorrect syntax near the keyword 'ON'.Server: Msg 156, Level 15, State 1, Procedure select_Most_InstantMessages_Total, Line 8Incorrect syntax near the keyword 'ON'. |
 |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-11-06 : 02:51:14
|
opps try this CREATE procedure dbo.select_Most_InstantMessages_InBoundAs SELECT top 100 nameOnline, userID, ( Select COUNT(IM.instantMessageID) From tblinstantmessage IM ON Where UD.userID = IM.messageToID )AS IMCount_Inbound( Select COUNT(IM.messageFromID) From tblinstantmessage IM Where WUD.userID = IM.messageFromID )AS IMCount_outboundFROM tblUserDetails UDGROUP BY nameOnline, UD.userIDORDER BY IMCount_Inbound desc Chiraghttp://chirikworld.blogspot.com/ |
 |
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2006-11-06 : 02:56:59
|
great thanks got it going .. do you know how I could add this one part I would like to on? I am having problems adding the total line I have bolded out.. if possible I'd also to ORDER BY this columnthanks once again :) much apprecaited!mike123alter procedure dbo.select_Most_InstantMessages_TotalAs SELECT top 100 nameOnline, userID, ( Select COUNT(IM.instantMessageID) From tblinstantmessage IM Where UD.userID = IM.messageToID )AS IMCount_Inbound,( Select COUNT(IM.messageFromID) From tblinstantmessage IM Where UD.userID = IM.messageFromID )AS IMCount_outbound,SUM(IMCount_Inbound + IMCount_OutBound) as IMCount_TotalFROM tblUserDetails UDGROUP BY nameOnline, UD.userIDORDER BY IMCount_Inbound desc |
 |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-11-06 : 03:03:08
|
[code]alter procedure dbo.select_Most_InstantMessages_TotalAsSelect NameOnLine,UserID,IMCount_Inbound,IMCount_outbound,SUM(IMCount_Inbound + IMCount_OutBound) as IMCount_TotalFrom ( SELECT top 100 nameOnline, userID, ( Select COUNT(IM.instantMessageID) From tblinstantmessage IM Where UD.userID = IM.messageToID )AS IMCount_Inbound, ( Select COUNT(IM.messageFromID) From tblinstantmessage IM Where UD.userID = IM.messageFromID )AS IMCount_outbound, FROM tblUserDetails UD GROUP BY nameOnline, UD.userID) As a Order by IMCount_Total[/code]Chiraghttp://chirikworld.blogspot.com/ |
 |
|
|
|
|
|
|