Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is there a way to do this with Grouping?
Message
From
27/10/2004 15:08:42
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
00954810
Message ID:
00954987
Views:
17
This message has been marked as the solution to the initial question of the thread.
>Thanks Fabio,
>It was a real plesure to look this code. I never think of that. But what if the some dates of table 2 is equal. When I try the code & make two dates equal always some of the table2 records is missing & always that has equal date with other record.

Hi Borislav,

this is correct because my SELECTs are correct only if
id,PayedDate it is a Unique Key for Table2
Without a Table2 Primary Key,
it is impossible to distinguish these two rows
* Table2
7,$60,^2004/10/29 && 7's first row
7,$60,^2004/10/29 && 7's second row 
You can use RECNO() like primary key,
but then you cannot do the task with a single SELECT
( on VFP9 perhaps it will be possible).

Another point:
if you move the data into other databases,
you have to add a primary key, sure.

Then, add a primary key: idTable2 I AUTOINCR can do the task.
create cursor Table1 (	Id	I,ContrSum	Y,ContrDate	D)

insert into Table1 values(1, $100, {^2004/10/10})
insert into Table1 values(2, $200, {^2004/10/11})
insert into Table1 values(3, $300, {^2004/10/13})
insert into Table1 values(4, $300, {^2004/10/14})
	
Create cursor Table2 (	idTable2 I AUTOINCR ,Id	I, PayedSum	Y, PayedDate	D	)

insert into Table2 (id,PayedSum,PayedDate) Values(1, $50,  {^2004/10/10})
insert into Table2 (id,PayedSum,PayedDate) Values(1, $20,  {^2004/10/10})
insert into Table2 (id,PayedSum,PayedDate) Values(1, $10,  {^2004/10/11})
insert into Table2 (id,PayedSum,PayedDate) Values(1, $20,  {^2004/10/12})
insert into Table2 (id,PayedSum,PayedDate) Values(2, $100, {^2004/10/11})
insert into Table2 (id,PayedSum,PayedDate) Values(2, $100, {^2004/10/13})
insert into Table2 (id,PayedSum,PayedDate) Values(3, $200, {^2004/10/14})
	
SELECT T1.id	;
,	IIF(MIN(T2.idTable2)>MIN(T3.idTable2),NULL,MIN(T1.ContrSum))	ContrSum;
,	IIF(MIN(T2.idTable2)>MIN(T3.idTable2),NULL,MIN(T1.ContrDate))	ContrDate;
,	MIN(T2.PayedSum)						PayedSum;
,	MIN(T2.PayedDate)	PayedDate;
INTO CURSOR OK_Only_If_Input_id_And_PayedDate_It_Is_Unique;
FROM Table1 T1	LEFT JOIN Table2 T2 ON T2.id=T1.id;
		LEFT JOIN Table2 T3 ON T3.id=T1.id;
GROUP BY T1.id,T2.idTable2
BROWSE LAST

* ATTENTION: previous it is ok only if,
* : order of idTable2 and order by PayedDate put Table2 into the same order!

* add a out of order payment
insert into Table2 (id,PayedSum,PayedDate) Values(3, $10, {^2004/10/13})
	
SELECT T1.id	;
,	IIF(T2.idTable2>MIN(T3.idTable2),NULL,MIN(T1.ContrSum))		ContrSum;
,	IIF(T2.idTable2>MIN(T3.idTable2),NULL,MIN(T1.ContrDate))	ContrDate;
,	MIN(T2.PayedSum)						PayedSum;
,	MIN(T2.PayedDate)	PayedDate;
INTO CURSOR FAIL_Only_If_Input_PayedDate_It_Is_Ordered;
FROM Table1 T1	LEFT JOIN Table2 T2 ON T2.id=T1.id;
		LEFT JOIN Table2 T3 ON T3.id=T1.id;
GROUP BY T1.id,T2.idTable2
BROWSE LAST
* PayedDate it is not in order !

* you cannot resolve this , because if you reorder the table, you reoder the table1 fields

SELECT MIN(T1.id)	;
,	IIF(T2.idTable2>MIN(T3.idTable2),NULL,MIN(T1.ContrSum))	ContrSum;
,	IIF(T2.idTable2>MIN(T3.idTable2),NULL,MIN(T1.ContrDate))	ContrDate;
,	MIN(T2.PayedSum)						PayedSum;
,	MIN(T2.PayedDate)	PayedDate;
INTO CURSOR FAIL_Reorder_PayedDate_Reorder_NULLS;
FROM Table1 T1	LEFT JOIN Table2 T2 ON T2.id=T1.id;
		LEFT JOIN Table2 T3 ON T3.id=T1.id;
GROUP BY T2.idTable2;
ORDER BY 1,5
BROWSE LAST
* Solution: build a PayedDate ordered Unique key on the fly 
* ( Attention: VFP untyped Nulls fire a error at SELECT build plan time )

SELECT T1.id	;
,IIF(DTOS(T2.PayedDate)+BINTOC(T2.idTable2)>MIN(DTOS(T3.PayedDate)+BINTOC(T3.idTable2));
	,$0,MIN(T1.ContrSum))	ContrSum;
,IIF(DTOS(T2.PayedDate)+BINTOC(T2.idTable2)>MIN(DTOS(T3.PayedDate)+BINTOC(T3.idTable2));
	,{},MIN(T1.ContrDate))	ContrDate;
,	MIN(T2.PayedSum)				PayedSum;
,	T2.PayedDate	;
INTO CURSOR OK_Build_A_Ordered_PayedDate_Unique_Value_with_Empty;
FROM Table1 T1	LEFT JOIN Table2 T2 ON T2.id=T1.id;
		LEFT JOIN Table2 T3 ON T3.id=T1.id;
GROUP BY T1.id,T2.PayedDate,T2.idTable2
BROWSE LAST

* if you want NULL ( the correct choice ) you can use a typed variable
yNull=$0
dNull={}
STORE NULL TO yNull,dNull
SELECT T1.id	;
,IIF(DTOS(T2.PayedDate)+BINTOC(T2.idTable2)>MIN(DTOS(T3.PayedDate)+BINTOC(T3.idTable2));
	,m.yNull,MIN(T1.ContrSum))	ContrSum;
,IIF(DTOS(T2.PayedDate)+BINTOC(T2.idTable2)>MIN(DTOS(T3.PayedDate)+BINTOC(T3.idTable2));
	,m.dNull,MIN(T1.ContrDate))	ContrDate;
,	MIN(T2.PayedSum)		PayedSum;
,	T2.PayedDate	;
INTO CURSOR OK_Build_A_Ordered_PayedDate_Unique_Value_with_Null;
FROM Table1 T1	LEFT JOIN Table2 T2 ON T2.id=T1.id;
		LEFT JOIN Table2 T3 ON T3.id=T1.id;
GROUP BY T1.id,T2.PayedDate,T2.idTable2
BROWSE LAST
Fabio
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform