yrstruly Posted August 1, 2022 Posted August 1, 2022 I have this function running(see code). I would like to select data beteen dates e.g 30-06-2022 to 31-07-2022. Specificaly for columns Eligible, Fuel Levy, Claimable and % Eligible of purchase. I am gettig zero for these columns. Is my code the reason i am getting these errors or my data missing. More of the views/tables ddl: CREATE VIEW [dbo].[vw_UsageReportCached] AS SELECT * FROM [dbo].[cacheUsageLogbook] WHERE 1=1 AND RegNumber NOT IN (SELECT le.RegNumber FROM lstEquipment AS le WHERE le.Id IN (SELECT EquipmentId FROM mapEquipmentLocation AS mel) ) GO --------------------------------- SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[auditLevyBreakdown]( [Id] [int] IDENTITY(1,1) NOT NULL, [StartDate] [datetime] NOT NULL, [EndDate] [datetime] NULL, [IsActive] [bit] NOT NULL, [CreateDate] [datetime] NOT NULL, [ModifyDate] [datetime] NOT NULL, [RAFLevy] [float] NOT NULL, [FuelLevy] [float] NOT NULL, [PercEligible] [float] NOT NULL, CONSTRAINT [PK_auditLevyBreakdown] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[auditLevyBreakdown] ADD CONSTRAINT [DF_auditLevyBreakdown_IsActive] DEFAULT ((1)) FOR [IsActive] GO ALTER TABLE [dbo].[auditLevyBreakdown] ADD CONSTRAINT [DF_auditLevyBreakdown_CreateDate] DEFAULT (getdate()) FOR [CreateDate] GO ALTER TABLE [dbo].[auditLevyBreakdown] ADD CONSTRAINT [DF_auditLevyBreakdown_ModifyDate] DEFAULT (getdate()) FOR [ModifyDate] GO ------------------------ /****** Object: Table [dbo].[lstEquipment] Script Date: 2022/08/01 17:09:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[lstEquipment]( [Id] [int] IDENTITY(1,1) NOT NULL, [FleetId] [nvarchar](25) NOT NULL, [RegNumber] [nvarchar](25) NULL, [EquipmentDescription] [nvarchar](100) NULL, [ModelId] [int] NOT NULL, [MakeId] [int] NOT NULL, [TankSize] [float] NULL, [ConsumptionTypeId] [int] NULL, [VehicleTypeId] [int] NULL, [DecommissionDate] [datetime] NULL, [CreateDate] [datetime] NOT NULL, [ModifyDate] [datetime] NOT NULL, [IsActive] [bit] NOT NULL, [AFSEquipmentId] [int] NULL, CONSTRAINT [PK_lstEquipment] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[lstEquipment] ADD CONSTRAINT [DF_lstEquipment_CreateDate] DEFAULT (getdate()) FOR [CreateDate] GO ALTER TABLE [dbo].[lstEquipment] ADD CONSTRAINT [DF_lstEquipment_ModifyDate] DEFAULT (getdate()) FOR [ModifyDate] GO ALTER TABLE [dbo].[lstEquipment] ADD CONSTRAINT [DF_lstEquipment_IsActive] DEFAULT ((1)) FOR [IsActive] GO ALTER TABLE [dbo].[lstEquipment] WITH CHECK ADD CONSTRAINT [FK_lstEquipment_lstConsumptionType] FOREIGN KEY([ConsumptionTypeId]) REFERENCES [dbo].[lstConsumptionType] ([Id]) GO ALTER TABLE [dbo].[lstEquipment] CHECK CONSTRAINT [FK_lstEquipment_lstConsumptionType] GO ALTER TABLE [dbo].[lstEquipment] WITH CHECK ADD CONSTRAINT [FK_lstEquipment_lstMake] FOREIGN KEY([MakeId]) REFERENCES [dbo].[lstMake] ([Id]) GO ALTER TABLE [dbo].[lstEquipment] CHECK CONSTRAINT [FK_lstEquipment_lstMake] GO ALTER TABLE [dbo].[lstEquipment] WITH CHECK ADD CONSTRAINT [FK_lstEquipment_lstModel] FOREIGN KEY([ModelId]) REFERENCES [dbo].[lstModel] ([Id]) GO ALTER TABLE [dbo].[lstEquipment] CHECK CONSTRAINT [FK_lstEquipment_lstModel] GO ALTER TABLE [dbo].[lstEquipment] WITH CHECK ADD CONSTRAINT [FK_lstEquipment_lstVehicleType] FOREIGN KEY([VehicleTypeId]) REFERENCES [dbo].[lstVehicleType] ([Id]) GO ALTER TABLE [dbo].[lstEquipment] CHECK CONSTRAINT [FK_lstEquipment_lstVehicleType] GO ------------------ AND SELECT * FROM [dbo].[fn_GetSummaryReport] ( <@pStartDate, date,> ,<@pEndDate, date,>) /****** Object: UserDefinedFunction [dbo].[fn_GetSummaryReport] Script Date: 2022/08/01 16:57:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[fn_GetSummaryReport] ( @pStartDate DATE, @pEndDate DATE ) RETURNS TABLE RETURN SELECT SUM(vurc.QuantityReceived) TotalLitres, SUM(vurc.QuantityReceived * afp.PricePerL) TotalValue, SUM(vurc.EligiblePurchases) EligibleLitres, SUM(vurc.EligiblePurchases * afp.PricePerL) EligibleValue, SUM(vurc.NonEligible) IneligibleLitres, SUM(vurc.NonEligible * afp.PricePerL) IneligibleValue, COUNT(vurc.QuantityReceived) TotalNumberOfTransactions, COUNT(IIF(le.RegNumber = 'RNF',NULL,1)) TotalNumberOfCompleteTransactions, COUNT(IIF(le.RegNumber = 'RNF',1,NULL)) TotalNumberOfFaultyTransactions, SUM(IIF(le.RegNumber = 'RNF',NULL,vurc.QuantityReceived)) TotalVolumeOfCompleteTransactions, SUM(IIF(le.RegNumber = 'RNF',vurc.QuantityReceived,NULL)) TotalVolumeOfFaultyTransactions, alb.RAFLevy RAFLevy, alb.FuelLevy FuelLevy, alb.PercEligible PercEligible, (alb.RAFLevy + alb.FuelLevy) / 100 * alb.PercEligible FinalRebatePL, ( SUM(vurc.EligiblePurchases * alb.FuelLevy) + SUM(vurc.EligiblePurchases * alb.RAFLevy) ) / 100 * alb.PercEligible TotalClaimable FROM vw_UsageReportCached AS vurc LEFT JOIN vw_FuelPrice AS afp ON vurc.TransactionDateTime BETWEEN afp.PurchaseDate AND ISNULL(afp.NextPurchaseDate, GETDATE()) LEFT JOIN auditLevyBreakdown AS alb ON vurc.TransactionDateTime BETWEEN alb.StartDate AND ISNULL(alb.EndDate, GETDATE()) AND alb.IsActive = 1 LEFT JOIN lstEquipment AS le ON le.FleetId = vurc.RegNumber WHERE CAST(vurc.TransactionDateTime AS DATE) BETWEEN @pStartDate AND @pEndDate GROUP BY alb.RAFLevy, alb.FuelLevy, alb.PercEligible GO
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now