473,386 Members | 1,801 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,386 software developers and data experts.

SELECT DISTINCT Problem

bergy
89
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 duplicates and then there is a unique primary key.

The table looks like this:

Expand|Select|Wrap|Line Numbers
  1. id    color
  2. ---   ------
  3.  3     red
  4.  4     red
  5.  5     blue
  6.  6     green
  7.  7     red
  8.  8     blue
  9.  9     green
I'm trying to eliminate the duplicates and tie the color to one id. This ties in with some software so I was planning on doing it in 2 steps. First, return some results with no duplicate color and their corresponding id. Then run a quick delete statement - something like "DELETE FROM table WHERE id NOT 3 AND id NOT 5 AND id NOT 6".

I tried playing around with SELECT DISTINCT but I can't seem to get the id to return with the distinct results. Examples:

SELECT DISTINCT color FROM table
Returns:
Expand|Select|Wrap|Line Numbers
  1. color
  2. ------
  3.  red
  4.  blue
  5.  green
With no ID field

SELECT DISTINCT color, id FROM table
Returns my original result (since all ids are distinct)

and I'm looking for a way to arrive at this:

Expand|Select|Wrap|Line Numbers
  1. id    color
  2. ---   ------
  3.  3     red
  4.  5     blue
  5.  6     green
  6.  
Any help is appreciated - using SQL Server 2000 and MSDE as testing environment.
Mar 6 '07 #1
2 3060
Yes you can get your solution

Expand|Select|Wrap|Line Numbers
  1.  Select max(id) as id ,   color  from tablename group by Color
  2.  
Mar 6 '07 #2
bergy
89
Thanks a bunch mabuakarpk! I've never used min or max before, I'll have to look them up and see what else they can be used for. You are a life saver!
Mar 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Martin Feuersteiner | last post by:
Dear Group I'm having trouble with the clause below. I would like to select only records with a distinct TransactionDate but somehow it still lists duplicates. I need to select the...
2
by: mfyahya | last post by:
I have two tables, both containing an 'authors' column. Is there a way to get a unique list of authors from the two tables? I tried SELECT DISTINCT `authors` from `table1`, `table2`; but I got an...
9
by: Kelvin | last post by:
Okay so this is baking my noodle. I want to select all the attritbutes/fields from a table but then to excluded any row in which a single attributes data has been duplicated. I.E. Here's my...
6
by: John M | last post by:
Hi, The line below is used to feed a combobox. (It is from a database which is used to log pupil behaviour!) The 'incidents' table contains a list of students who have been involved in incidents....
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...
2
by: ray well | last post by:
i have to extract info from a legacy access database, which i can't alter, or run APPEND or UPDATE quries against. i can only use SELECT statments to extract what i need. the database has...
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 ...
2
by: johnhanis | last post by:
I'm using a Visual Basic front end with an SQL query to select some data from a MS Access database. I have a table named Tithes with Columns of TitheDate Tither No Total Tithes Faith Promise...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
5
by: movieking81 | last post by:
Hello All, I'm building an inventory database for my company and I need to setup some specific select statements to write the material to a web page. There are duplicate descriptions in the...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.