Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Just want to understand

Author  Topic 

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2014-03-10 : 04:49:03
Hi All,
Just want to know that is it good practice to create multiple INNER JOIN on derived tables as I have written below or use a #temp table
for all derived tables and use them into JOIN. Please advise .
This below query is also very hard to understand what is going on .


CREATE TABLE #Temp
(
NumPlayers INT,
ModuleID INT,
ClientId INT,
ASF_Version VARCHAR(10),
ASF_VersionHead INT
);

INSERT INTO #Temp

SELECT NumPlayers=Count( DISTINCT UserId), ModuleID, ClientId, ASF_Version, ASF_VersionHead

FROM (SELECT t.UserID,
m.ModuleID,
m.ClientID,
t.UserTransNumber,
t.EventNumber,
t.GameData,
CAST(t.GameData AS XML).value('(/X/@version)[1]', 'varchar(50)') AS ASF_Version,

(SELECT value FROM fn_SplitDelimitedList(Cast(t.Gamedata AS XML).value('(/X/@version)[1]', 'varchar(50)'), '.') WHERE idx = 0) AS ASF_VersionHead

FROM (SELECT ugd.UserID,
Max(EventNumber) AS max_EventNumber,
cg.ModuleID,
cg.ClientID
FROM dbo.tb_UserGameData ugd (nolock)

JOIN ( SELECT UserID,
ModuleID,
ClientID,
TransNumber
FROM dbo.tb_CurrentGame (nolock)) cg

ON cg.UserId = ugd.UserId
AND cg.TransNumber = ugd.UserTransNumber
GROUP BY ugd.UserID, cg.ModuleID, cg.ClientID) AS m
JOIN dbo.tb_UserGameData AS t
ON t.UserID = m.UserID
AND t.EventNumber = m.max_EventNumber) AS x

GROUP BY ModuleID, ClientId, ASF_Version, ASF_VersionHead
HAVING ASF_VersionHead < 1000
ORDER BY ModuleID, ASF_Version

DELETE FROM #Temp
WHERE ModuleId IN (Select ModuleID from #temp where ASF_VersionHead > 130 )

SELECT * FROM #Temp

IF(OBJECT_ID('tempdb..#Temp') Is Not Null)
BEGIN
DROP TABLE #Temp
END


Vijay is here to learn something from you guys.

maunishq
Yak Posting Veteran

71 Posts

Posted - 2014-03-10 : 15:28:45
Hi Vijay,
In the above query, a temporary table is created. It is filled with the values that are derived using multiple joins (i.e. joining multiple tables).

=======================
Not an Expert, Just a learner.
!_(M)_!
Go to Top of Page
   

- Advertisement -