help me please | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

help me please

Employee Date Enquired Title Company Bryan Cole 2007-05-01 Recruiter ABC
Bryan Cole 2007-05-15 Recruiter ABC
Bryan Cole 2007-05-21 Recruiter ABCBryan Cole 2007-06-15 Recruiter ABC
Bryan Cole 2007-07-01 Recruiter ABCBryan Cole 2007-07-30 Recruiter ABC
I have to do row by row date comparisons in a date column. If the date difference is more than 30 days we keep it , otherwise we suppress it. How can we write the query without using cursor so that only the bold rows will come ?
You have to use NOT EXISTS to get only the hightlighted rows. See if you can grasp the logic here:
SELECT t1.* FROM MyTable t1
WHERE NOT EXISTS
(SELECT t2.* FROM MyTable t2
WHERE t2.Employee = t1.Employee AND t2.Title = t1.Title AND t2.Company = t1.Company
AND t2.DateEnquired BETWEEN DATEADD(d,-30,t1.DateEnquired) AND t1.DateEnquired)
OR NOT EXISTS
(SELECT t2.* FROM MyTable t2
WHERE t2.Employee = t1.Employee AND t2.Title = t1.Title AND t2.Company = t1.Company
AND t2.DateEnquired < t1.DateEnquired)

]]>

Software Reviews | Book Reviews | FAQs | Tips | Articles | Performance Tuning | Audit | BI | Clustering | Developer | Reporting | DBA | ASP.NET Ado | Views tips | | Developer FAQs | Replication Tips | OS Tips | Misc Tips | Index Tuning Tips | Hints Tips | High Availability Tips | Hardware Tips | ETL Tips | Components Tips | Configuration Tips | App Dev Tips | OLAP Tips | Admin Tips | Software Reviews | Error | Clustering FAQs | Performance Tuning FAQs | DBA FAQs |