Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Date range in select statement
Message
From
04/12/2001 16:58:33
 
 
To
04/12/2001 16:50:26
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00589506
Message ID:
00589540
Views:
26
This message has been marked as a message which has helped to the initial question of the thread.
>>I have a consumer status table that contains the consumer id, the consumer's status (active, inactive, etc.) and the begin date of the status (the end date is not included - it's assumed that the previous status ended the day the new status began). I would like to determine the date range (or # of days) that a consumer had a certain status. Is there any way to do this with a SQL statement?
>>
>>TIA
>>
>>John
>
>OTTOMH.
>
>You need to join the table to itself and then compare the dates, finding the closest end date to the beginning. A union with the most current status completes the query.
>
Noting what Dan says, I believe, that with the aggregate function in the having, you can calculate the number of days as follows:
>SELECT A.CustID, A.CustStatus, A.BeginDate, B.BeginDate AS EndDate,  ;
>   <b>B.BeginDate - A.BeginDate AS Duration </b>;
>   FROM Status A INNER JOIN Status B ;
>   ON A.CustID = B.CustID ;
>      AND A.BeginDate < B.BeginDate ;
>   GROUP BY A.CustID ;
>   HAVING MIN (B.BeginDate) ;
>UNION ;
>SELECT  Status.CustID, Status.CustStatus, Status.BeginDate, {} AS EndDate, ;
>    <b>DATE - Status.BeginDate AS Duration </b>
>    FROM Status ;
>    GROUP BY Status.CustID ;
>    HAVING MAX (Status.BeginDate)
> Jay
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform