Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Kick Start My Brain
Message
From
17/02/2011 13:04:46
 
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
01500443
Message ID:
01500572
Views:
32
>>>T-SQL has some ideas for running totals (like Jeff Moden quirky update), but it's not official solution and not supported. So, for these kind of problems T-SQL may not be a good choice.
>>>
>>>Unless I misunderstood the problem.
>>>
>>>Perhaps you can post your table structure, what exactly do you want to get in SQL forum, then it will be easier to provide a solution.
>>
>>Maybe this will get me the number of premiums paid:
>>
>>
	SELECT NumberMonthsToAdd = SUM(
>>		CASE [PremiumMode] 
>>			 WHEN 'Monthly' THEN NumberOfPremiums * 1
>>			 WHEN 'Quarterly' THEN NumberOfPremiums * 3
>>			 WHEN 'Semi-Annually' THEN NumberOfPremiums * 6
>>			 WHEN 'Annually' THEN NumberOfPremiums * 12
>>			 ELSE 0
>>		END )
>>	FROM [dbo].[ReceiptDetails]
>>
>>
>>This way I don't need to loop through to get the result. What do you think?
>
>Looks good to me.

Since this is my first UDF, I'd appreciate a critique of it by anyone here.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION GetDatePaidTo
(
	-- Add the parameters for the function here
	@PolicyHeaderPK uniqueidentifier
)
RETURNS datetime
AS
BEGIN
	-- Declare the return variable here
	DECLARE @DatePaidTo datetime

	-- Add the T-SQL statements to compute the return value here
	declare @MonthsToAdd int
	declare @PremiumMode varchar(15)
	declare @IssueDate datetime
	
	SELECT @MonthsToAdd = SUM(
		CASE [PremiumMode] 
			 WHEN 'Monthly' THEN NumberOfPremiums * 1
			 WHEN 'Quarterly' THEN NumberOfPremiums * 3
			 WHEN 'Semi-Annually' THEN NumberOfPremiums * 6
			 WHEN 'Annually' THEN NumberOfPremiums * 12
			 ELSE 0
		END )
	FROM [dbo].[ReceiptDetails]
	inner join [dbo].[Receipts] on ReceiptFK = ReceiptPK
	WHERE 
		([PolicyHeaderFK] = @PolicyHeaderPK
		AND ReceiptTypeFK = 3)

	SELECT @IssueDate = IssueDate
	FROM [dbo].[Policyheaders]
		INNER JOIN [dbo].[Policies]	ON [PolicyHeaderPK] = [PolicyHeaderFK]
		INNER JOIN (SELECT [PolicyHeaderFK], MAX(LastChanged) as LastChanged
						FROM [dbo].[Policies]
						GROUP BY [PolicyHeaderFK]) subquery ON [Policies].[PolicyHeaderFk] = subquery.policyHeaderFK AND Policies.LastChanged = subquery.LastChanged
		inner join [dbo].[Plans] on [PlanFK] = [PlanPK]
		inner join [dbo].[Clients] on [ClientFK] = [ClientPK]
		INNER JOIN [dbo].[Status] on [StatusFK] = [StatusPK]
	WHERE 
		([PolicyHeaderPK] = @PolicyHeaderPK)

	SET @DatePaidTo = DATEADD(M, @MonthsToAdd, @IssueDate)
	
	-- Return the result of the function
	RETURN @DatePaidTo

END
GO
Thanks
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform