
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER view [dbo].[ZAMOWIENIA_REKLAMY] as
SELECT     z.idZamowienia,
           COUNT(r.ReklamaID) AS iloscReklam, 
			ROUND(SUM((r.[CENA JEDN] - r.[RABAT WARTOŚĆ]) * r.krotność * (1 + wyroznienie_procent)), 2) 
		   AS wartoscNetto, SUM(r.Brutto_Euro) 
              AS kwotaWalutaBrutto, r.Brutto_Euro_Miano
FROM         dbo.zamowienia AS z left JOIN
                      dbo.REKLAMA AS r ON z.idZamowienia = r.idZamowienia
                     where r.Brutto_Euro_Miano is null or r.Brutto_Euro_Miano =''
GROUP BY z.idZamowienia,r.Brutto_Euro_Miano
union 
SELECT     z.idZamowienia,
           COUNT(r.ReklamaID) AS iloscReklam, 
		    round(SUM(r.Brutto_Euro*r.waluta_kurs/(r.waluta_Przelicznik*(1+r.VAT))),2) 
				  AS wartoscNetto, SUM(r.Brutto_Euro) 
              AS kwotaWalutaBrutto, r.Brutto_Euro_Miano
FROM         dbo.zamowienia AS z left JOIN
                      dbo.REKLAMA AS r ON z.idZamowienia = r.idZamowienia
                     where not (r.Brutto_Euro_Miano is null or r.Brutto_Euro_Miano ='')
GROUP BY z.idZamowienia,r.Brutto_Euro_Miano
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO

