Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
TblChatMaster
---------------------
ChatId    CreatedDate

 4           15/05/2013   

 5           16/05/2013   




TblChatDesc
----------------------
id   ChatId   ReceiverId

 1     4           2

 2     4           3

 3     5           2




TblMessageMaster
---------------------------------

 id   ChatId  MsgText    MessengerId

  1     4      'Hi'         1

  2     5      'Hello'      1





I have ReceiverId and MessengerId.

Let we have,

MessengerId = 1
ReceiverId = 2,3

Now I want to get that ChatId which have messengerId=1 and ReceiverId=2 & 3
By above conclusion chatId should be 4

-----I am doing following way but it is not work
SQL
select cm.ChatId from TblChatMaster cm
 join TblChatDesc cd on cm.ChatId=cd.ChatId
 join TblMessageMaster mm on cm.ChatId=mm.ChatId where ReceiverId in(select * from dbo.Split('2,3')) and MessengerId=1
Posted
Updated 20-May-13 0:21am
v2
Comments
CHill60 20-May-13 6:31am    
When you say "is not work" what happens, what doesn't happen or what error message do you get?
rajesh@1989 20-May-13 6:37am    
It shown two different chatId 4 and 5

make sure
select * from dbo.Split('2,3')

returns proper result

or try this
SQL
where (ReceiverId  = 2 or ReceiverId = 3)  and ...

Happy Coding!
:)
 
Share this answer
 
v2
Comments
rajesh@1989 20-May-13 6:39am    
Sorry I didn't explain it. dbo.Split is a method that is split the value into rows where comma is founded.
Replace
where ReceiverId in(select * from dbo.Split('2,3'))
with
where ReceiverId in (2,3)
SQL
where cm.ChatId in (Select ChatId from tblChatDesc where ReceiverId = 2)
and cm.ChatId in (Select ChatId from tblChatDesc where ReceiverId = 3)

[Attempt 3!!]
I can't return temporary tables from a stored procedure in the version of SQL I'm currently using so you may need to tweak this code to make it work on your version and to simplify it
Basically, I'm using group by on both ChatId and ReceiverId to get a temporary table in the format
ChatId	ReceiverId	c
4	2			1
4	3			1
5	2			1

Then I'm using an outer select, also with a group by ... but this time I just want the ChatId and only if it appears in the temporary table exactly twice ...
SQL
select ChatId from
(
    select cm.ChatId, ReceiverId, count(*) as c
    from #ChatMaster cm
    join #ChatDesc cd on cm.ChatId=cd.ChatId
    join #MessageMaster mm on cd.ChatId=mm.ChatId
    where MessengerId=1
    and cm.ChatId in (Select ChatId from #ChatDesc where ReceiverId in (2,3))
    group by cm.ChatId, ReceiverId
) tmp
group by ChatId
having count(*) = 2 -- replace this 2 with the count of items returned by the split function
 
Share this answer
 
v3
Comments
rajesh@1989 20-May-13 6:48am    
It display two different chatId 4 and 5
CHill60 20-May-13 7:02am    
I've updated my solution
rajesh@1989 20-May-13 7:32am    
but how could I separate ReceiverId ? and ReceiverId may be 2,3,4 then By above conclusion I need to add an extra condition. How could I do that!!
CHill60 20-May-13 7:35am    
So you are calling this code/function with a string that contains a comma-separated list of the values that you need?
rajesh@1989 20-May-13 8:04am    
Yes, Exactly!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900