I need to write a SQL query for a report. A user can select six filters which are dynamic i.e. User can select one or two up to max six filters.
In each filter the user selects an attribute of a device from a dropdown, an operator and its value user needs to input. This constitutes a single filter, e.g:
AttributeName Operator Value
Cost equal 480
AND MappedName contains DummyString
Here “Cost” is attribute and “480” is the value and this whole thing consist of one filter . In Similar fashion user can select max. 6 filters
Also the operator between two filters are again dynamic . it can be “AND” or “OR”
In the report I need to dynamically generate a column for each filter along with some static columns, e.g. Device Name, Device Manufacture are static columns whereas the above filter are dynamic.
So the report when the user has selected two filters will look something like:
DeviceName DeviceManufactur Cost MappedName
D1 DM1 480 DummyString
D2 DM2 480 DummyString
In the database the attributes are stored something like:
DeviceName DeviceManufactur AttributeName AttributeValue
D1 DM1 Cost 480
D1 DM1 MappedName DummyString
D2 DM2 Cost 480
D2 DM2 MappedName DummyString
So when I write simple sql like
select d.name,d.manufacture,d.AttName,d.value
from Device d
where d.AttName='Cost' and d.AttValie='480'
and d.AttName='MappedName' and d.AttValue='DummyString'
it will never give me any records although the device is associated with both the attributes.
Can anyone suggest any specific function of SQL which will help me in writing this logic efficiently.
I don’t think I’m really understanding your question right, but the only thing I can think of is:
This should return all information for any device which matches the query
Cost = '480' AND MappedName = 'DummyString'…