A4 (B5 Platform) Discussion Discussion forum for the B5 Audi A4 produced from 1995-2001 B5 FAQ

OT Need some help with a SQL select statement

Thread Tools
 
Search this Thread
 
Old Feb 22, 2002 | 06:43 AM
  #1  
HDClown's Avatar
Thread Starter
AudiWorld Super User
 
Joined: Jul 2001
Posts: 14,487
Likes: 0
From: Orlando, FL
Default OT Need some help with a SQL select statement

This is for straight transact-SQL statement to be run in query analyzer against SQL Server 7.0

I have afield that holds unique #'s in a table. When you do a select on this field for a particular number, it should return ONLY 1 row all the time.

I've got duplicate numbers right now, so I need to find all the rows where these dupes occur, but I only want to return these rows. So how can i design my select statement so it will only show me all the duplicate instances rows?
Reply
Old Feb 22, 2002 | 06:50 AM
  #2  
D.Ross's Avatar
Senior Member
 
Joined: Jun 2000
Posts: 7,903
Likes: 0
Default just alias the table and compare it to itself:

select table1.*
from thetable table1, thetable table2
where table1.field = table2.field
;
Reply
Old Feb 22, 2002 | 06:53 AM
  #3  
rowdiboy's Avatar
Junior Member
 
Joined: May 2001
Posts: 1,121
Likes: 0
Default play with this

SELECT DISTINCTROW Table1.col1
Table1.col2
FROM table1
WHERE (((table1.Col1) In (SELECT [col1] FROM [Table1] As Tmp GROUP BY [col1] HAVING Count(*)>1 )))
ORDER BY tabl1.Col1;
Reply
Old Feb 22, 2002 | 07:01 AM
  #4  
D.Ross's Avatar
Senior Member
 
Joined: Jun 2000
Posts: 7,903
Likes: 0
Default

I have a feeling that would be WAAYYYY slower.
Reply
Old Feb 22, 2002 | 07:07 AM
  #5  
Matt H.'s Avatar
Elder Member
 
Joined: Apr 2001
Posts: 26,881
Likes: 0
Default in cases of dupe numbers...will all the data be identical??

if so, then do this:

SELECT DISTINCT * FROM tb_tablename

Then, take those query results and insert them into a new table....I'm not sure how to write that part of it.

If the records containing dupe ID's have different info in them (but the same ID)...then you're kinda screwed. there is no way to automate which record is valid, unless you have a date field that tells when the right or wrong record was put in, or something similar, that you could query, to weed out the wrong fields.
Reply
Old Feb 22, 2002 | 07:11 AM
  #6  
dingster's Avatar
Member
 
Joined: Mar 2000
Posts: 3,348
Likes: 0
Default you're doing a cartesian join of the table itself

you'll get way more results than desired
Reply
Old Feb 22, 2002 | 07:14 AM
  #7  
dingster's Avatar
Member
 
Joined: Mar 2000
Posts: 3,348
Likes: 0
Default Re: OT Need some help with a SQL select statement

find the dup keys and insert into a temp table. and then do a join to the temp table

select keyfield
into temptable
from table
group by keyfield
having count(*) > 1


select t.* from table t, temptable r
where t.keyfield = r.keyfield


drop table temptable
Reply
Old Feb 22, 2002 | 07:19 AM
  #8  
D.Ross's Avatar
Senior Member
 
Joined: Jun 2000
Posts: 7,903
Likes: 0
Default yes but it can still work in this case as long as there is another field to check against that

wouldn't be the same in for the dup. keys ie..
select table1.*
from thetable table1, thetable table2
where table1.field = table2.field AND table1.field2 <> table2.field2
;
Reply
AudiWorld Stories

Bringing Audi to Life for Audi Fans

story-0

10 Strangest Audi Designs That Actually Made Production

 Joe Kucinski
story-1

2027 Audi Q7 and SQ7: Audi Upgraded EVERYTHING!

 Michael S. Palmer
story-2

Audi Unveils Absurdly Cool New Supercar: 10 Things You Need to Know!

 Verdad Gallardo
story-3

The Highs & Lows of Every Audi C-Class Generation

 Joe Kucinski
story-4

Top 10 Most Expensive Audis Ever Sold on Bring-A-Trailer

 Brett Foote
story-5

10 Audi Features & Options We Miss the Most!

 Joe Kucinski
story-6

Audi Recreates Crazy-Looking Speed Record Breaker From 1935

 Verdad Gallardo
story-7

Coachbuilder Recreates the 1995 Audi TTS Concept

 Verdad Gallardo
story-8

Every Audi V10 Car Ranked!

 Joe Kucinski
story-9

9 Audi Designs That Aged Like Fine Wine

 Verdad Gallardo
Old Feb 22, 2002 | 07:21 AM
  #9  
SDSilver01's Avatar
Junior Member
 
Joined: Oct 2000
Posts: 1,504
Likes: 0
Default Not really. And using the HAVING and GROUP BY clauses actually works. The table aliasing doesn't.

It will return you all the records that are duplicated only.
Reply
Old Feb 22, 2002 | 07:23 AM
  #10  
dingster's Avatar
Member
 
Joined: Mar 2000
Posts: 3,348
Likes: 0
Default You're assuming there's another field where every record is unique

if not then it won't work. In his case I don't think he knows that.
Reply



All times are GMT -8. The time now is 02:31 AM.

story-0
10 Strangest Audi Designs That Actually Made Production

Slideshow: 10 strangest Audi designs that actually made production

By Joe Kucinski | 2026-06-10 16:32:29


VIEW MORE
story-1
2027 Audi Q7 and SQ7: Audi Upgraded EVERYTHING!

Slideshow: Everything you need to know about the 2027 Audi Q7 and SQ7

By Michael S. Palmer | 2026-06-09 06:02:56


VIEW MORE
story-2
Audi Unveils Absurdly Cool New Supercar: 10 Things You Need to Know!

Slideshow: Limited to just 499 units, the 987-horsepower halo car signals a new chapter for Audi performance.

By Verdad Gallardo | 2026-06-04 17:37:15


VIEW MORE
story-3
The Highs & Lows of Every Audi C-Class Generation

Slideshow: The highs and lows of every Audi C-Class generation.

By Joe Kucinski | 2026-05-27 16:05:50


VIEW MORE
story-4
Top 10 Most Expensive Audis Ever Sold on Bring-A-Trailer

People were more than happy to shell out big bucks for these cars.

By Brett Foote | 2026-05-27 15:32:23


VIEW MORE
story-5
10 Audi Features & Options We Miss the Most!

Slideshow: 10 Audi features and options we miss the most.

By Joe Kucinski | 2026-05-12 19:33:47


VIEW MORE
story-6
Audi Recreates Crazy-Looking Speed Record Breaker From 1935

Slideshow: Audi has recreated one of the wildest machines of the pre-war speed-record era, reviving a streamlined V16 racer that originally exceeded 200 mph in 1935.

By Verdad Gallardo | 2026-05-11 09:49:34


VIEW MORE
story-7
Coachbuilder Recreates the 1995 Audi TTS Concept

Slideshow: A Dutch coachbuilder has reimagined the original Audi TT by finishing what the 1995 concept only hinted at.

By Verdad Gallardo | 2026-05-05 15:17:58


VIEW MORE
story-8
Every Audi V10 Car Ranked!

Slideshow: Ranking every Audi V10 road car

By Joe Kucinski | 2026-04-29 16:11:56


VIEW MORE
story-9
9 Audi Designs That Aged Like Fine Wine

Slideshow: A look back at the Audis that didn't just survive changing tastes, they quietly outgrew them.

By Verdad Gallardo | 2026-04-28 19:38:27


VIEW MORE