SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Help


Query Help

Author
Message
inayatkhan
inayatkhan
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 169
drop table #TypeCode
create table #TypeCode(TypeCodeId int identity(1,1), TypeCode bigint, Name varchar(50))
insert into #TypeCode values
(3001, 'Loan Type'),
(3004, 'Transaction Type'),
(3009, 'Event Type'),
(3015, 'Participant Type')
-- select * from #TypeCode

drop table #LD
create table #LD(TDID bigint identity(1,1), TranID bigint, LoanID bigint, [Description] varchar(500))
insert into #LD values
(1234, 5467, 'sdasdas asd asdas'),
(1232, 2357, 'nbvnvb vbjjytuytu yt'),
(1344, 7989, 'yuimhjkhj'),
(1734, 2456, 'retret ghjghjghj'),
(1854, 8883, 'ewrgghjg hhgj'),
(1784, 2357, 'ytuyu ghjghjghj'),
(1284, 9734, 'werewrew ewrwerw')
-- select * from #LD

drop table #PD
create table #PD(PDID bigint, TranID bigint, [Description] varchar(500))
insert into #PD values
(3456, 1234, 'fghfg fghf hfgh'),
(2135, 1234, 'sdjhjk hjkhj khjk'),
(7564, 1854, 'wewqg ghjghj hgj')
-- select * from #PD

drop table #EventLog
create table #EventLog(EID int, SourceID bigint, SourceType bigint, Description varchar(500))
insert into #EventLog values
(1, 1234, 3004, 'sadasdadadasfsdfsd fsdfsdf'), -- 3004 means its TranID from #LD
(2, 1234, 3004, 'erewrwerwerw '),
(3, 1234, 3004, 'erewsfsdf sdfsf sdf '),
(4, 5467, 3001, 'ertre rett sdfsf sdf '), -- 3001 means its LoanID from #LD
(5, 5467, 3001, 'kljkljkl rett sdfsf sdf '),
(6, 3456, 3015, 'utyutyu tyu yutyutuy '), -- 3015 means its PDID from #PD
(7, 1784, 3004, 'sadsadasdasd asdasdsad ')


Now basically in event log for same transaction id I have different source type id data. I have one store procedure which I pass only #LD TranID and I want it return all event log for all different source type data for that transaction.
Like If I pass TranID 1234 sp should return EID 1 to 6
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7623 Visits: 6431
You're not particularly clear about the relationships between each table so this is just a shot in the dark.


DECLARE @TranID BIGINT = '1234';

SELECT DISTINCT c.EID, c.SourceID, c.SourceType, c.Description
FROM #LD a
JOIN #PD b ON a.TranID = b.TranID
CROSS APPLY #EventLog c
WHERE a.TranID = @TranID AND SourceID IN (a.TranID, a.LoanID, b.PDID);





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Nevyn
Nevyn
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1558 Visits: 3149
Something like:



SELECT e.EID FROM #EventLog e
INNER JOIN #LD l
ON e.SourceID = l.TranId
WHERE l.TranID = 1234
AND e.SourceType = 3004
UNION ALL
SELECT e.EID FROM #EventLog e
INNER JOIN #LD l
ON e.SourceID = l.LoanId
WHERE l.TranId = 1234
AND e.SourceType = 3001
UNION ALL
SELECT e.EID FROM #EventLog e
INNER JOIN #PD p
ON e.SourceID = p.PDID
WHERE p.TranID = 1234
AND e.SourceType = 3015



You can try to do it in one query with some outer joins and case statements in the ON clauses, but this way just looks cleaner. You can switch from union all to union if you want to filter out duplicates, and add any fields from #EventLog you want to select.
inayatkhan
inayatkhan
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 169
Thanks its solve my issue
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search