Skip to content
Advertisement

SQL Query to find a group of rows having all of them a certain value in one of its fields

Given table:

I need an SQL query that would list ALL and ONLY attr’s that have ALL their fker=NULL and all of their did=1

In this example,

  • only attr4 satisifies this condition, because
  • attr1 has no fker=NULL,
  • attr2 doesn’t have all of its did=1 (none in fact),
  • attr3 doesn’t have all of its fekr=NULL (even if it had, it has did=2).

In other words, the query’s result set should only have ‘attr4’ in its result set.

Using the following example on http://sqlfiddle.com:

I tried this SQL (with the help of a similar question here — similar but not identical):

But I am unable to come up with TOTAL count of all rows of an attr, without giving up the group by two fields, not one.

How do I accomplish that?

Advertisement

Answer

You can use group by and having:

You could also use:

or if you prefer:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement