if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FilialenMonate]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[FilialenMonate]

###

SELECT Filiale, Artikelnummer, WG_Nr, Lieferantennr, MONTH(Datum) AS Monat, SUM(Anzahl) AS ANZAHL, AVG(EPreis) AS DEPreis, AVG(EK) AS DEK, SUM(GRohertrag) AS GRohertrag, SUM(GPreis) AS GPreis
INTO   FilialenMonate
FROM   Kassenbericht
GROUP BY Filiale, Artikelnummer, WG_Nr, Lieferantennr, MONTH(Datum)

###

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Monate]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Monate]

###

SELECT Artikelnummer, WG_Nr, Lieferantennr, Monat, SUM(Anzahl) AS ANZAHL, AVG(DEPreis) AS DEPreis, AVG(DEK) AS DEK, SUM(GRohertrag) AS GRohertrag, SUM(GPreis) AS GPreis
INTO   Monate
FROM   FilialenMonate
GROUP BY Artikelnummer, WG_Nr, Lieferantennr, Monat

###




if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FilialenWGruppen]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[FilialenWGruppen]

###

SELECT Filiale, WG_Nr, Lieferantennr, Monat, SUM(Anzahl) AS ANZAHL, AVG(DEPreis) AS DEPreis, AVG(DEK) AS DEK, SUM(GRohertrag) AS GRohertrag, SUM(GPreis) AS GPreis
INTO   FilialenWGruppen
FROM   FilialenMonate
GROUP BY Filiale, WG_Nr, Lieferantennr, Monat

###

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MonateWGruppen]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[MonateWGruppen]

###

SELECT WG_Nr, Lieferantennr, Monat, SUM(Anzahl) AS ANZAHL, AVG(DEPreis) AS DEPreis, AVG(DEK) AS DEK, SUM(GRohertrag) AS GRohertrag, SUM(GPreis) AS GPreis
INTO   MonateWGruppen
FROM   FilialenWGruppen
GROUP BY WG_Nr, Lieferantennr, Monat

###




if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FilialenLiefer]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[FilialenLiefer]

###

SELECT Filiale, Lieferantennr, Monat, SUM(Anzahl) AS ANZAHL, AVG(DEPreis) AS DEPreis, AVG(DEK) AS DEK, SUM(GRohertrag) AS GRohertrag, SUM(GPreis) AS GPreis
INTO   FilialenLiefer
FROM   FilialenWGruppen
GROUP BY Filiale, Lieferantennr, Monat

###

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MonateLiefer]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[MonateLiefer]

###

SELECT Lieferantennr, Monat, SUM(Anzahl) AS ANZAHL, AVG(DEPreis) AS DEPreis, AVG(DEK) AS DEK, SUM(GRohertrag) AS GRohertrag, SUM(GPreis) AS GPreis
INTO   MonateLiefer
FROM   FilialenLiefer
GROUP BY Lieferantennr, Monat

###