Wednesday, 18 July 2018

SQL Server DISTINCT


DISTINCT return only distinct value. If any column content similar data and DISTINCT is applied on it,  the column value may or may not be repetitive, they comes only once in the output. Duplicate value is discarded , if it is already in the list. 

District can be applied almost type of data. District can be applied on various numeric functions like average,sum,count etc.

We have created a table and corresponding data to do some sample example. 
CREATE TABLE student
(
    id INT NOT NULL,
    name VARCHAR (500) NOT NULL,
    age INT NOT NULL,
    adress VARCHAR (500) NOT NULL,
    date_of_birth DATETIME NOT NULL,
   height DECIMAL(10,2) NOT NULL
)


INSERT INTO student(id,name,age,adress,date_of_birth,height)
SELECT 1,'John1',12,'34,student street','01/08/2018',10.5
UNION
SELECT 1,'John2',13,'35,student street','02/08/2018',11
UNION
SELECT 1,'John3',12,'36,student street','03/08/2018',11.5
UNION
SELECT 1,'John4',13,'37,student street','04/08/2018',12
UNION
SELECT 1,'John5',12,'34,student street','05/08/2018',12.5
UNION
SELECT 1,'John6',13,'35,student street','06/08/2018',13.5
UNION
SELECT 1,'John7',15,'36,student street','07/08/2018',14
UNION
SELECT 1,'John8',14,'34,student street','08/08/2018',14.5
UNION
SELECT 1,'John9',12,'35,student street','09/08/2018',15
UNION
SELECT 1,'John10',15,'36,student street','10/08/2018',15.5
UNION
SELECT 1,'John11',16,'34,student street','11/08/2018',16

 
Here  DISTINCT  is applied on numeric values

Example 1

SELECT DISTINCT age FROM student

Output
age
12
13
14
15
16




Here  DISTINCT  is applied on string values


Example 2
SELECT DISTINCT adress FROM student

Output
adress
34,student street
35,student street
36,student street
37,student street



Here  DISTINCT  is applied on Average values


Example 3
SELECT AVG(DISTINCT age) as Avarage FROM student

Output
Avarage 
14


Here  DISTINCT  is applied on Count values

Example 4 
SELECT COUNT(DISTINCT adress) as Adress_Count FROM student

Output
Adress_Count  
4

 

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...