Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
One to many relationship INNER JOIN condition
Message
 
 
À
14/02/2011 09:47:38
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2008
Application:
Web
Divers
Thread ID:
01500073
Message ID:
01500095
Vues:
43
>>Could you post some example data and desired result?
>
>Here is an example, where I added Sergey's logic in it:
>
>
>SELECT Master.Numero
>FROM (SELECT Master.Numero FROM Master 
>WHERE EXISTS (SELECT 1 FROM ManyToManyTable
> INNER JOIN ChildTable ON ManyToManyTable.NoChildTable=ChildTable.Numero 
> WHERE Master.Numero=ManyToManyTable.NoMaster
> HAVING COUNT(*)=SUM(CASE WHEN ChildTable.Enabled=1 THEN 1 ELSE 0 END))
>GROUP BY Master.Numero) Temp 
>INNER JOIN Master ON Temp.Numero=Master.Numero 
>
If you want to make sure that all records are enabled, then
select M.Numero
from Master M inner join ManyToManyTable MM on M.Numero = MM.NoMaster
inner join ChildTable C on MM.NoChildTable = C.Numero
group by M.Numero
having min(case when C.Enabled = 1 then 1 else 0 end) = 1 -- means all records are enabled
and sum(case when C.Enabled = 1 then 1 else 0 end) = count(C.Numero) -- total number of enabled records = total number of joined records
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform