Solved

Date Difference between two different columns and rows

Posted on 2007-11-15
5
1,945 Views
Last Modified: 2008-02-01
Hi Experts:

I had asked this question before but never received a correct answer.  I'm currently using SQL 2000 and need to calculate the following:

EffDT                  TermDT           Status
20071101                                      A
20070801           20071031           D
20070601           20070630           A <-------- Error
20070501           20070531           A

The above shows dates of member eligibility.  Everytime that a new eligibility sequence (or line) is added the prior one is closed with the termdt and a new one is open.  I need to make sure there are no gaps in the dates.  For example if you like at the line marked with error,  the line has a term date of 20070630 and the top line has an effective date of 20070801.  This is incorrect because there's a time gap (July).  The correct termdt for the error line should be 20070731 or the effective date on the next line should be 20070701.  I can't figure out how to have SQL read the effdt on the top line and do a datediff of one day with the termdt in the bottom line.
0
Comment
Question by:IEHP1
  • 3
5 Comments
 
LVL 4

Expert Comment

by:rschoenbach
ID: 20291123
Please post the sql query you run to analyze this.
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20291480
here's something to get you started
select a.effdt, a.status,

 a.termdt,

 dateadd(d, -1, b.effdt) as correct_termdt,

 b.effdt as nexteffdt

from #temp a

 inner join #temp b on b.seq = a.seq + 1

order by a.effdt

Open in new window

0
 

Author Comment

by:IEHP1
ID: 20291490
SELECT t1.abeligno,t1.absubno, t1.abeffdt, t1.abtermdt, DATEDIFF(d, CAST(t1.abeffdt as datetime), (
    SELECT MAX(CAST(t2.abtermdt as datetime))
    FROM diamond.JMELIGM0_DAT t2
    WHERE t2.abtermdt > '0' and t2.abeligno < t1.abeligno)) AS Interval
FROM diamond.JMELIGM0_DAT t1
WHERE t1.absubno like '199803000000'
ORDER BY t1.abeffdt desc

Where t1.abeligno is the line number and t1.absubno is the member ID
0
 

Author Comment

by:IEHP1
ID: 20291616
imitchie:
Your solution almost acts like a union query it's not comparing the dates (effdate and termdate) between the two lines
0
 

Author Comment

by:IEHP1
ID: 20292477
imitchie:
I used your solution but also joined the subid number and that seems to give me the answer and I needed I also added to the where statement so that I see only the line I needed.   This seems to be the solution I'm looking for.  Thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now