Hi there,
version: SQL SERVER 2008 SP1
DDL:
CREATE TABLE [dbo].[SalePlans]( [ProductId] [nvarchar](30) NOT NULL, [SupplierId] [nvarchar](30) NOT NULL, [PeriodType] [tinyint] NOT NULL, [SaleDate] [datetime] NOT NULL, [Sale] [float] NOT NULL, [Origin] [smallint] NULL, [Description] [nvarchar](255) NULL, [SafetyStock] [float] NULL, CONSTRAINT [PK_SalePlan] PRIMARY KEY CLUSTERED ( [ProductId] ASC, [SupplierId] ASC, [PeriodType] ASC, [SaleDate] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY] ) ON [PRIMARY] GO
This is the view:
ALTER view [dbo].[v_SalePlansByMonth] as select SupplierID, productid ,dbo.firstdayofmonth(GetDate()) as [Start Date], Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM,0,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM,0,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanCurrentMonth] ,Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM, +1,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM, +1,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanMonth_1] ,Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM, +2,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM, +2,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanMonth_2] ,Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM, +3,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM, +3,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanMonth_3] ,Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM, +4,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM, +4,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanMonth_4] ,Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM, +5,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM, +5,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanMonth_5] ,Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM, +6,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM, +6,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanMonth_6] ,Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM, +7,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM, +7,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanMonth_7] ,Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM, +8,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM, +8,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanMonth_8] ,Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM, +9,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM, +9,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanMonth_9] ,Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM,+10,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM,+10,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanMonth_10] ,Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM,+11,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM,+11,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanMonth_11] ,Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM,+12,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM,+12,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanMonth_12] ,Round(sum(case when SaleDate between dbo.firstdayofmonth(DATEADD(MM,+13,GetDate())) AND dbo.Lastdayofmonth(DATEADD(MM,+13,GetDate())) THEN Sale ELSE 0 END),0) AS [SalePlanMonth_13] from SalePlans where PeriodType = 1 group by SupplierId, productid
UDFs:
CREATE FUNCTION [dbo].[firstdayofmonth] (@date1 datetime) -- Skilar fyrsta degi viku, án tímasetningar RETURNS datetime AS BEGIN --return dbo.trimtime(@date1 - (DATEPART(D, @date1) - 1)) return DATEADD(mm, DATEDIFF(mm, 0, @date1), 0) END GO -- CREATE FUNCTION [dbo].[lastdayofmonth] (@date1 datetime) -- Skilar fyrsta degi viku, án tímasetningar RETURNS datetime AS BEGIN --return dateadd(mm,1,@date1) return dateadd(dd,-1,dbo.firstdayofmonth(dateadd(mm,1,@date1))) END GOthe fixed table has 64 million of rows
For this current year (having real data in the table) the view does not return data for month 5,6 and 7.
Can you light up this code in some sense?
Thanks a lot,