jeu. 14 juin 2012, 12h51
Gregory
atoutfox.public.association
Re: SELECT-SQL : relation n-n
Il faut d’abord etablir le ContactType minimal par client – plusieurs sont possible
Apres quelques join
Ceci donne les clients sans contact – pour ne pas les avoir : remplacer le premier ‘left join’ par join
Dans cet exemple
- Olivier a deux contact qui sont minimum
- Jean en a un
- Thierry n’en a pas
create cursor Client ;
( id I, ;
name c(32) ;
)
insert into Client values(1, 'Olivier')
insert into Client values(2, 'Jean')
insert into Client values(3, 'Thierry')
create cursor Contact ;
( id I, ;
name c(32) ;
)
insert into Contact values(1, 'Michel')
insert into Contact values(2, 'Francis')
insert into Contact values(3, 'Gerard')
create cursor cli cont ;
( client id I, ;
contact id I, ;
ContactType I ;
)
for i = 1 to 1
for j = 1 to 2
for k = 100 to 10 step -10
insert into cli cont values(m.i, m.j, m.k)
endfor
endfor
endfor
for i = 2 to 2
for j = 2 to 2
for k = 100 to 10 step -10
insert into cli cont values(m.i, m.j, m.k)
endfor
endfor
endfor
for i = 1 to 2
for j = 3 to 3
for k = 200 to 100 step -10
insert into cli cont values(m.i, m.j, m.k)
endfor
endfor
endfor
select Client.Id, ;
Client.Name, ;
Contact.Id as contact, ;
Contact.Name as ContactName, ;
cli cont.ContactType ;
from Client ;
left join ( ;
select client id, ;
min(ContactType) as ContactType ;
from cli cont ;
group by 1 ;
) X ;
on ( Client.Id = X.client id );
left join cli cont ;
on ( cli cont.client id == X.client id ) ;
and ( cli cont.ContactType == X.ContactType ) ;
left join Contact on Contact.Id = cli cont.Contact id
Gregory
"FoxInCloud" wrote in message news:jrcbhh$2rsm$1@news.niouzes.net...
Bonjour
soit les tables
client (ID I)
contact (ID I)
cli cont (client id I, contact id I, ContactType I)
cli cont établit une relation n-n entre client et contact :
chaque client peut avoir plusieurs contacts (au moins 1) dont le ContactType peut évoluer de 1 à 100
comment trouver avec un SELECT-SQL
pour chaque client,
le contact dont le ContactType est minimum?
J'ai essayé sans succès
SELECT;
awCustomer.Company;
, awCustomer.CustomerID;
, awContact.Civil;
, awContact.First;
, awContact.Last;
FROM;
awSales!awCustomer;
LEFT JOIN awSales!awCus Cont ON awCus Cont.CustomerID = awCustomer.CustomerID;
JOIN (SELECT;
Min(awCus Cont.ContactType) AS ContactType;
FROM awSales!awCus Cont;
WHERE awCus Cont.CustomerID = awCustomer.CustomerID;
) LicenceContactType ON LicenceContactType.ContactType = awCus Cont.ContactType;
JOIN awSales!awContact ON awContact.ContactID = awCus Cont.ContactID;
ORDER BY;
Company;
INTO CURSOR awCustomerContact
merci
Journal

FoxInCloud (Th. Nivelet) pense que ce message est la bonne réponse ou qu'il est utile
Permalink : http://www.atoutfox.org/nntp.asp?ID=0000009177