SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO
CREATE   VIEW [transform_imr].[Arbeitstage] AS
SELECT [Bundeslaender Id]
     , [Datum]
     , [Wochentage Id]
     , [Arbeitstag Mofr]
     , [Zaehler Mofr]
     , [Summe Mofr]
     , [Arbeitstag Mosa]
     , [Zaehler Mosa]
     , [Summe Mosa]
     , [Feiertage Id]
     , [Jahr]
     , iif([Arbeitstag Mofr] = 1, [Arbeitstage Nr Jahr Mofr], 0) AS [Arbeitstag Nr Jahr Mofr]
     , [Gesamt Arbeitstage Mofr]
     , iif([Arbeitstag Mosa] = 1, [Arbeitstage Nr Jahr Mosa], 0) AS [Arbeitstag Nr Jahr Mosa]
     , [Gesamt Arbeitstage Mosa]
     , day([Datum]) AS [Tag_Dashboard]
     , month([Datum]) AS [Monat_Dashboard]
     , year([Datum]) AS [Jahr_Dashboard]

FROM (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"
           , year([datum]) AS "Jahr"
           , row_number() OVER (PARTITION BY [bundeslaender_id], year([datum]), [arbeitstag_mofr] ORDER BY [datum]) AS "Arbeitstage Nr Jahr Mofr"
           , sum([arbeitstag_mofr]) OVER (PARTITION BY [bundeslaender_id], year([datum])) AS "Gesamt Arbeitstage Mofr"
           , row_number() OVER (PARTITION BY [bundeslaender_id], year([datum]), [arbeitstag_mosa] ORDER BY [datum]) AS "Arbeitstage Nr Jahr Mosa"
           , sum([arbeitstag_mosa]) OVER (PARTITION BY [bundeslaender_id], year([datum])) AS "Gesamt Arbeitstage Mosa"
      FROM [data].[Arbeitstage]) [V1]
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

GO
