Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Query to SQL Server on consecutive dates
Message
De
23/11/2004 02:34:04
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00963700
Message ID:
00963732
Vues:
7
>Hi,
>
>I have to create a SQL Passthrough in which the WHERE clause filters if the days passed between a date in one record to the date in the previous record are greater than let say 10 days, within a specific grouping of records.

IF your rows have a consecutively numbered key column you might be able to use something like the following, which works in Fox but requires 3 SELECTs.

Your backend (SQL Server, Oracle, ??) may support more powerful SQL which may let you combine some of the SELECT steps. OTOH you'll probably have to do some sort of arithmetic and/or CAST()/CONVERT() with datetimes since backends typically don't support pure date types.
* Build and populate cursor for test:
CREATE CURSOR PrevTest ;
	(iID I, ;
	dDate D)

INSERT INTO PrevTest (iID, dDate) VALUES (1, {^2004-11-22})
INSERT INTO PrevTest (iID, dDate) VALUES (2, {^2004-11-23})
INSERT INTO PrevTest (iID, dDate) VALUES (3, {^2004-12-04})
INSERT INTO PrevTest (iID, dDate) VALUES (4, {^2004-12-06})
INSERT INTO PrevTest (iID, dDate) VALUES (5, {^2004-12-06})
INSERT INTO PrevTest (iID, dDate) VALUES (6, {^2004-12-18})
INSERT INTO PrevTest (iID, dDate) VALUES (7, {^2004-12-19})
INSERT INTO PrevTest (iID, dDate) VALUES (8, {^2004-12-31})
INSERT INTO PrevTest (iID, dDate) VALUES (9, {^2005-01-01})
INSERT INTO PrevTest (iID, dDate) VALUES (10, {^2005-01-21})

* Get 1st temporary cursor with "previous" row ID in each row:
SELECT ;
	*, ;
	iID - 1 AS iPrevID ;
	FROM PrevTest ;
	INTO CURSOR Temp1 ;
	NOFILTER

* Look up previous date for each row (if present),
* if not present use NVL() and a dummy date far in the future
* so the date difference will always be negative:
SELECT ;
	A.*, ;
	NVL(B.dDate, {^2005-12-31}) AS dPrevDate ;
	FROM Temp1 A ;
		LEFT OUTER JOIN PrevTest B ;
		ON B.iID = A.iPrevID ;
	INTO CURSOR Temp2 ;
	NOFILTER

* Now just get the rows that meet the date difference criterion:
SELECT ;
	* ;
	FROM Temp2 ;
	WHERE dDate - dPrevDate >= 10 ;
	INTO CURSOR Result
If you examine the cursors you'll see how it works.
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform