Click here to Skip to main content
15,917,622 members
Home / Discussions / Database
   

Database

 
GeneralRe: Random Select Pin
PIEBALDconsult24-Aug-07 7:57
mvePIEBALDconsult24-Aug-07 7:57 
QuestionSQL query Pin
Milind Panchal23-Aug-07 6:56
Milind Panchal23-Aug-07 6:56 
AnswerRe: SQL query Pin
Michael Potter23-Aug-07 8:20
Michael Potter23-Aug-07 8:20 
GeneralRe: SQL query Pin
Milind Panchal23-Aug-07 18:29
Milind Panchal23-Aug-07 18:29 
GeneralRe: SQL query Pin
Rocky#23-Aug-07 20:11
Rocky#23-Aug-07 20:11 
GeneralRe: SQL query Pin
Michael Potter24-Aug-07 5:10
Michael Potter24-Aug-07 5:10 
QuestionHow to upload an Ntext column value to a text file Pin
edukulla23-Aug-07 6:48
edukulla23-Aug-07 6:48 
QuestionHow can I optimize this script? Pin
Skanless23-Aug-07 6:11
Skanless23-Aug-07 6:11 
I am trying to create a reporting system which will give me the "Customer ID", "Number of Messages sent", "Is Message a part of a thread or Distinct", Message Status (i.e. New, Under Review, Cancelled or Resolved) within a given period. I honestly do not see no need for a curso in here but I believe one can be used but I do not understand the benefit of using one. The search is working fine except for the last "Select" prior to dropping the table is not providing Distinct User's. Users with multiple messages/Threads are being pulled up as many time as the amount of records found for them. It is the result set from the Last select that is displayed in the GUI.

Any assistance given will be appreciated.



SET NOCOUNT ON /* Don't return row counting to caller */

CREATE TABLE #MS_MessagesFromCustomer(
NumOfMessages BIGINT,
CustomerID VARCHAR(100),
Period datetime,
ThreadID int,
MessageStatus int

)

CREATE TABLE #MS_InquiriesByCustomer(
ThreadID int,
CustomerID VARCHAR(100),
TotalMessages int,
New int,
UnderReview int,
Cancelled int,
Resolved int,
TotalThreads int


)

Insert into #MS_MessagesFromCustomer(CustomerID, NumOfMessages, Period, ThreadID, MessageStatus)
Select MSM.SenderID, count(MSM.MessageID)Messages, Convert(varchar, MSM.SentDate, 101) [Sent Date],MSM.ThreadID,MST.CurrentStatusID [Current Status]
from dbo.MS_Messages MSM INNER JOIN dbo.MS_Threads MST ON MSM.ThreadID = MST.ThreadID
where MSM.SentDate >'3/1/2005'
and MSM.SentDate < '3/31/2005'
group by MSM.SenderID, MSM.SentDate, MSM.ThreadID, MST.CurrentStatusID
order by SenderID

--Select * from #MS_MessagesFromCustomer


INSERT INTO #MS_InquiriesByCustomer(ThreadID,CustomerID, TotalMessages, New, UnderReview, Cancelled,Resolved, TotalThreads)
Select Distinct(ThreadID)'ThreadID', CustomerID'Customer' , count(NumOfMessages) 'Total Messages Recevied',COUNT(CASE MessageStatus when 19 THEN MessageStatus end) AS 'New',
COUNT(CASE MessageStatus WHEN 20 THEN MessageStatus end) AS 'UnderReview',
COUNT(CASE MessageStatus WHEN 21 THEN MessageStatus end) AS 'Cancelled',
COUNT(CASE MessageStatus WHEN 22 THEN MessageStatus end) AS 'Resolved',
(COUNT(CASE MessageStatus WHEN 19 THEN MessageStatus end) +
COUNT(CASE MessageStatus WHEN 20 THEN MessageStatus end) +
COUNT(CASE MessageStatus WHEN 21 THEN MessageStatus end) +
COUNT(CASE MessageStatus WHEN 22 THEN MessageStatus end)) AS 'Total Threads'


from #MS_MessagesFromCustomer
group by customerID, ThreadID
order by customerID

SELECT Distinct(CustomerID),* FROM #MS_InquiriesByCustomer
Select * from #MS_MessagesFromCustomer


Select Distinct(CustomerID), TotalMessages 'Total Messages Recevied', New 'New', UnderReview 'Under Review', Cancelled 'Cancelled',Resolved 'Resolved', count(TotalThreads) 'Total Threads'
FROM #MS_InquiriesByCustomer
GROUP BY customerID,ThreadID, TotalThreads,TotalMessages, New, UnderReview, Cancelled, Resolved
ORDER BY CustomerID

DROP TABLE #MS_MessagesFromCustomer
DROP TABLE #MS_InquiriesByCustomer

Skan

If you knew it would not compile why didn't you tell me?!?!?!

AnswerRe: How can I optimize this script? Pin
Michael Potter23-Aug-07 8:41
Michael Potter23-Aug-07 8:41 
Questionreturn int (urgent) [modified] Pin
ksaw12323-Aug-07 5:28
ksaw12323-Aug-07 5:28 
AnswerRe: return int (urgent) Pin
Colin Angus Mackay23-Aug-07 5:34
Colin Angus Mackay23-Aug-07 5:34 
GeneralRe: return int (urgent) [modified] Pin
ksaw12323-Aug-07 5:38
ksaw12323-Aug-07 5:38 
GeneralRe: return int (urgent) Pin
Xandip24-Aug-07 17:31
Xandip24-Aug-07 17:31 
GeneralRe: return int (urgent) Pin
ksaw12325-Aug-07 0:14
ksaw12325-Aug-07 0:14 
Questionloop in sql table Pin
costavo23-Aug-07 4:06
costavo23-Aug-07 4:06 
AnswerRe: loop in sql table Pin
Colin Angus Mackay23-Aug-07 5:30
Colin Angus Mackay23-Aug-07 5:30 
QuestionConnect to sql server Pin
messages23-Aug-07 3:56
messages23-Aug-07 3:56 
AnswerRe: Connect to sql server Pin
WhiteGirl2323-Aug-07 4:00
WhiteGirl2323-Aug-07 4:00 
GeneralRe: Connect to sql server Pin
messages23-Aug-07 4:14
messages23-Aug-07 4:14 
AnswerRe: Connect to sql server Pin
Colin Angus Mackay23-Aug-07 4:03
Colin Angus Mackay23-Aug-07 4:03 
GeneralRe: Connect to sql server Pin
messages23-Aug-07 4:17
messages23-Aug-07 4:17 
GeneralRe: Connect to sql server Pin
Colin Angus Mackay23-Aug-07 5:35
Colin Angus Mackay23-Aug-07 5:35 
GeneralRe: Connect to sql server Pin
Colin Angus Mackay23-Aug-07 5:37
Colin Angus Mackay23-Aug-07 5:37 
GeneralRe: Connect to sql server Pin
messages23-Aug-07 6:55
messages23-Aug-07 6:55 
AnswerRe: Connect to sql server Pin
martin_hughes23-Aug-07 13:21
martin_hughes23-Aug-07 13:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.