Sunday, August 23, 2015

C# Program to Generate Fibonacci Series

This C# Program generates Fibonacci series.The numbers that precedes the series are 0 and 1.The next number is found by adding up the two numbers before it. Here is source code of the C# program which generates a Fibonacci series.The C# program is successfully compiled and executed with Microsoft Visual Studio.The program output is also shown below.
  1. /*
  2.  * C#  Program to Generate Fibonacci Series
  3.  */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8.  
  9. namespace fibonaci
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             int i, count, f1 = 0, f2 = 1, f3 = 0;
  16.             Console.Write("Enter the Limit : ");
  17.             count = int.Parse(Console.ReadLine());
  18.             Console.WriteLine(f1);
  19.             Console.WriteLine(f2);
  20.             for (i = 0; i <= count; i++)
  21.             {
  22.                 f3 = f1 + f2;
  23.                 Console.WriteLine(f3);
  24.                 f1 = f2;
  25.                 f2 = f3;
  26.             }
  27.             Console.ReadLine();
  28.  
  29.         }
  30.     }
  31. }
     
     
    Here is the output of the C# Program:
    
    
    
    Enter the Limit : 10
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    89
    144
     

Difference Between ROW_NUMBER(), RANK(), and DENSE_RANK() in SQL Server?

Solution:
-----------
ROW_NUMBER()
… assigns unique numbers to each row within the PARTITION given the ORDER BY clause. So you’d get:
1
2
SELECT v, ROW_NUMBER() OVER()
FROM t
Note that some SQL dialects (e.g. SQL Server) require an explicit ORDER BY clause in the OVER() clause:
1
2
SELECT v, ROW_NUMBER() OVER(ORDER BY v)
FROM t
The above query returns:
| V | ROW_NUMBER |
|---|------------|
| a |          1 |
| a |          2 |
| a |          3 |
| b |          4 |
| c |          5 |
| c |          6 |
| d |          7 |
| e |          8 |

RANK()
… behaves like ROW_NUMBER(), except that “equal” rows are ranked the same. If we substitute RANK() into our previous query:
1
2
SELECT v, RANK() OVER(ORDER BY v)
FROM t
… then the result we’re getting is this:
| V | RANK |
|---|------|
| a |    1 |
| a |    1 |
| a |    1 |
| b |    4 |
| c |    5 |
| c |    5 |
| d |    7 |
| e |    8 |

As you can see, much like in a sports ranking, we have gaps between the different ranks. We can avoid those gaps by using
DENSE_RANK()
Trivially, DENSE_RANK() is a rank with no gaps, i.e. it is “dense”. We can write:
1
2
SELECT v, DENSE_RANK() OVER(ORDER BY v)
FROM t
… to obtain
| V | DENSE_RANK |
|---|------------|
| a |          1 |
| a |          1 |
| a |          1 |
| b |          2 |
| c |          3 |
| c |          3 |
| d |          4 |
| e |          5 |

One interesting aspect of DENSE_RANK() is the fact that it “behaves like” ROW_NUMBER() when we add the DISTINCT keyword.
1
2
SELECT DISTINCT v, DENSE_RANK() OVER(ORDER BY v)
FROM t
… to obtain
| V | DENSE_RANK |
|---|------------|
| a |          1 |
| b |          2 |
| e |          5 |
| d |          4 |
| c |          3 |
In fact, ROW_NUMBER() prevents you from using DISTINCT, because ROW_NUMBER() generates unique values across the partition before DISTINCT is applied:
1
2
3
SELECT DISTINCT v, ROW_NUMBER() OVER(ORDER BY v)
FROM t
ORDER BY 1, 2
DISTINCT has no effect:
| V | ROW_NUMBER |
|---|------------|
| a |          1 |
| a |          2 |
| a |          3 |
| b |          4 |
| c |          5 |
| c |          6 |
| d |          7 |
| e |          8 |

Putting it all together

A good way to understand the three ranking functions is to see them all in action side-by-side. Run this query
1
2
3
4
5
6
7
SELECT
  v,
  ROW_NUMBER() OVER(ORDER BY v),
  RANK()       OVER(ORDER BY v),
  DENSE_RANK() OVER(ORDER BY v)
FROM t
ORDER BY 1, 2
… or this one (using the SQL standard WINDOW clause, to reuse window specifications):
1
2
3
4
5
6
7
SELECT
  v,
  ROW_NUMBER() OVER(w),
  RANK()       OVER(w),
  DENSE_RANK() OVER(w)
FROM t
WINDOW w AS (ORDER BY v)
… to obtain:
| V | ROW_NUMBER | RANK | DENSE_RANK |
|---|------------|------|------------|
| a |          1 |    1 |          1 |
| a |          2 |    1 |          1 |
| a |          3 |    1 |          1 |
| b |          4 |    4 |          2 |
| c |          5 |    5 |          3 |
| c |          6 |    5 |          3 |
| d |          7 |    7 |          4 |
| e |          8 |    8 |          5 |
 

DotNet and SQL Server Interview Questions for 3+ yrs of Experienced

1)Get the positive and negative sum for following series by using SQL Query:
Table1:
   Test1
    1
   -4
    4
    6
   -2

Solution:
-----------
(i)
SELECT
  SUM(CASE WHEN test1 > 0 THEN test1 ELSE 0 END) as positives_sum,
  SUM(CASE WHEN test1 < 0 THEN test1 ELSE 0 END) as negatives_sum
FROM
  table1

(ii)
select SUM(test1)from Table1 where test1>0 union
select SUM(test1)   from Table1 where test1<0
  
2)What is Row Number Function in SQL Server ?

Solution:
----------
Assigns unique numbers to each row within the PARTITION given the ORDER BY clause.
 Ex: SELECT test1, ROW_NUMBER() OVER(ORDER BY test1) FROM Table1

3)Difference Between ROW_NUMBER(), RANK(), and DENSE_RANK() 
Solution:
-----------
http://monodroidapp.blogspot.in/2015/08/difference-between-rownumber-rank-and.html

4) How to get Distinct values from given series {1,1,2,3,4,3,8}
Solution:
----------
int[] s = {1,1,2,3,4,3,8};
int[] q = s.Distinct().ToArray();
 
5) C#  Program to Generate Fibonacci Series 
Solution:
--------- 
 http://monodroidapp.blogspot.in/2015/08/c-program-to-generate-fibonacci-series.html
 

Wednesday, August 12, 2015

How to know SQL server version?

Solution:
-----------
select @@version

Monday, August 10, 2015

How to make Visual Studio fast?

Solution:

Delete all files from the following path
C:\Users\(User_Name)\AppData\Local\Microsoft\WebsiteCache