Registriert seit: 26. Aug 2004
Ort: Nebel auf Amrum
3.154 Beiträge
Delphi 7 Enterprise
|
AW: MSSQL Schleife ueber einen gewissen Zeitraum
16. Feb 2012, 13:03
Vielleicht so...
SQL-Code:
DECLARE @t INT
DECLARE @m INT
DECLARE @j INT
DECLARE @minj INT
DECLARE @maxj INT
DECLARE @tag TABLE (
tag INT
)
DECLARE @monat TABLE (
monat INT
)
DECLARE @jahr TABLE (
jahr INT
)
SET @t = 1
WHILE @t <= 31 BEGIN
INSERT INTO @tag VALUES (@t)
SET @t = @t + 1
END
SET @m = 1
WHILE @m <= 12 BEGIN
INSERT INTO @monat VALUES (@m)
SET @m = @m + 1
END
SELECT @minj = MIN(YEAR(datum)), @maxj = MAX(YEAR(datum))
FROM tabelle
SET @j = @minj
WHILE @j <= @maxj BEGIN
INSERT INTO @jahr VALUES (@j)
SET @j = @j + 1
END
SELECT *
FROM (SELECT *
FROM (SELECT *, DAY(DATEADD(d, -1,
DATEADD(m, 1,
CONVERT(DATETIME, '01.' +
CONVERT(VARCHAR, monat) + '.' +
CONVERT(VARCHAR, jahr))))) maxtage
FROM @jahr, @monat) x, @tag, (SELECT DISTINCT lappenid FROM tabelle) y
WHERE tag <= maxtage) x
WHERE NOT EXISTS (SELECT *
FROM tabelle
WHERE DAY(datum) = x.tag
AND MONTH(datum) = x.monat
AND YEAR(datum) = x.jahr
AND lappenid = x.lappenid)
ORDER BY jahr, monat, tag
|
|
Zitat
|