473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Select distinct rows from duplicate rows....

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
sa**************@gmail.com
Nov 28 '07 #1
4 12974
select
[Name],
max([Weight]) as wgt
from
[Customer]
group by
[Name]

Cheers,
Jason Lepack

On Nov 28, 8:26 am, sanjeev.atvan...@gmail.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...@gmail.com
Nov 28 '07 #2
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...@gmail.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...@gmail.com
Nov 28 '07 #3
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...@gmail.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...@gmail.com
Nov 28 '07 #4
On Nov 28, 6:47 pm, Jason Lepack <jlep...@gmail.comwrote:
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...@gmail.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...@gmail.com- Hide quoted text -

- Show quoted text -
Note that SQL Server doesnt support first() or last() function
Nov 28 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Colman | last post by:
Howdy all! I guess I'm a newbie, because I am stumped (or maybe just too durned tired). Here's what I got... CREATE TABLE `nodecat_map` ( `nodecat_id` mediumint(8) unsigned NOT NULL...
1
by: leegold2 | last post by:
say I'm selecting distinct (non-duplicate) rows for insertion, insert into original_table select distinct * from new_table these tables have 3 fields/row. Per the above code all 3 fields are...
5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
5
by: harry | last post by:
Hi, Can the DataTable.Select Method use 'Distinct' to remove duplicate rows? If so how? I tried, however code triggered error 'no colmn 'Distinct' found'. Thanks Harry
18
by: mathilda | last post by:
My boss has been adamant that SELECT DISTINCT is a faster query than SELECT all other factors being equal. I disagree. We are linking an Access front end to a SQL Server back end and normally are...
6
by: Bob Stearns | last post by:
I am getting duplicate rows back from a select distinct statement of the form: SELECT DISTINCT 'jhough', '000111', t0.bhid FROM (SELECT lots of good stuff) t0 LEFT OUTER JOIN another_table ...
6
by: Bob Stearns | last post by:
I am getting unwanted duplicate rows in my result set, so I added the DISTINCT keyword to my outermost SELECT. My working query then returned the following message: DB2 SQL error: SQLCODE: -214,...
6
by: pooh80133 | last post by:
Hi! I am pasting my SQL code at the end of this message. I am trying to use SELECT DISTINCT in a query, but I am a beginner for using Access. Right now I have duplicate ID's (Indiv ID) in my...
2
bergy
by: bergy | last post by:
Hello MS SQL experts, I'm trying to reorganize some data for a friend and I'm running into this problem. Currently he has some duplicate rows that I need to get rid of - only one of the columns has...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.