- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
-- находим текущий месяц
FOR rec IN
SELECT date_part ('month', now()) as ma, date_part ('year', now()) as ya
LOOP
m1 = rec.ma-1;
IF m1=0 THEN m1=12; END IF;
Y1 = rec.ya;
IF m1=12 THEN Y1 = rec.ya - 1; END IF;
END LOOP;
FOR rec IN
SELECT count(v.partner_id) as cnt
FROM v, a where v.id=a.id and period like '%'||m1||'-'||Y1||' по%' and accepted = 1 and active=1
LOOP
-- вырезано
-- а десятком строк ниже
-- находим предыдущие 3 месяца
m2 = m1 - 1;
IF m2 = 0 THEN m2 = 12; END IF;
IF m1<m2 THEN Y2 = Y1 - 1; ELSE Y2 = Y1; END IF;
m3 = m2 - 1;
IF m3 = 0 THEN m3 = 12; END IF;
IF m2<m3 THEN Y3 = Y2 - 1; ELSE Y3 = Y2; END IF;
m4 = m3 - 1;
IF m4 = 0 THEN m4 = 12; END IF;
IF m3<m4 THEN Y4 = Y3 - 1; ELSE Y4 = Y3; END IF;