First Create One function that will generate very first part
CREATE FUNCTION pGetCallsCount
(
@totalcall INT = 51,
@Count INT = 4
)
RETURNS INT
AS
BEGIN
DECLARE @result1 INT
DECLARE @r INT = @totalcall % @Count
IF(@r=0)
BEGIN
IF(@count=1)
SET @result1=@totalcall
ELSE
SET @result1=@totalcall/@count
END
ELSE
BEGIN
SET @result1=(@Count-@r)+@totalcall -- surely divided
SET @result1=@result1/@Count
END
RETURN @result1
END
GO
Then Call that function to create next parts like mentioned below :-
DECLARE @totalcall INT = 43
DECLARE @Count INT = 4
DECLARE @r INT = @totalcall % @Count
DECLARE @totalsum INT=0
DECLARE @result INT
DECLARE @totalcalldemo INT
SELECT @totalcalldemo=@totalcall
WHILE(@totalsum<>@totalcalldemo)
BEGIN
SELECT @result=dbo.pGetCallsCount(@totalcall,@Count) -- (51,4),
SET @totalcall=@totalcall-@result
SET @Count=@Count-1
SET @totalsum=@totalsum+@result
PRINT @result
END
OUTPUT
11
11
11
10
its nice article, but can i conver this scalar valued function to table valued fuction
ReplyDeleteyes you can, but it will simply convert that scalar value function into table value
DeleteNice solution, thanks. Can you please explain why two main variables in functions are set to 51 and 4?
ReplyDeleteIt is just an example,,for demo only
Delete