Für solche Fälle habe ich stored procedure.
Diese liefert mir den Zeitstrahl mit allen Monaten.
Als ADATUM_VON, ADATUM_BIS wird ein beliebiger Tag im Monat angegeben.
Bsp.:
SQL-Code:
select
JM.YYYYMM,
JM.JAHR,
JM.MONAT,
Sum(R.Betragnetto) as BetragNetto
from
SP_JJMM(:von, :bis) JM -- von/bin sind ein beliebiger Tag im Monat
left join rechnung R on (RECH.RECHNUNGDAT between JM.FIRSTDAY and JM.LASTDAY)
group by
JM.YYYYMM,
JM.JAHR,
JM.MONAT
SQL-Code:
create or alter procedure SP_JJMM (
ADATUM_VON date,
ADATUM_BIS date)
returns (
YYYYMM integer,
JAHR integer,
MONAT integer,
FIRSTDAY date,
LASTDAY date)
AS
declare M INTEGER;
declare J INTEGER;
declare BM INTEGER;
declare BJ INTEGER;
begin
IF (ADATUM_VON is not NULL) THEN
BEGIN
J = EXTRACT(YEAR FROM ADATUM_VON);
M = EXTRACT(MONTH FROM ADATUM_VON);
END ELSE
BEGIN
J = EXTRACT(YEAR FROM CURRENT_DATE);
M = EXTRACT(MONTH FROM CURRENT_DATE);
END
IF (ADATUM_BIS is not NULL) THEN
BEGIN
BJ = EXTRACT(YEAR FROM ADATUM_BIS);
BM = EXTRACT(MONTH FROM ADATUM_BIS);
END ELSE
BEGIN
BJ = EXTRACT(YEAR FROM CURRENT_DATE);
BM = EXTRACT(MONTH FROM CURRENT_DATE);
END
JAHR = NULL;
MONAT = NULL;
WHILE (J * 100 + M <= BJ * 100 + BM) DO
BEGIN
JAHR = J;
MONAT = M;
YYYYMM = J * 100 + M;
FIRSTDAY = J||'-'||M||'-01';
LASTDAY = DATEADD(MONTH, 1, FIRSTDAY);
LASTDAY = LASTDAY -1;
suspend;
M = M + 1;
IF (M > 12) THEN
BEGIN
M = 1;
J = J + 1;
end
END
end