I'm trying to get a maximum value of a field in a vector layer without using the filter that is active in QGIS. So far I've tried:

layer = iface.activeLayer()
idx = layer.fields().indexFromName('fid')
print layer.maximumValue(idx)

This sends the following SQL query to my SQL Server database (I have filtered the layer by fid in QGIS):

select max([fid]) from [mySchema].[myLayer] where ("fid" < 1000)

Is it possible to send

select max([fid]) from [mySchema].[myLayer]

instead? I'd still like to have the filter on, so that not all features are shown on the map.

1 Answer 1


You may temporary disable your filter to get the wanted info and restore it just after like below sample

layer = iface.activeLayer()
idx = layer.fields().indexFromName('fid')

# Save your layer filter to a variable
save_substr = layer.subsetString()

# Remove your filter to be sure to do calculation on all records

# Make your calculation without the filter

# Restore your filter
  • This worked for me when the layer was not currently in edit mode. However, if the layer is being edited, the maximum value is still calculated from the filtered features.
    – naaamk
    Feb 18, 2021 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.