ATOUTFOX
COMMUNAUTÉ FRANCOPHONE DES PROFESSIONNELS FOXPRO
Visual FoxPro : le développement durable

Forum AtoutFox : SELECT ... Count(distinct ...), Count(distinct ...) ... : DISTINCT is invalid.   

Sujet

rss Flux RSS des derniers messages

Vous devez vous identifier pour pouvoir poser une question ou répondre.

mer. 31 juillet 2013, 13h00

FoxInCloud (Th. Nivelet)
France France

atoutfox.public.association

SELECT ... Count(distinct ...), Count(distinct ...) ... : DISTINCT is invalid.

Bonjour,

Hier j'ai buté sur un problème de SQL et Google n'a rien trouvé de bien convaincant ...

Trouver le nombre d'enregistrement distinct de 2 tables enfants par JOIN (code de test et résultat plus bas)

Une idée?
Merci,

&& librairies pour mettre en forme le résultat à partir d'un tableau
&& remplacer pour reproduire ...
#include ab.h
AB()

CreateData() && ci-dessous

local ;
  aResult[1]; && résultat d'un test
, cResult && résultats cumulés

cResult = ''
on error = cResultAdd(@m.cResult, Message())

_Tally = 0
select ;
    parent.pk;
  , Count(child1.data) as cnt_child1;
  from parent;
    join child1 on parent.Pk = child1.fk;
    join child2 on parent.Pk = child2.fk;
  group by 1;
  where parent.Pk = 1;
  into array aResult
=_Tally > 0 and cResultAdd(@m.cResult, cResult('pk, child1:', @m.aResult)) && cResult() ci-dessous

_Tally = 0
select ;
    parent.pk;
  , Count(child2.data) as cnt_child2;
  from parent;
    join child1 on parent.Pk = child1.fk;
    join child2 on parent.Pk = child2.fk;
  group by 1;
  where parent.Pk = 1;
  into array aResult
=_Tally > 0 and cResultAdd(@m.cResult, cResult('pk, child2:', @m.aResult))


_Tally = 0
select ;
    parent.pk;
  , Count(child1.data) as cnt_child1;
  , Count(child2.data) as cnt_child2;
  from parent;
    join child1 on parent.Pk = child1.fk;
    join child2 on parent.Pk = child2.fk;
  where parent.Pk = 1;
  group by 1;
  into array aResult
=_Tally > 0 and cResultAdd(@m.cResult, cResult('pk, child1, child2:', @m.aResult))

_Tally = 0
select ;
    parent.pk;
  , Count(distinct child1.data) as cnt_child1;
  from parent;
    join child1 on parent.Pk = child1.fk;
    join child2 on parent.Pk = child2.fk;
  group by 1;
  where parent.Pk = 1;
  into array aResult
=_Tally > 0 and cResultAdd(@m.cResult, cResult('pk, child1 distinct:', @m.aResult))

_Tally = 0
select ;
    parent.pk;
  , Count(distinct child2.data) as cnt_child2;
  from parent;
    join child1 on parent.Pk = child1.fk;
    join child2 on parent.Pk = child2.fk;
  group by 1;
  where parent.Pk = 1;
  into array aResult
=_Tally > 0 and cResultAdd(@m.cResult, cResult('pk, child2 distinct:', @m.aResult))


_Tally = 0
select ;
    parent.pk;
  , Count(distinct child1.data) as cnt_child1;
  , Count(distinct child2.data) as cnt_child2;
  from parent;
    join child1 on parent.Pk = child1.fk;
    join child2 on parent.Pk = child2.fk;
  where parent.Pk = 1;
  group by 1;
  into array aResult
=_Tally > 0 and cResultAdd(@m.cResult, cResult('pk, child1 distinct, child2 distinct:', @m.aResult))


_cliptext = m.cResult + CRLF && collé dans le #if ... #endif ci-dessous
on error

return m.cResult

#if .F. && collé de _cliptext ci-dessus
pk, child1:
1,12

pk, child2:
1,12

pk, child1,  child2:
1,12,12

pk, child1 distinct:
1,3

pk, child2 distinct:
1,4

&& pk, child1 distinct, child2 distinct
!() - SQL: DISTINCT is invalid. && BINGO

#endif

* ---------------------------------------------------------
procedure CreateData

create cursor parent (pk I)
insert into parent values (1)
insert into parent values (2)

create cursor child1 (fk I, data I)
insert into child1 values (1, 1)
insert into child1 values (1, 2)
insert into child1 values (1, 3)

insert into child1 values (2, 1)
insert into child1 values (2, 2)
insert into child1 values (2, 3)
insert into child1 values (2, 4)

create cursor child2 (fk I, data I)
insert into child2 values (1, 1)
insert into child2 values (1, 2)
insert into child2 values (1, 3)
insert into child2 values (1, 4)

insert into child2 values (2, 1)
insert into child2 values (2, 2)
insert into child2 values (2, 3)
insert into child2 values (2, 4)

* ---------------------------------------------------------
function cResult(cResult, aResult) && résultat d'un test
return m.cResult + CRLF + cListOfArray(@m.aResult,,-1,,,,,,.T.)


Permalink : http://www.atoutfox.org/nntp.asp?ID=0000012684
20 286 messages dans le forum • Liste complète des messages

www.atoutfox.org - Site de la Communauté Francophone des Professionnels FoxPro - v3.4.0 - © 2004-2026.
Cette page est générée par un composant COM+ développé en Visual FoxPro 9.0-SP2-HF3