Question: count all and count in specific date range
See original GitHub issueHello, sorry for the noise.I’ve not been able to add a new metric for a specific date range. I need to count two things. The first one is count by month and I could do it. But the second one is to count in the last five months.
The code that is workings is:
now = timezone.now()
five_month_ago = now - relativedelta(months=5)
query_banks = Q("terms", bank__id=ids_of_banks)
aggs_by_month = A('date_histogram', field='created_at', interval='month', format='YYYY-MM')
search = search.query("nested", path="bank", query=[1, 2, 3])
search.aggs.metric('count', 'cardinality', field='id')
search.aggs.bucket('by_month', aggs_by_month)
result = search.execute()
I tried a lot of things, for counting the last five month without success.
aggs_five_month = A('filter', Q('range', created_at={'gt': five_month_ago, 'lt': now}))
search.aggs.bucket('last_five_months', aggs_five_month).metric('count', 'cardinality', field='id')
Is it possible to do both things with one query?
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
SQL Query to Count Items Within Date Range? - Stack Overflow
This assumes that all dates are valid. If some dates are special, you need to take that into account in the order by...
Read more >SQL count(*) grouped by date range - DBA Stack Exchange
You can use the APPLY operator to count the number of employees active at the termination date. As I looked at the question...
Read more >Count cells between dates - Excel formula - Got It AI
Excel allows us to count cells between dates by using the COUNTIFS function. The COUNTIFS function counts the number of cells that meet...
Read more >Excel COUNTIF function examples - not blank, greater than ...
Count dates greater than or equal to a date in another cell, minus x days. =COUNTIF(B2:B10,">="&B2-"7"), Count the number of cells in the...
Read more >Count the number of times a month appears within in a range!
Do you know how to count the number of times a specific month appears in a range of dates ? This scenario is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This should do the trick:
It will give you a count per month and also for the last 5 months.
Note that you also have some weird constructs with the
nested
query. Ifbank
is anested
field then you need to query for the bank ids as:if not then just:
Hope this helps!
👍 😃 Thanks!