Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
One to many relationship INNER JOIN condition
Message
 
 
To
14/02/2011 09:12:09
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2008
Application:
Web
Miscellaneous
Thread ID:
01500073
Message ID:
01500079
Views:
53
>I have a situation that I have been trying to resolve but was unable to up until now. I have a one to many relationships in a SQL where I need to use a condition on the INNER JOIN table. So, for example, the INNER JOIN table might have three records related to one record from the master table in the SQL. But, I need to assure to take only master records where all the INNER JOIN records for that table would satisfy a condition. What could be an example of such a query?

Try
SELECT *
  FROM master m
  WHERE EXISTS ( SELECT 1 FROM slave s 
             WHERE s.fk = m.pk HAVING COUNT(*) = SUM( CASE WHEN <condition> THEN 1 ELSE 0 END ))
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform