Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
please Dont Use Temptable Because Of Performance Issue ، I have A time With A cardNo that i want to found Related Personnel Base On some Effective Date. each Personnel can have Multiple EffectiveDate With Diffrent CardNo

SQL
<pre>CREATE PROCEDURE [dbo].[PersonnelDetails_GetPersonnelByCardNoAndTime]
(
@CardNo VARCHAR(16),
@Time SMALLDATETIME
)
AS
DECLARE @PersonnelBaseID INT;
DECLARE @EffectiveDate SMALLDATETIME;

SELECT EffectiveDate,PersonnelBaseID,CardNo INTO #PersonnelDetails FROM 
(
	SELECT
		personnelDetails.EffectiveDate, personnelDetails.PersonnelBaseID, CardNo 
	FROM
		dbo.tkp_PersonnelDetails AS personnelDetails 
	INNER JOIN
	(
		SELECT
				MAX(EffectiveDate) AS EffectiveDate,details.PersonnelBaseID
		FROM            
			dbo.tkp_PersonnelDetails AS details INNER JOIN
			prs_Personnel personnel ON personnel.ID = details.PersonnelBaseID AND 
			Personnel.Active = 1 AND 
			Personnel.Deleted = 0 AND
			details.Deleted = 0 AND 
			details.EffectiveDate <= @Time
		GROUP BY PersonnelBaseID
	)result ON (result.PersonnelBaseID = personnelDetails.PersonnelBaseID AND result.EffectiveDate = personnelDetails.EffectiveDate AND personnelDetails.Deleted = 0  AND CardNo = @CardNo)
)details 
WHERE 
	CardNo = @CardNo
ORDER BY details.Effectivedate DESC

SELECT TOP 1 
	@PersonnelBaseID = PersonnelBaseID ,
	@EffectiveDate = EffectiveDate,
	@CardNo = CardNo
FROM 
	#PersonnelDetails 
ORDER BY Effectivedate DESC
 IF ((SELECT COUNT(DISTINCT PersonnelBaseID) FROM #PersonnelDetails WHERE EffectiveDate = @EffectiveDate AND CardNo = @CardNo) > 1)--handle Multi Personnel With On CardNo In same EffectiveDate :(
	 SELECT 0;
 ELSE
	SELECT @PersonnelBaseID;
DROP TABLE #PersonnelDetails


What I have tried:

please Dont Use Temptable Because Of Performance Issue ، I have A time With A cardNo that i want to found Related Personnel Base On some Effective Date. each Personnel can have Multiple EffectiveDate With Diffrent CardNo
Posted
Updated 7-Jan-18 19:08pm
v5
Comments
PIEBALDconsult 7-Jan-18 11:38am    
Is all the code there? It looks like parts are missing. Not all parentheses are closed and what is that ELSE doing in there?
Avoid subqueries; try using Common Table Expessions instead.
And why go through all that trouble just to get a count?
mhd.sbt 8-Jan-18 0:56am    
Hello PIEBALDconsult :)
In this businesses there are sum personnel With same Effectivedate and one CardNo،
I want To Filter This personnel ، If this query face With this condition we must return 0 and in another case We must return related Personnel with carno :(
and this new script is worked Correctly ، bu has Low Perfomance With Large Data
PIEBALDconsult 8-Jan-18 18:14pm    
Uh huh. And the answers to my questions...?
mhd.sbt 12-Jan-18 12:17pm    
which one?
PIEBALDconsult 12-Jan-18 14:58pm    
I count three.

1 solution

Hopefully, try the analytical functions[^]
 
Share this answer
 

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