Suppose you have a table TempTable having Date Column given below.
Now you have to fecth the data of date column whose diff. between one date to second date of student is >= 20.
| Student | Date |
| A | 20/11/2021 |
| A | 20/11/2022 |
| B | 19/11/2019 |
| B | 19/12/2020 |
Suppose you have a table TempTable having Date Column given below.
Now you have to fecth the data of date column whose diff. between one date to second date of student is >= 20.
| Student | Date |
| A | 20/11/2021 |
| A | 20/11/2022 |
| B | 19/11/2019 |
| B | 19/12/2020 |
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pankajkumar PatelRank #181 · 13.4K reputationPosted Nov 29, 2022, 6:54 AM EST
Hi Brahma Prakash Shukla,
You can solve SQL query mentioned problem as like following:
DECLARE @Students TABLE ( StudentId int NOT NULL, StudentName varchar(20), AdmissionDate datetime ) INSERT INTO @Students (StudentId, StudentName, AdmissionDate) VALUES (1, 'A', '2021-11-20') INSERT INTO @Students (StudentId, StudentName, AdmissionDate) VALUES (2, 'A', '2022-11-20') INSERT INTO @Students (StudentId, StudentName, AdmissionDate) VALUES (3, 'B', '2019-11-19') INSERT INTO @Students (StudentId, StudentName, AdmissionDate) VALUES (4, 'B', '2020-12-19') declare @days as int = 365 SELECT distinct t1.StudentName,DATEDIFF(day,t1.AdmissionDate,t2.AdmissionDate) - @days as 'Difference', t1.AdmissionDate, t2.AdmissionDate FROM @Students t1 INNER JOIN @Students t2 on t1.StudentName = t2.StudentName WHERE DATEDIFF(day,t1.AdmissionDate,t2.AdmissionDate) > @daysOutput:
For more details:
Hope this will help you!