i have one crm application. i worked on message threading and found one trouble. when i have to show no of new replies from multiple inquiries that was raised by per user. as shortly like facebook that show 'comment counter' when u login to ur account then it's shows no of new replies. how ever i just only want to count not showing summary report.
here i have include my table hierarchy that give basic idea.
i just count no of new replies that given by staff user to multiple inquiry raised by end user. how ever i want to count total no of AnswerId's(Replies that given by staff users) while user sign in to his account.
please give me idea how i implement this.
declare @user_id int -- I assume this is an integer
set @user_id=123 -- the user_id you are interested in
select count(*)
from tbl_answer a
join tbl_question q on q.id=a.question_id
join tbl_inquiry_history i on i.inquiry_id=q.inquiry_id
where i.user_id = @user_id
To notify the user of "new" answers since they last logged in, you have two major options:-
- Store the number of answers the user knows about on the user record - so that next time they log in, you can tell them how many "new" answers have been created. This is called a "high watermark". The user record has 10 stored as the high water mark. They log in. The system counts how many answers the user now has and finds 15 answers - so 5 must be new - and adds a message count to the user interface to say "5 new answers" (and updates the high watermark to 15).
- Store a flag on each answer to record if the user has read that particular answer. Whenever an answer is added - the flag is not set. Whenever the user reads the answer the flag is set. Periodically the system counts how many answers exist for the user that don't have the flag set - and displays a message if it finds any.
Option 1 is "quick and dirty". It's easy to implement. It's also the least useful. The next thing the user is going to ask is "ok. so which answers are new?". If you can sort the answers into the order they have been created, you can (from the example above) show them the latest 5 answers - but that's about as much as you can do. They have no granularity to be able to go read one of the answers and return to the list and have the list shorten (taking out the answer they just read) - without you (as the developer) jumping through some complex hoops to manipulate the list.
Option 2 is the more complex solution but provides the type of interface that the user is likely to expect (like Facebook - or StackOverflow). The clever thing is not to use a bool/bit as your flag - which only allows you to keep track of whether the user has read the answer. If you use a datetime, you can also keep track of when they read the answer - which allows for much more analysis of the way the system is being used.
You (or rather, your users) will need to decide which approach you use - a simple high watermark or a granular flag on each answer. If you start with option 1, you will almost certainly have to implement option 2 at some point - so you may as well byte (lol) the bullet and do it "right" first time.