Summary Index is probably your only practical option.
Schedule this search to run every hour (or day or whatever your minimum granularity is):
... | sistats dc(deviceId) by fieldA
When you save it, click "Summary Index" and give it an SI in which to put the data, then run the backfill command to go backwards in time and summarize your data. Then use this to get the data back out:
index=MySummaryIndex| stats dc(deviceId) by fieldA