dbTalk Databases Forums  

Select distinct rows from duplicate rows....

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Select distinct rows from duplicate rows.... in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
sanjeev.atvankar@gmail.com
 
Posts: n/a

Default Select distinct rows from duplicate rows.... - 11-28-2007 , 07:26 AM






Dear Gurus,

I have table with following entries

Table name = Customer

Name Weight
------------ -----------
Sanjeev 85
Sanjeev 75
Rajeev 80
Rajeev 45
Sandy 35
Sandy 30
Harry 15
Harry 45

I need a output as follow

Name Weight
------------ -----------
Sanjeev 85
Rajeev 80
Sandy 30
Harry 45

OR

Name Weight
------------ -----------
Sanjeev 75
Rajeev 45
Sandy 35
Harry 15

i.e. only distinct Name should display with only one value of Weight.
I tried with 'group by' on Name column but it shows me all rows.

Could anyone help me for above.

Thanking in Advance.

Regards
Sanjeev
sanjeev.atvankar (AT) gmail (DOT) com

Reply With Quote
  #2  
Old   
Jason Lepack
 
Posts: n/a

Default Re: Select distinct rows from duplicate rows.... - 11-28-2007 , 07:45 AM






select
[Name],
max([Weight]) as wgt
from
[Customer]
group by
[Name]

Cheers,
Jason Lepack

On Nov 28, 8:26 am, sanjeev.atvan... (AT) gmail (DOT) com wrote:
Quote:
Dear Gurus,

I have table with following entries

Table name = Customer

Name Weight
------------ -----------
Sanjeev 85
Sanjeev 75
Rajeev 80
Rajeev 45
Sandy 35
Sandy 30
Harry 15
Harry 45

I need a output as follow

Name Weight
------------ -----------
Sanjeev 85
Rajeev 80
Sandy 30
Harry 45

OR

Name Weight
------------ -----------
Sanjeev 75
Rajeev 45
Sandy 35
Harry 15

i.e. only distinct Name should display with only one value of Weight.
I tried with 'group by' on Name column but it shows me all rows.

Could anyone help me for above.

Thanking in Advance.

Regards
Sanjeev
sanjeev.atvan... (AT) gmail (DOT) com


Reply With Quote
  #3  
Old   
Jason Lepack
 
Posts: n/a

Default Re: Select distinct rows from duplicate rows.... - 11-28-2007 , 07:47 AM



Please note that because you don't care which weight you get, max()
can be replaced with min(), first(), last(), etc.

Cheers,
Jason Lepack

On Nov 28, 8:26 am, sanjeev.atvan... (AT) gmail (DOT) com wrote:
Quote:
Dear Gurus,

I have table with following entries

Table name = Customer

Name Weight
------------ -----------
Sanjeev 85
Sanjeev 75
Rajeev 80
Rajeev 45
Sandy 35
Sandy 30
Harry 15
Harry 45

I need a output as follow

Name Weight
------------ -----------
Sanjeev 85
Rajeev 80
Sandy 30
Harry 45

OR

Name Weight
------------ -----------
Sanjeev 75
Rajeev 45
Sandy 35
Harry 15

i.e. only distinct Name should display with only one value of Weight.
I tried with 'group by' on Name column but it shows me all rows.

Could anyone help me for above.

Thanking in Advance.

Regards
Sanjeev
sanjeev.atvan... (AT) gmail (DOT) com


Reply With Quote
  #4  
Old   
SQL Menace
 
Posts: n/a

Default Re: Select distinct rows from duplicate rows.... - 11-28-2007 , 07:50 AM



SELECT Name, MAX(Weight) AS Weight
FROM Customer
GROUP BY Name

or

SELECT Name, MIN(Weight) AS Weight
FROM Customer
GROUP BY Name

Denis The SQL Menace
http://sqlservercode.blogspot.com
http://sqlblog.com/blogs/denis_gobo/default.aspx



On Nov 28, 8:26 am, sanjeev.atvan... (AT) gmail (DOT) com wrote:
Quote:
Dear Gurus,

I have table with following entries

Table name = Customer

Name Weight
------------ -----------
Sanjeev 85
Sanjeev 75
Rajeev 80
Rajeev 45
Sandy 35
Sandy 30
Harry 15
Harry 45

I need a output as follow

Name Weight
------------ -----------
Sanjeev 85
Rajeev 80
Sandy 30
Harry 45

OR

Name Weight
------------ -----------
Sanjeev 75
Rajeev 45
Sandy 35
Harry 15

i.e. only distinct Name should display with only one value of Weight.
I tried with 'group by' on Name column but it shows me all rows.

Could anyone help me for above.

Thanking in Advance.

Regards
Sanjeev
sanjeev.atvan... (AT) gmail (DOT) com


Reply With Quote
  #5  
Old   
Madhivanan
 
Posts: n/a

Default Re: Select distinct rows from duplicate rows.... - 11-28-2007 , 08:27 AM



On Nov 28, 6:47 pm, Jason Lepack <jlep... (AT) gmail (DOT) com> wrote:
Quote:
Please note that because you don't care which weight you get, max()
can be replaced with min(), first(), last(), etc.

Cheers,
Jason Lepack

On Nov 28, 8:26 am, sanjeev.atvan... (AT) gmail (DOT) com wrote:



Dear Gurus,

I have table with following entries

Table name = Customer

Name Weight
------------ -----------
Sanjeev 85
Sanjeev 75
Rajeev 80
Rajeev 45
Sandy 35
Sandy 30
Harry 15
Harry 45

I need a output as follow

Name Weight
------------ -----------
Sanjeev 85
Rajeev 80
Sandy 30
Harry 45

OR

Name Weight
------------ -----------
Sanjeev 75
Rajeev 45
Sandy 35
Harry 15

i.e. only distinct Name should display with only one value of Weight.
I tried with 'group by' on Name column but it shows me all rows.

Could anyone help me for above.

Thanking in Advance.

Regards
Sanjeev
sanjeev.atvan... (AT) gmail (DOT) com- Hide quoted text -

- Show quoted text -
Note that SQL Server doesnt support first() or last() function


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.