Wednesday, January 17, 2018

How To Use Select Distinct Statement In Mssql

Hello Friend Today In This Blog I Will Show You How Use Select Distinct Statement In Mssql.The SELECT DISTINCT statement is used to return only distinct (different) values.Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.The SELECT DISTINCT statement is used to return only distinct (different) values.SELECT DISTINCT returns only distinct (different) values.SELECT DISTINCT eliminates duplicate records from the results.DISTINCT can be used with aggregates: COUNT, AVG, MAX, etc.DISTINCT operates on a single column. DISTINCT for multiple columns is not supported.The SQL Server (Transact-SQL) DISTINCT clause is used to remove duplicates from the result set. The DISTINCT clause can only be used with SELECT statements.This SQL Server tutorial explains how to use the DISTINCT clause in SQL Server (Transact-SQL) with syntax and examples.Many people resort to using the DISTINCT keyword in a SELECT statement to remove duplicates returned by a query. The fact that the resultset has duplicates is frequently (though not always) the result of a poor database design, an ineffective query, or both. In any case, issuing the query without the DISTINCT keyword yields more rows than expected or needed so the keyword is employed to limit what is returned to the user.In SQL multiple fields may also be added with DISTINCT clause. DISTINCT will eliminate those rows where all the selected fields are identical.This article is inspired by a series of questions that one of my readers, Nan, recently sent me regarding DISTINCT, TOP, and ORDER BY.All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can get started using these free tools using my Guide Getting Started Using SQL Server.

Learn how to use the DISTINCT keyword to eliminate duplicate rows in this excerpt from "SQL Queries for Mere Mortals: A Hands-On Guide to Data Manipulation in SQL, Second Edition." You'll learn how to use DISTINCT on either single or multiple columns when working with SELECT statements in SQL.SQL Server gives you the ability to store mixed case data in your databases, but depending on how you create your databases SQL Server will ignore the case when you issue T-SQL commands.To illustrate this behavior we are going to look at a couple ways this works using a case sensitive database and a case insensitive database.DISTINCT clause is used to get unique set of records from the SELECT statement.


SELECT DISTINCT Syntax

SELECT DISTINCT column1, column2, ...

FROM table_name;


Step 1 :Create database

Create database BMC





Step 2:Create table

Here In This step We Will Create table.


Here In This Step We Required Four Column With Name Cids ,Cname ,Ccity And CPrice using Parameter bigint,varchar and float.

create table Client_table_news
(
Cids bigint,
Cname varchar(50),
Ccity varchar(200),
CPrice float
);


Step 3 :Create Insert Trigger Functionality

Here In This Step We Will Insert Data Using Insert Command.
Data Will Be Added Manually Using Insert Command.


Insert Into Client_table_news values(1,'VIRAJ','MUMBAI',15000.00)
Insert Into Client_table_news values(2,'RAJ','DELHI',10000.00)
Insert Into Client_table_news values(3,'ANIL','PUNE',12000.00)
Insert Into Client_table_news values(4,'SUNIL','MUMBAI',9000.00)
Insert Into Client_table_news values(5,'SHERYAS','JAIPUR',11000.00)
Insert Into Client_table_news values(6,'AJAY','MUMBAI',16000.00)

select *from Client_table_news


Step 4 :Create Select Distinct Statement

Here In This Step  We Will Use Select Distinct Statement.
Select Distinct Statement By Passing Parameter Ccity Using Table Name Client_table_news.

SELECT DISTINCT Ccity  FROM Client_table_news;



SELECT COUNT(DISTINCT Ccity)as Ccity  FROM Client_table_news;




2 comments: