Advertisement

05.12.2008 at 11:00AM PDT, ID: 23395389
[x]
Attachment Details

t-sql compare date between first record and next record

Asked by cmleung2 in MS SQL Server

Tags: t-sql

I have two tables like the following, using MS SQL Server 2000:
Table A:
ID   Date
---  ------
A1  1999-01-08
A2  1999-03-02
===================
Table B:
ID   Date                Amt
---  ----------          ------
A1  1999-01-11    100
A1  1999-02-17     49
A1  1999-02-18     50
A2  1999-03-01     200
A2  1999-03-10     122
====================
Now I have do join the two so that I have 1 table that looks like this with and additional column, so that if the Date in table B is between the Date in Table A first record and second record, I will assign the same sequence number, and the last date will be between the Date in Table A last record and current system date:
ID       Addl     Date             Amt
----    -------   --------------   ------
A1    1           1999-01-11   100
A1    1           1999-02-17     49
A1    1            1999-02-18     50
A2    2            1999-03-01     200
A2    2            1999-03-10     122
============================

How do I do that?  Is it possible to do it in one query?
ThanksStart Free Trial
[+][-]05.12.2008 at 11:07AM PDT, ID: 21549207

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.12.2008 at 12:10PM PDT, ID: 21549678

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.12.2008 at 12:13PM PDT, ID: 21549693

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.12.2008 at 12:29PM PDT, ID: 21549820

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.13.2008 at 11:31AM PDT, ID: 21557870

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.13.2008 at 11:38AM PDT, ID: 21557942

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.13.2008 at 12:30PM PDT, ID: 21558484

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Tags: t-sql
Sign Up Now!
Solution Provided By: ScottPletcher
Participating Experts: 2
Solution Grade: A
 
 
[+][-]05.15.2008 at 07:15AM PDT, ID: 21573838

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_2_20070628