Skip to main content

Divide any number into Equal parts in SQL SERVER


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

Comments

  1. its nice article, but can i conver this scalar valued function to table valued fuction

    ReplyDelete
    Replies
    1. yes you can, but it will simply convert that scalar value function into table value

      Delete
  2. Nice solution, thanks. Can you please explain why two main variables in functions are set to 51 and 4?

    ReplyDelete

Post a Comment

Popular posts from this blog

Export Doc,Access,Image,CSV,Excel,Pdf,XML,HTML,Text,Print of Gridview in Asp.net

First import itextsharp.dll in the solution and use three namespces (basically for pdf) using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html.simpleparser;          Then Use the following code :- on each button  click  protected void Page_Load(object sender, EventArgs e)         {             if (!Page.IsPostBack)             {                 BindGridDetails(GridView1);             }         }                 protected DataTable BindGridDetails(GridView GridView1)         {             DataTable dt = new DataTable();             dt.Columns.Add("Student_ID", typeof(Int32));             dt.Columns.Add("Student_Name", typeof(string));             dt.Columns.Add("Education", typeof(string));             dt.Columns.Add("City", typeof(string));             DataRow dtrow = dt.NewRow();             dtrow["Student_ID"] = 1;             dtrow["Student_Name"] = "Musakkhir";   

Ip Sec Internet Security Basic Structure

The IPsec suite is an  open standard . IPsec uses the following  protocols  to perform various functions: Authentication Headers (AH)  provide connectionless  integrity  and data origin  authentication  for IP  datagrams  and provides protection against  replay attacks . Encapsulating Security Payloads (ESP)  provide  confidentiality , data-origin  authentication , connectionless  integrity , an anti-replay service (a form of partial sequence integrity), and limited traffic-flow confidentiality. Security Associations (SA)  provide the bundle of algorithms and data that provide the parameters necessary to AH and/or ESP operations. The  Internet Security Association and Key Management Protocol  (ISAKMP) provides a framework for authentication and key exchange, with actual authenticated keying material provided either by manual configuration with pre-shared keys,  Internet Key Exchange  (IKE and IKEv2),  Kerberized Internet Negotiation of Keys  (KINK), or IPSECKEY  DNS records .

Advent of code 2022 day 22 part 1

  function main(input, input1) {     let grid = input.split( '\n' );     grid.shift();     // only in big input     for ( let i = 0 ; i < 100 ; i++) {         grid[i] = '                                                  ' + grid[i];     }     //console.log(grid[100])     grid = grid.map(x => x.split( '' ))     // find first left top allowed     let temppos = - 1 ;     grid[ 0 ].forEach((element, i) => {         if (temppos == - 1 && element == '.' ) {             temppos = i;         }     });     let initpos = new pos(temppos, 0 )     // console.log(initpos)     // make grid equal shape     let maxlength = 0 ;     grid.forEach(el => {         if (el.length > maxlength) {             maxlength = el.length         }     })     console.log(maxlength)     grid.forEach(el => {         if (el.length < maxlength) {             let diff = maxlength - el.length;             while (diff > 0 ) {                 di