SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO
CREATE   VIEW transform_ims.current_date_fuer_ims
AS
SELECT "Bundeslaender Id" AS "Bundeslaender Id",
	"Datum" AS "Datum",
	"Wochentage Id" AS "Wochentage Id",
	"Arbeitstag Mofr" AS "Arbeitstag Mofr",
	"Zaehler Mofr" AS "Zaehler Mofr",
	"Summe Mofr" AS "Summe Mofr",
	"Arbeitstag Mosa" AS "Arbeitstag Mosa",
	"Zaehler Mosa" AS "Zaehler Mosa",
	"Summe Mosa" AS "Summe Mosa",
	"Feiertage Id" AS "Feiertage Id",
	"Jahr" AS "Jahr",
	CASE 
		WHEN ("Arbeitstag Mofr" = 1)
			THEN 1
		ELSE NULL
		END AS "Arbeitstag Nr Jahr",
	"Gesamt Arbeitstage" AS "Gesamt Arbeitstage"
FROM (
	SELECT Arbeitstage."bundeslaender_id" AS "Bundeslaender Id",
		Arbeitstage."datum" AS "Datum",
		Arbeitstage."wochentage_id" AS "Wochentage Id",
		Arbeitstage."arbeitstag_mofr" AS "Arbeitstag Mofr",
		Arbeitstage."zaehler_mofr" AS "Zaehler Mofr",
		Arbeitstage."summe_mofr" AS "Summe Mofr",
		Arbeitstage."arbeitstag_mosa" AS "Arbeitstag Mosa",
		Arbeitstage."zaehler_mosa" AS "Zaehler Mosa",
		Arbeitstage."summe_mosa" AS "Summe Mosa",
		Arbeitstage."feiertage_id" AS "Feiertage Id",
		(getdate())  AS "Jahr",
		CASE 
			WHEN (Arbeitstage."datum" BETWEEN convert(DATETIME, '2008-01-01T00:00:00.000') AND convert(DATETIME, '2008-12-31T00:00:00.000'))
				THEN (252)
			WHEN (Arbeitstage."datum" BETWEEN convert(DATETIME, '2009-01-01T00:00:00.000') AND convert(DATETIME, '2009-12-31T00:00:00.000'))
				THEN (252)
			WHEN (Arbeitstage."datum" BETWEEN convert(DATETIME, '2010-01-01T00:00:00.000') AND convert(DATETIME, '2010-12-31T00:00:00.000'))
				THEN (253)
			WHEN (Arbeitstage."datum" BETWEEN convert(DATETIME, '2011-01-01T00:00:00.000') AND convert(DATETIME, '2011-12-31T00:00:00.000'))
				THEN (250)
			WHEN (Arbeitstage."datum" BETWEEN convert(DATETIME, '2012-01-01T00:00:00.000') AND convert(DATETIME, '2012-12-31T00:00:00.000'))
				THEN (249)
			ELSE (250)
			END AS "Gesamt Arbeitstage"
	FROM "data"."Arbeitstage" Arbeitstage

	WHERE (
			(
				(
					(Arbeitstage."bundeslaender_id" = '2')
					AND (
						((year(Arbeitstage."datum")) = ((year(((getdate()) ))) - 1))
						OR ((year(Arbeitstage."datum")) = ((year(((getdate()) )))))
						)
					)
				AND (Arbeitstage."datum" <= ((getdate()) ))
				)
			OR (
				(
					(
						(Arbeitstage."bundeslaender_id" = '2')
						AND ((year(Arbeitstage."datum")) = ((year(((getdate()) ))) - 1))
						)
					AND (Arbeitstage."datum" <= ((getdate()) ))
					)
				AND (Arbeitstage."datum" IN (convert(DATETIME, '2012-01-01T00:00:00.000'), convert(DATETIME, '2013-01-01T00:00:00.000'), convert(DATETIME, '2014-01-01T00:00:00.000'), convert(DATETIME, '2015-01-01T00:00:00.000'), convert(DATETIME, '2016-01-01T00:00:00.000'), convert(DATETIME, '2017-01-01T00:00:00.000')))
				)
			)
		-- order by "Bundeslaender Id" asc,"Datum" asc,"Wochentage Id" asc,"Arbeitstag Mofr" asc,"Zaehler Mofr" asc,"Summe Mofr" asc,"Arbeitstag Mosa" asc,"Zaehler Mosa" asc,"Summe Mosa" asc,"Feiertage Id" asc,"Gesamt Arbeitstage" asc
	) D1
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

GO
