October 8, 2008 at 10:45 pm
Hello All!!!
I need to take diff of two dates,comapring two other columns,row by row.So,I have used Cursor for that,its is working fine...But its taking time.
I did find tht cursors can be replaced with derived tables.
I am new to SQL...I m gving the code here
Please Help!!!
USE HR_Recruitment_Operational_Reports_StagingDb
DECLARE @a AS DATETIME ,@B AS DATETIME,@C AS BIGINT,@D AS BIGINT,@Id AS BIGINT
DECLARE MyCursor CURSOR FOR SELECT MIN(RIV_ID),MAX(RIV_ID),RIV_RCN_ID FROM ABACUSNEWDATA_REC_INTERVIEW GROUP BY RIV_RCN_ID
OPEN MyCursor
FETCH NEXT FROM MyCursor
INTO @C,@D,@Id
WHILE @@FETCH_STATUS = 0
BEGIN
SET @a = (SELECT RIV_CREATED_ON FROM ABACUSNEWDATA_REC_INTERVIEW WHERE RIV_ID = @C)
SET @b-2 = (SELECT RIV_CREATED_ON FROM ABACUSNEWDATA_REC_INTERVIEW WHERE RIV_ID = @D)
print (@Id) print(DATEDIFF(day,@A,@B)) --FROM ABACUS_REC_INTERVIEW --GROUP BY RIV_RCN_ID
FETCH NEXT FROM MyCursor
INTO @C,@D,@Id
END
CLOSE MyCursor
DEALLOCATE MyCursor
October 9, 2008 at 12:36 am
Hello
This is also untested but should be pretty close to what you're looking for - if it isn't, please post DDL of your table and some sample data.
[font="Courier New"]SELECT d1.RIV_RCN_ID, DATEDIFF(DAY, d2.RIV_CREATED_ON, d3.RIV_CREATED_ON)
FROM (SELECT MIN(RIV_ID) AS MIN_RIV_ID, MAX(RIV_ID) AS MAX_RIV_ID, RIV_RCN_ID
FROM ABACUSNEWDATA_REC_INTERVIEW
GROUP BY RIV_RCN_ID) d1
INNER JOIN ABACUSNEWDATA_REC_INTERVIEW d2 ON d2.RIV_RCN_ID = d1.RIV_RCN_ID AND d2.RIV_ID = d1.MIN_RIV_ID
INNER JOIN ABACUSNEWDATA_REC_INTERVIEW d3 ON d3.RIV_RCN_ID = d1.RIV_RCN_ID AND d3.RIV_ID = d1.MAX_RIV_ID
[/font]
Wandrag - your solution assumes that the min and max RIV_CREATED_ON per RIV_RCN_ID is the same thing as the RIV_CREATED_ON on the min RIV_ID row and max RIV_ID row per RIV_RCN_ID. They may not be the same.
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 9, 2008 at 6:26 am
I found it myself...Thankss a lot anywayz...
October 9, 2008 at 6:29 am
bharathi (10/9/2008)
I found it myself...Thankss a lot anywayz...
Hi bharathi
Please would you post your solution? It would finish this thread nicely and also, possibly, provide useful learning material for others.
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 9, 2008 at 8:21 pm
bharathi (10/9/2008)
I found it myself...Thankss a lot anywayz...
Two way street here... would you please post your solution? Thanks. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
October 9, 2008 at 9:07 pm
Hi Chris!!!
I could do the same way as wandrag did without derived tables...
And I tried ur query..it worked perfectly....
Thanku for ur Help.....
Regards,
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply