Search the Blog

Showing posts with label SQL Server Error. Show all posts
Showing posts with label SQL Server Error. Show all posts

Sunday, September 27, 2020

Differnece Between RANK, DENSE_RANK and ROW_NUMBER

 /*Rank , RowNumber, Danse rank*/

Rank Function - It is used to get the rank of records on the basis of any column. It skips the records if matched the same.

DENSE_RANK Function - It is used to get the rank of records on the basis of any column. It doesn't skips the records if matched the same.

ROW_NUMBER Function - It is used to get the order of records on the basis of any column. It reset itself once the shorted column values gets changed.

CREATE TABLE Dizsweb_Cars_SQL_Server_Rank_dense_Rank_row_number
(
id INT,
name VARCHAR(50) NOT NULL,
company VARCHAR(50) NOT NULL,
power INT NOT NULL
)



INSERT INTO Dizsweb_Cars_SQL_Server_Rank_dense_Rank_row_number
VALUES
(1, 'Corrolla', 'Toyota', 1800),
(2, 'City', 'Honda', 1500),
(3, 'C200', 'Mercedez', 2000),
(4, 'Vitz', 'Toyota', 1300),
(5, 'Baleno', 'Suzuki', 1500),
(6, 'C500', 'Mercedez', 5000),
(7, '800', 'BMW', 8000),
(8, 'Mustang', 'Ford', 5000),
(9, '208', 'Peugeot', 5400),
(10, 'Prius', 'Toyota', 3200),
(11, 'Atlas', 'Volkswagen', 5000),
(12, '110', 'Bugatti', 8000),
(13, 'Landcruiser', 'Toyota', 3000),
(14, 'Civic', 'Honda', 1800),
(15, 'Accord', 'Honda', 2000)

RANK Function
The RANK function is used to retrieve ranked rows based on the condition of the ORDER BY clause.

SELECT name,company, power,
RANK() OVER(ORDER BY power DESC) AS PowerRank
FROM Dizsweb_Cars_SQL_Server_Rank_dense_Rank_row_number

Output
name        company        power    PowerRank
800            BMW             8000    1
110            Bugatti            8000    1
208            Peugeot          5400    3
Atlas        Volkswagen     5000    4
Mustang        Ford           5000    4
C500        Mercedez        5000    4
Prius        Toyota              3200    7
Landcruiser    Toyota       3000    8
Accord        Honda          2000    9
C200        Mercedez        2000    9
Corrolla    Toyota            1800    11
Civic        Honda             1800    11
City        Honda               1500    13
Baleno        Suzuki          1500    13
Vitz        Toyota               1300    15




SELECT name,company, power,
RANK() OVER(PARTITION BY company ORDER BY power DESC) AS PowerRank
FROM Dizsweb_Cars_SQL_Server_Rank_dense_Rank_row_number

name                                               company                                            power       PowerRank
-------------------------------------------------- -------------------------------------------------- ----------- --------------------
800                                                BMW                                                 8000        1
110                                                Bugatti                                               8000        1
Mustang                                            Ford                                               5000        1
Accord                                             Honda                                              2000        1
Civic                                              Honda                                                1800        2
City                                               Honda                                                 1500        3
C500                                               Mercedez                                          5000        1
C200                                               Mercedez                                          2000        2
208                                                Peugeot                                              5400        1
Baleno                                             Suzuki                                              1500        1
Prius                                              Toyota                                                3200        1
Landcruiser                                        Toyota                                           3000        2
Corrolla                                           Toyota                                              1800        3
Vitz                                               Toyota                                                 1300        4
Atlas                                              Volkswagen                                        5000        1




DENSE_RANK Function

The DENSE_RANK function is similar to RANK function however the DENSE_RANK function does not skip any ranks if there is a tie between the ranks of the preceding records. Take a look at the following script.

SELECT name,company, power,
dense_RANK() OVER(ORDER BY power DESC) AS PowerRank
FROM Dizsweb_Cars_SQL_Server_Rank_dense_Rank_row_number


name                                               company                                            power       PowerRank
-------------------------------------------------- -------------------------------------------------- ----------- --------------------
800                                                BMW                                                8000        1
110                                                Bugatti                                            8000        1
208                                                Peugeot                                            5400        2
Atlas                                              Volkswagen                                         5000        3
Mustang                                            Ford                                               5000        3
C500                                               Mercedez                                           5000        3
Prius                                              Toyota                                             3200        4
Landcruiser                                        Toyota                                             3000        5
Accord                                             Honda                                              2000        6
C200                                               Mercedez                                           2000        6
Corrolla                                           Toyota                                             1800        7
Civic                                              Honda                                              1800        7
City                                               Honda                                              1500        8
Baleno                                             Suzuki                                             1500        8
Vitz                                               Toyota                                             1300        9


SELECT name,company, power,
DENSE_RANK() OVER(PARTITION BY company ORDER BY power DESC) AS DensePowerRank
FROM Dizsweb_Cars_SQL_Server_Rank_dense_Rank_row_number

name                                               company                                            power       DensePowerRank
-------------------------------------------------- -------------------------------------------------- ----------- --------------------
800                                                BMW                                                8000        1
110                                                Bugatti                                            8000        1
Mustang                                            Ford                                               5000        1
Accord                                             Honda                                              2000        1
Civic                                              Honda                                              1800        2
City                                               Honda                                              1500        3
C500                                               Mercedez                                           5000        1
C200                                               Mercedez                                           2000        2
208                                                Peugeot                                            5400        1
Baleno                                             Suzuki                                             1500        1
Prius                                              Toyota                                             3200        1
Landcruiser                                        Toyota                                             3000        2
Corrolla                                           Toyota                                             1800        3
Vitz                                               Toyota                                             1300        4
Atlas                                              Volkswagen                                         5000        1




ROW_NUMBER Function


Unlike the RANK and DENSE_RANK functions, the ROW_NUMBER function simply returns the row number of the sorted records starting with 1

SELECT name,company, power,
ROW_NUMBER() OVER(ORDER BY power DESC) AS RowRank
FROM Cars

name                                               company                                            power       RowRank
-------------------------------------------------- -------------------------------------------------- ----------- --------------------
800                                                BMW                                                8000        1
110                                                Bugatti                                            8000        2
208                                                Peugeot                                            5400        3
Atlas                                              Volkswagen                                         5000        4
Mustang                                            Ford                                               5000        5
C500                                               Mercedez                                           5000        6
Prius                                              Toyota                                             3200        7
Landcruiser                                        Toyota                                             3000        8
Accord                                             Honda                                              2000        9
C200                                               Mercedez                                           2000        10
Corrolla                                           Toyota                                             1800        11
Civic                                              Honda                                              1800        12
City                                               Honda                                              1500        13
Baleno                                             Suzuki                                             1500        14
Vitz                                               Toyota                                             1300        15

SELECT name, company, power,
ROW_NUMBER() OVER(PARTITION BY company ORDER BY power DESC) AS RowRank
FROM Dizsweb_Cars_SQL_Server_Rank_dense_Rank_row_number


name                                               company                                            power       RowRank
-------------------------------------------------- -------------------------------------------------- ----------- --------------------
800                                                BMW                                                8000        1
110                                                Bugatti                                            8000        1
Mustang                                            Ford                                               5000        1
Accord                                             Honda                                              2000        1
Civic                                              Honda                                              1800        2
City                                               Honda                                              1500        3
C500                                               Mercedez                                           5000        1
C200                                               Mercedez                                           2000        2
208                                                Peugeot                                            5400        1
Baleno                                             Suzuki                                             1500        1
Prius                                              Toyota                                             3200        1
Landcruiser                                        Toyota                                             3000        2
Corrolla                                           Toyota                                             1800        3
Vitz                                               Toyota                                             1300        4
Atlas                                              Volkswagen                                         5000        1










Similarities between RANK, DENSE_RANK, and ROW_NUMBER Functions
The RANK, DENSE_RANK and ROW_NUMBER Functions have the following similarities:
1- All of them require an order by clause.
2- All of them return an increasing integer with a base value of 1.
3- When combined with a PARTITION BY clause, all of these functions reset the returned integer value to 1 as we have seen.
4- If there are no duplicated values in the column used by the ORDER BY clause, these functions return the same output.







Friday, August 7, 2020

Database Diagram Designing For Free Application for Various School

Database Diagram Designing For Free Application for Various School:-


This article will explain for SQL Developer and Database Architecture Designer to design the Database for School or Couching or Any academic center.

 To demonstrate this, I have created for tables
1- Student

Student
StudentIDNameUID
1Bill Gate1100110001
2SAM1100110002
3Jastin1100110004
4Bejos1100110015
5Rohit1100110016
6Sachin1100110020
7Virat1100110021
8Modi1100110023
9Trump1100110026
10Putin1100110030

 

2- Standard

Standard
StandardIDName
1I
2II
3III
4IV
5V
6VI
7VII
8VIII
9IX
10X
11XI
12XII

 

3- CENTER/ School/ Branch/ Location

CENTER
CenterIDCenter
1INDIA
2US
3CANADA
4RUSSIA
5UK

 

4-ANNUAL Charges

ANNUAL Charges
ACIDCenterIDStandardIDCHARGES($)
11I100
21II120
31III140
41IV160
51V180
61VI200
71VII220
81VIII240
91IX260
101X280
111XI300
121XII320
132I1000
142II1200
152III1400
162IV1600
172V1800
182VI2000
192VII2200
202VIII2400
212IX2600
222X2800
232XI3000
242XII3200
253I500
263II750
273III1000
283IV1250
293V1500
303VI1750
313VII2000
323VIII2250
333IX2500
343X2750
353XI3000
363XII3250
374I500
384II550
394III600
404IV650
414V700
424VI750
434VII800
444VIII850
454IX900
464X950
474XI1000
484XII1050
495I1100
505II1200
515III1300
525IV1400
535V1500
545VI1600
555VII1700
565VIII1800
575IX1900
585X2000
595XI2100
605XII2200

 

 Now to Design the Database Diagram for any Organization, First we need to check up to which level we need to create the flexibility. 

1- If all data is static then we should use the  one- to one- relationship

2- If we required full flexibility then we should use many-to-many relationship.



Translate