Table1
CatId Name
1 Category1
2 Category2
3 Category3
Table2
FieldId FieldName FieldValue Category
1 Field1 Value1 1
2 Field2 NULL 1
3 Field3 NULL 2
4 Field4 NULL 2
5 Field5 Value2 3
6 Field6 Value3 3
I have a table structure as shown above. I want to fetch fields under category when any of the Fieldvalue is not null under that category. For my above example result should be:
FieldId FieldName FieldValue Category
1 Field1 Value1 1
2 Field2 NULL 1
5 Field5 Value2 3
6 Field6 Value3 3
As you can see under category1 field2 has null value but it is still displayed. Under category 2 both field3 and field4 has null values and so it should not be selected.
Can someone help me with sql query for the above problem? One way i thought is to use groupby category and see for the count(FieldValue)>0 where fieldvalue is not null. but i am stuck how to bring this logic into sql?
Thanks in advance.
or: