DotNet Solution

  • Home
  • Asp.net
    • Controls
    • DataControl
    • Ajax
  • Web Design
    • Html
    • Css
    • Java Script
  • Sql
    • Queries
    • Function
    • Stored Procedures
  • MVC
    • OverView
    • Create First Application
  • BootStrap
    • Collapse Function

Saturday, 25 June 2016

Unicque Row Num and Same Rank in Sql server

  Unknown       00:18       Function       No comments    

 Use Of  Row_Number() ,Rank() function , Dense_Rank()

Row_Number()

This function will assign a unique id to each row returned from the query.

Consider the following query:

DECLARE @Table TABLE (
      Col_Value varchar(2)
)

INSERT INTO @Table (Col_Value)
      VALUES ('A'),('A'),('A'),('B'),('B'),('C'),('C');

SELECT
      Col_Value,
      ROW_NUMBER() OVER (ORDER BY Col_Value) AS 'Row ID'
FROM
      @Table;    

After executing it, we will get:

 Col_Value  Row ID
           A     1
           A     2
           A     3
           B     4
           B     5
           C     6
           C     7

As we notice, each and every row has a unique ID.

Rank()
This function will assign a unique number to each distinct row, but it leaves a gap between the groups. Let me explain with a query, we will use the same query we used above with Rank().



SELECT
      Col_Value,
      Rank() OVER (ORDER BY Col_Value) AS 'Row ID'
FROM
      @Table;
 
After executing it, we will get:
 
Col_Value Row ID
     A    1
     A    1
     A    1
     B    4
     B    4
     C    6
     C    6 

As we can see,rowid is unique for each distinct value, but with a gap. What is this gap?
This gap represents number of occurrence. For example: value a is repeated thrice and has rank 1, the next rank will be 1+3=4. Same with the next value 4+2=6 and so on.

Dense_Rank()

 his function is similar to Rank with only difference, this will not leave gaps between groups.
 So if we use the same query used for Rank, then:

SELECT
      Col_Value,
      DENSE_RANK() OVER (ORDER BY Col_Value) AS 'Row ID'
FROM
      @Table;
 
 After executing it, we will get:
Col_Value Row ID
        A     1
        A     1
        A     1
        B     2
        B     2
        C     3
        C     3


So it is clear that it generates a unique id for each group and without repetition.
As we are clear now what these functions do, we can use them in different scenarios based our need.
For example: Row_Number() can be used if we want to skip and fetch some records. Take a look at this query:








Similarly, we can use Dense_Rank()when we need to calculate  row id with Select Distinct.

SELECT
DISTINCT
      Col_Value,
      DENSE_RANK() OVER (ORDER BY Col_Value) AS 'Row ID'
FROM
      @Table;

Will return:-

    Col_Value Row ID
        A     1
        B     2
        C     3






 

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
Email This BlogThis! Share to X Share to Facebook
Newer Post Older Post Home

0 comments :

Post a Comment

Popular Posts

  • Auto Increment Id in Sql Server
    SQL server identity column values use in table Steps- Firstly Open-> Sql Server Create Table -> First Field Like ID Usually Autoi...
  • Open First Application in MVC
    How To Create First Application in MVC.Follows Some Steps-    STEP 1 . Firstly Go Start menu in Computer And Click on Installed Visual s...
  • The Evolution of MVC
    Microsoft had introduced ASP.NET MVC in .Net 3.5,since then lots of new features have been added.The following table list brief history of ...
  • Asp.net- MultiView
    MultiView and View controls allow you to divide the content of a page into different groups, displaying only one group at a time. Each Vie...
  • Install Visual Stdio Step by Step Process
    How to install visual studio 2015 Step by Step Process like => Step   (1) : Once downloading is complete, run the installer. The f...
  • Values Enter With SplitFunction in Sql Server
    Basically We Use Split Function Thorugh Commma Sepertaed string value with  Comma  Means One Then more value through split function Enter...
  • Numeric Value Validation Through JavaScript
    Validate Only Numeric Value Enter In TextBox Using JavaScript function Firstly Write Java Script Function in <head tag> in Asp.Net ...
  • Calender Control In Ajax
    <% @ Page Language ="C#" AutoEventWireup ="true" CodeFile ="Default.aspx.cs" Inherits ="_Def...
  • MVC Framework Architecture
    MVC Flow- MVC application takes place when certain request comes from the client. The diagram below shows the flow: Flow Steps ...
  • MVC vs ASP.NET
    There are various positive points to Using MVC 1.  TDD support out of the box as most of the design is based on interfaces. 2.  SEO fr...

Blog Archive

  • ▼  2016 ( 36 )
    • ►  February ( 1 )
    • ►  March ( 5 )
    • ►  April ( 1 )
    • ▼  June ( 10 )
      • Ajax FilteredTextBox Extender
      • Current Date
      • CSS 3D Transforms
      • Use SQL Server Functions
      • Show Table Data
      • Create Table
      • Rank Given In Table
      • COALESCE function
      • Unicque Row Num and Same Rank in Sql server
      • Table Structure Acces in Database
    • ►  July ( 6 )
    • ►  November ( 8 )
    • ►  December ( 5 )
  • ►  2017 ( 1 )
    • ►  January ( 1 )
Powered by Blogger.

Categories

  • Ajax
  • AllFunction
  • Controls
  • CreateApplication
  • css
  • Function
  • javascript
  • Js
  • over view
  • OverView
  • Queries
  • StoredProcedures

Text Widget

Sample Text

Pages

  • Home

Copyright © DotNet Solution | Powered by Blogger
Design by Vibha Acharya