Nested SQL Query (Query a subset of data) Assume I have the following structure...

Structures
- ID
- Name

Fields
- ID
- StructureID
- Key


These table hold simplistic 'structures' (Think very simplistic tables).

There is then an additional table

DataValues
- ID
- FieldID 
- Value
- RowID


Example data inside of the table...

ID | FieldID | Value             | RowID
1  | 5       | Blog Post Title   | 1
2  | 6       | Blog Post Body    | 1
3  | 5       | Another Post H1   | 2
4  | 6       | Another Body      | 2


Now I want the ability to do dynamic queries to each data set, so grab all the RowID's that follow a certain pattern.

SELECT DV.RowID FROM DataValues as DV 
    INNER JOIN Fields as F ON DV.FieldID = F.ID 
    INNER JOIN DataRows as DR ON DV.RowID = DR.ID 
WHERE DR.StructureID = 2 
AND (F.[Key] = 'Title' AND DV.[Value] = 'Blog Post Title')


The issue is then when we want to do multiple conditions

SELECT DV.RowID FROM DataValues as DV 
    INNER JOIN Fields as F ON DV.FieldID = F.ID 
    INNER JOIN DataRows as DR ON DV.RowID = DR.ID 
WHERE DR.StructureID = 2 
AND (F.[Key] = 'Title' AND DV.[Value] = 'Blog Post Title') 
AND (F.[Key] = 'Body' AND DV.[Value] &lt;&gt; 'A Body We Dont Want') 


This obviously wont work, is there a way to query a subset of the data?
If I understand what you mean correctly (and I'm not sure I do), you should return 2 rows per RowID for a 'matching' result.

Therefore you can do an OR on the key data you are searching for, GROUP BY RowID and count that there are matching results for each subset.

SELECT 
  DV.RowID,
  MIN(CASE 
        WHEN F.[Key] = 'Title' THEN 1 
        WHEN F.[Key] = 'Body' THEN 2
      END
    ) AS Ordinal 
FROM DataValues as DV,
INNER JOIN Fields as F ON DV.FieldID = F.ID 
INNER JOIN DataRows as DR ON DV.RowID = DR.ID 
WHERE DR.StructureID = 2 
AND (
     (F.[Key] = 'Title' AND DV.[Value] = 'Blog Post Title') 
OR   (F.[Key] = 'Body' AND DV.[Value] &lt;&gt; 'A Body We Dont Want') 
    )
GROUP BY DV.RowID
HAVING COUNT(*) = 2
ORDER BY Ordinal ASC;


Alternatively, you could do an INTERSECT of the two result sets for all of the RowID values that satisfy both conditions:

SELECT DV.RowID, 1 as Ordinal 
FROM DataValues as DV 
INNER JOIN Fields as F ON DV.FieldID = F.ID 
INNER JOIN DataRows as DR ON DV.RowID = DR.ID 
WHERE DR.StructureID = 2 
  AND (F.[Key] = 'Title' AND DV.[Value] = 'Blog Post Title') 
INTERSECT
SELECT DV.RowID, 2 AS Ordinal
FROM DataValues as DV 
INNER JOIN Fields as F ON DV.FieldID = F.ID 
INNER JOIN DataRows as DR ON DV.RowID = DR.ID 
WHERE DR.StructureID = 2 
  AND (F.[Key] = 'Body' AND DV.[Value] &lt;&gt; 'A Body We Dont Want')
ORDER BY Ordinal ASC;