# Using the Expression String Builder to ultilise specific data within an attribute table

I have an issue relating to the 'Expression String Builder'. I have a series of data over a 8 year period. I need to collect that data for only a 5 year period. Once collected I need to find the fishing intensity associated with pipelines. From figure 1 I need Dem11 to Dem15 (The figure shows a few pieces of data).

However I don't need Dem07 to Dem10. With this data I open up properties and create `rule-based`symbology. The increments I require are

• less than 0
• 0 - 5
• 5 - 20
• 20 - 50
• 50 - 200
• 200 - 500
• greater than 500

Being new to QGIS, I thought the logical solution would be seen in figure 2.

This seems to work, but not very accurately.

Additionally, I need to do the same with 2 data sets e.g Dem11 to Dem15 & Nep11 to Nep15 together using the same increments. It doesn't seem to give accurate numbers whatsoever. e.g following the formula seen below Dem 11 to 15 gives me (26103 data points between 0 - 5). When I do the exact same with Nep11 to 15. I get (36051 between 0 - 5). However when I use the same formula as seen in figure 3 to add both together to find the fishing intensity I get (23448) which I not sure how is possible as this is 2655 less than the Dem11 to Dem15 data alone. Can anyone help me with my issue?

• It seems logical. If the sum of the dem is 4 and the sum of the nep is also 4, then adding the two will be more than 5 and so will be excluded by the filter.
– JGH
Jan 22, 2019 at 15:16
• maybe you are looking for 4 conditions: sum of dem > 0 and < 5, and also sum of nep > 0 and < 5
– JGH
Jan 22, 2019 at 15:17
• So you think I might need to do... "Nep 11" + "Nep12" + "Nep13" + "Nep14" + "Nep15" >=0 AND <=5 + "Dem 11" + "Dem12" + "Dem13" + "Dem14" + "Dem" >=0 AND <=5 ? Jan 22, 2019 at 15:23
• May be with parentheses ? For the first with only Dem it will be something like : ("Dem11" + "Dem12" +...)>=0 AND ("Dem11" + "Dem12" +...) <=0 For the second, it seems you have to use the OR condition Jan 22, 2019 at 15:23
• Like as seen here? is this what you mean by using the 'Or' condition?("Nep 11" + "Nep12" + "Nep13" + "Nep14" + "Nep15") + ("Dem 11" + "Dem12" + "Dem13" + "Dem14" + "Dem" >=0 AND <=5 Jan 22, 2019 at 15:24

I'm reading the comments and I think you're a little confused with the conditions. I recommend using parentheses so you can better interpret how they work.

First case (only Dem):

``````( ( "Dem11" + "Dem12" + "Dem13" + "Dem14" + "Dem15") >= 0)
AND
( ( "Dem11" + "Dem12" + "Dem13" + "Dem14" + "Dem15") <= 5)
``````

Second case (Dem OR Nep):

``````(
( ( "Dem11" + "Dem12" + "Dem13" + "Dem14" + "Dem15") >= 0)
AND
( ( "Dem11" + "Dem12" + "Dem13" + "Dem14" + "Dem15") <= 5)
)
OR
(
( ( "Nep11" + "Nep12" + "Nep13" + "Nep14" + "Nep15") >= 0)
AND
( ( "Nep11" + "Nep12" + "Nep13" + "Nep14" + "Nep15") <= 5)
)
``````

While you can add up the field values in each expression you write, the expressions will be much easier to write and understand if you store the sums in separate fields.

Use the Field Calculator to create new fields as follows:

• Create a field called "Dem11to15" with this expression

``````"Dem11" + "Dem12" + "Dem13" + "Dem14" + "Dem15"
``````
• Create a field called "Nep11to15" with this expression

``````"Nep11" + "Nep12" + "Nep13" + "Nep14" + "Nep15"
``````

Note: if you don't want to edit the source data, simply check the box for "virtual fields" when creating the new field. This creates a field which is saved as part of the QGIS project, not as part of the layer file.

With the new fields, your expressions become much simpler. Simpler expressions means you're less likely to make typos, and typos will be easier to fix.

The rules for Dem values would be as follows:

``````"Dem11to15" > 0 AND "Dem11to15" < 5
"Dem11to15" >= 5 AND "Dem11to15" < 20
"Dem11to15" >= 20 AND "Dem11to15" < 50
"Dem11to15" >= 50 AND "Dem11to15" < 200
"Dem11to15" >= 200 AND "Dem11to15" < 500
"Dem11to15" >= 500
``````

The rules for Dem and Nep values would be as follows:

``````("Dem11to15" > 0 AND "Dem11to15" < 5) OR ("Nep11to15" > 0 AND "Nep11to15" < 5)
("Dem11to15" >= 5 AND "Dem11to15" < 20) OR ("Nep11to15" >= 5 AND "Nep11to15" < 20)
("Dem11to15" >= 20 AND "Dem11to15" < 50) OR ("Nep11to15" >= 20 AND "Nep11to15" < 50)
("Dem11to15" >= 50 AND "Dem11to15" < 200) OR ("Nep11to15" >= 50 AND "Nep11to15" < 200)
("Dem11to15" >= 200 AND "Dem11to15" < 500) OR ("Nep11to15" >= 200 AND "Nep11to15" < 500)
("Dem11to15" >= 500) OR ("Nep11to15" >=500)
``````
• Or, with the new fields, you could use a Graduated style instead of rule-based style.
– csk
Jan 22, 2019 at 20:13