Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
One to many relationship INNER JOIN condition
Message
 
 
To
14/02/2011 11:34:16
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2008
Application:
Web
Miscellaneous
Thread ID:
01500073
Message ID:
01500115
Views:
53
This message has been marked as the solution to the initial question of the thread.
>>What exactly is the requirement here? Can you post some input and a bit of output?
>>
>>Do you want to only get Master.Numero if all Child records for that Numero (through ManyToManyTable) are Enabled?
>
>My example was using an inner SQL. The reason is because I need to return several fields from the master table. So, this is why for now the inner SQL, as shown in the example, would have been the same if it would have been as is. But, I show the example with the inner SQL so to allow me to expand the example, if needed, and show more master fields I need to return.
>
>But, for the main point of the thread, this is not a factor.
>
>The goal is to make sure that if I have three records in the many to many table and one of them does not satisfy the condition, then the master record should not be returned.

Try this (suggested by Peter Larsson):
SELECT                   m.Numero
FROM                     dbo.ChildTable AS c
LEFT JOIN   dbo.ManyToManyTable AS mm ON mm.NoChildTable = c.Numero
LEFT JOIN   dbo.[Master] AS m ON m.Numero = mm.NoMaster
GROUP BY    m.Numero
HAVING                   COUNT(c.Numero) <= COUNT(m.Numero)
                         AND MIN(CASE WHEN c.[Enabled] = 1 THEN 1 ELSE 0 END) = 1
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform