0

HI Guys I have a datas in the model like this

Date        Day         Amount
------------------------------
01/09/2016  Thursday    2500
02/09/2016  Friday      300
03/09/2016  Saturday    600
04/09/2016  Sunday      7500
05/09/2016  Monday      9800
06/09/2016  Tuesday     2800
07/09/2016  Wednesday   3600
08/09/2016  Thursday    580
09/09/2016  Friday      352
10/09/2016  Saturday    950
11/09/2016  Sunday      780
12/09/2016  Monday      650
13/09/2016  Tuesday     440
14/09/2016  Wednesday   25
15/09/2016  Thursday    39
16/09/2016  Friday      500
17/09/2016  Saturday    51
18/09/2016  Sunday      65
19/09/2016  Monday      99
20/09/2016  Tuesday     350
21/09/2016  Wednesday   280
22/09/2016  Thursday    782
23/09/2016  Friday      98
24/09/2016  Saturday    785
25/09/2016  Sunday      965
26/09/2016  Monday      1500
27/09/2016  Tuesday     3650
28/09/2016  Wednesday   85
29/09/2016  Thursday    70
30/09/2016  Friday      980

I want to write a filter query in django which filters the data like Sum of Friday to Thursday consecutively following next, of every month.

i.e sum(02/09/2016(Friday) To 08/09/2016(Thursday)) , sum(09/09/2016(Friday) To 15/09/2016(Thursday)) and so on....

Ajay Kumar
  • 1,595
  • 3
  • 20
  • 36
  • This might help. http://stackoverflow.com/questions/6266397/django-aggregation-to-lower-resolution-using-grouping-by-a-date-range – Anuj Sep 28 '16 at 02:11

1 Answers1

0

You can use django's raw query method to fire below query for your mentioned data

 [Model_Name.objects.raw('query_below_mentioned')][1]

. I'm not aware about the DB you are using but you can use below mentioned query for fetching data . I tried it is sqlite and it's working fine, but the only problem here is if your data keeps increasing then you have to manually adjust offset and modify the query. :( ,but if it's urgent you can use it as of now:

select * from (select sum(amount) as sum from (SELECT * FROM demo_table LIMIT 7 OFFSET 0)
                UNION
                select sum(amount) as sum from (SELECT * FROM demo_table LIMIT 7 OFFSET 7)
               UNION
               select sum(amount) as sum from (SELECT * FROM demo_table LIMIT 7 OFFSET 14)
               UNION
               select sum(amount) as sum from (SELECT * FROM demo_table LIMIT 7 OFFSET 21));