?
Solved

T-SQL Query(Microsoft SQL Server)

Posted on 2007-08-07
4
Medium Priority
?
264 Views
Last Modified: 2008-01-09
I have a SQL table

ID Value
__ _____
1   A
2   B
3   B
4   B
5   C
6   C
7   D

I want to remove the repeating values from the table, such that there are only distinct values in the table.
hence the values 3B, 4B & 6C should be deleted.

After the execution of the SQL query, The final output should be:

ID  value
__   ____
1    A
2    B
5    C
7    D

How can I accomplish this? Please help.
Thanks in advance,
0
Comment
Question by:vad2319
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 19648520
select value, min(id) from yourtable group by value
0
 
LVL 42

Expert Comment

by:dqmq
ID: 19648637
I'm guessing you've simplified the question and what you really want is:

Select * from YourTable T1
inner join
(Select Min(ID) from YourTable group by value) T2 on T1.ID = T2.ID
0
 
LVL 22

Expert Comment

by:dportas
ID: 19648697
If you want to DELETE rather than just query then try:

DELETE FROM tbl
WHERE EXISTS
 (SELECT *
  FROM tbl AS t
  WHERE Value = tbl.Value
   AND id < tbl.id);

This assumes ID is unique and is untested - test it out and take a backup before you try it for real.
0
 

Author Comment

by:vad2319
ID: 19651364
Thanks guys for your help.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question