C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » SQL Server »

GET Distinct Rows from a table


Posted Date: 24 Dec 2008      Posted By: shanthy      Member Level: Silver     Points: 1   Responses: 6



Hi,
I want to display the id which is pk, distinct country id and country name from a table.
but if i display the id of the table it does not fetch the distinct country id it displays all the rows of the table.

Select distinct(countryID),ID,CountryName from tbl_Country.
It displays as
ID CountryID Name
1 11 India
2 12 US
3 11 INDIA
This returns all the rows in the table. but i need only the rows which are distinct





Responses

Author: Tejinder Singh Barnala    24 Dec 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 3

shanthy, you are going in wrong direction

you are saying that you have a table

pkid country
1 india
2 india
3 newzeland


and you want result
1, india
3, newzeland


if i am right than

you need not to use distinct, u have to use group by

like

select min(ID), country from table
group by country

or

select max(ID), country from table
group by country

hope it will help u

Many Thanks
Tejinder Singh Barnala
/*I have the simplest tastes. I am always satisfied with the best*/



Author: Durga Prasad    24 Dec 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 1

Select DISTINCT countryID ,ID,CountryName from tbl_Country


Author: Babu Akkandi    24 Dec 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

Hi shanthy,

Use this Query,



SELECT distinct
[ID],
country_id,
country_name
FROM
Table1
WHERE
[ID] NOT IN
(SELECT MAX([ID]) FROM Table1 AS P WHERE Table1.country_id= P.country_id)



Use Min or Max, whatever you want, in sub Query

Hope it helps

Thanks and Regards,
Babu Akkandi
Microsoft Technology



Author: Sriman N Vangala    24 Dec 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 5

Here the table itself not properly designed.

In the Table tbl_Country, You have three columns
Id,countryID,CountryName where id is primary key. As you have the Id as primary key there is no need of countryID again as a separate column. Id itself can be treated as the countryID And as the tbl_Country table is the master table, it won't contain the duplicate values.
hence first change the design of the table tbl_Country as
tbl_Country(ID,name,...)
or
tbl_Country(CountryID,name,...)

then you can have the values as below:

CountryID Name
1 India
2 US
3 China

Now you can have only the distinct country values in the table and you can get it using the query

Select countryID,Name from tbl_Country

and there is no need of using Distince keyword in this case



Author: Pradeep M    24 Dec 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 1

Below code displays the results with distinct values in each row

Select distinct id, countryID,Name from tbl_Country



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Date comparision
Previous : Let any one explain the given query clearly...
Return to Discussion Forum
Post New Message
Category: SQL Server

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use