vidkshi


Hi,

I have 4 tables :

Code Block

Create Table #Request ( [requestid] int , [stateno] nvarchar(5) , [cityno] int , Callid int, UniqueNo int);

Create Table #RequestDetail(reqdetailid int, [customername] Varchar(20), [customerage] int, requestid int);

Create Table #Call(Callid int,Calltype int,callDetailid int )

Create Table #CallDetail(callDetailId int,empid int)

Insert into #CallDetail VALUES(12123,1)

Insert into #CallDetail VALUES(53423,1)

Insert into #CallDetail VALUES(6532,1)

Insert into #CallDetail VALUES(82323,1)

Insert into #CallDetail VALUES(124235,1)

Insert Into #Call VALUES(111,1,12123)

Insert Into #Call VALUES(112,1,53423)

Insert Into #Call VALUES(114,1,6532)

Insert Into #Call VALUES(123,2,6532)

Insert Into #Call VALUES(134,1,124235)

Insert Into #request Values('324234','SA023',12,111,0);

Insert Into #request Values('223452','SA023',12,112,0);

Insert Into #request Values('456456','SA024',12,114,0);

Insert Into #request Values('22322362','SA024',44,123,0);

Insert Into #request Values('22654392','SA023',12,134,0);

Insert into #requestdetail values(1, 'Bill',23,'324234')

Insert into #requestdetail values(2, 'Tom',25,'223452')

Insert into #requestdetail values(3, 'Bobby',27,'456456')

Insert into #requestdetail values(4, 'Guck',29,'22322362')

Insert into #requestdetail values(5, 'Bobby',33,'22654392')

1. My stored proc will take the CallDetailID.

2. I have to find out the requests made on this calldetail.

3. After getting the request, i want to take the Customername, cityno of the request/requestdetail and pass it again to #request table to search for any duplicates within the request.

4. If found return the details of the original record :

[A similar requirement was solved earlier, but the structure has changed. This is a seperate requirement with different table strucure. Hence a new post. ]


thanks




Re: searching similar records within a table

vidkshi


anyone






Re: searching similar records within a table

DaleJ

Code Block

select cd.*

from #calldetail cd

inner join #call c

on cd.calldetailid = c.calldetailid

inner join #request r

on c.callid = r.callid

inner join #requestdetail rd

on r.requestid = rd.requestid

where exists

(

select 1

from #request r2

inner join #requestdetail rd2

on r2.requestid = rd2.requestid

where r.cityno = r2.cityno

and rd.customername = rd2.customername

and r.requestid <> r2.requestid

)

and cd.calldetailid = 6532







Re: searching similar records within a table

vidkshi

Hi,

thanks for the query. The query returns the output as the same calldetailid that was passed

Code Block

CURRENT OUTPUT

6532 1 456456

Code Block

DESIRED OUTPUT

'22654392','SA023','Bobby',33

I tried fetching the records using r2 and rd2 in the select, but it says

The multi-part identifier "r2.requestid" could not be bound.

Any clues plz help

thanks.





Re: searching similar records within a table

DaleJ

Original Post:
4. If found return the details of the original record :

Does that mean you want all the duplicates (not including the requested item) to be returned

What if there is more than one






Re: searching similar records within a table

vidkshi

yes. And I am sorry to write original record. What i meant was to return all duplicates.

Can you please suggest the modified query.

I take in the batch id. Fetch the request details. Search the duplicate requests based on the request details. Return those duplicate requests. The desired output is mentioned previously.

thanks.





Re: searching similar records within a table

DaleJ

Code Block

select r.RequestId, r.stateno, rd.customername, rd.customerage

from #calldetail cd

inner join #call c

on cd.calldetailid = c.calldetailid

inner join #request r

on c.callid = r.callid

inner join #requestdetail rd

on r.requestid = rd.requestid

inner join

(

select cd2.callDetailId, r2.cityno, rd2.customername

from #calldetail cd2

inner join #call c2

on cd2.calldetailid = c2.calldetailid

inner join #request r2

on c2.callid = r2.callid

inner join #requestdetail rd2

on r2.requestid = rd2.requestid

and cd2.calldetailid = 6532

) as base

on cd.callDetailId <> base.callDetailId

and r.cityno = base.cityno

and rd.customername = base.customername