Wednesday, 14 November 2012

Techniques for adding the numbers

1.Techniques for adding the numbers 1 to 100

Sum from 1 to n =\displaystyle{\frac{n(n+1)}{2}}


Sum from 1 to 100 = \displaystyle{\frac{100(100+1)}{2} = (50)(101) = 5050}

2.Let's prove the second statement.

1 + 22 + 32 + ... + n2 =

3.The sum of cubes

 
\begin{eqnarray*} 
1 + 2 + 3 + 4 + \ldots + n & = & \frac{n(n + 1)}{2} \\ 
1^3 + 2^3 + 3^3 + 4^3 + \ldots + n^3 & = & \left[\frac{n(n + 1)}{2}\right]^2 
\end{eqnarray*} 
  


Wednesday, 7 November 2012

print a triangle of stars using SQL Server


DECLARE @count INT,@num INT,@num1 INT, @space INT, @str varchar(50)
SET @count =10 SET @num = 1
WHILE(@num<=@count)
BEGIN
    SET @num1 = 0 SET @space = @count-@num
    WHILE (@num1<@num)
    BEGIN
        if @str is null
            SET @str = '* '
        ELSE
            SET @str = @str+'* '   SET @num1 = @num1+1
    END
    PRINT (space(@space)+@str)
    SET @num = @num+1   SET @str = null
END

Find Nth column of the Particular Table – Query to Retrieve the Nth value

SELECT TOP 1 *
    FROM (
            SELECT TOP 3 *,ROW_NUMBER() OVER ( ORDER BY ColumnNameBasedOnWhichOrder DESC) AS RowNum
            FROM TableName
        )a 
ORDER BY
    a.RowNum ASC

SELECT TOP 1 ColumnDetailID
FROM (
SELECT DISTINCT TOP 6 ColumnDetailID
FROM TableName
ORDER BY ColumnDetailID DESC) a
ORDER BY ColumnDetailID


SELECT TOP 1 ColumnDetailID
FROM
(
    SELECT TOP 3 ColumnDetailID
    FROM TableName
    ORDER BY ColumnDetailID DESC
) SalarySubquery
ORDER BY ColumnDetailID ASC