how can i optimize this code in SQL
INSERT INTO #ActivePlayers ([PlayerId])
SELECT DISTINCT([OwnerSID]) [PlayerId]
FROM [WarehouseMgmt].[FactLoginTrans] FLT
JOIN [WarehouseMgmt].[DimPlayer] DP ON DP.[Id] = FLT.[OwnerSID]
WHERE [IsSystemUser]=0
AND [OwnerSID]>0
AND [WarehouseReports].[ConvertfromUTCtoTZ] (FLT.[LogonTime],@ZoneCode) BETWEEN DATEADD(month, -13, @Date) AND DATEADD(month, -1, @Date)
What's the best option for [WarehouseReports].[ConvertfromUTCtoTZ] (FLT.[LogonTime],@ZoneCode) BETWEEN DATEADD(month, -13, @Date) AND DATEADD(month, -1, @Date) , because this take me some time and i repeat this line in few SQL queries.
How to put this into temp table and use it later in query ?
@ZoneCodeis a constant instead of passingFLT.LogonTimeto the function for every row, you could just pass the constant@Date, thus only calling the function once? Alternatively, if you post the definition it is possible that you could convert the function to an inline table valued function, which performs significantly better than a scalar function.