SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old April 7th, 2004, 04:17 PM
forrest321 forrest321 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 12 forrest321 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Select Distinct...kind of

I have a table that is generated daily by one of our servers. The table has about 15 fields, one of which is called TicketNumber, which should be unique for each row. The problem is that every now and then a row is duplicated in the table. I need to be able to select all of the rows, minus duplicates...meaning any duplicated rows should show up, but only one time.

I found the following with a google search:

select *
from edata
where TicketNumber
in ( select TicketNumber
from edata
group
by TicketNumber
having count(*) = 1 )

That would show all fields from all distinct rows, however I need to also have the rows that are duplicated show up one time. I hope that makes sense.

Reply With Quote
  #2  
Old April 7th, 2004, 04:34 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
to determine the duplicates you can do something like this
Code:
SELECT TicketNumber
FROM edata
GROUP BY TicketNumber
HAVING COUNT(TicketNumber) > 1

Reply With Quote
  #3  
Old April 8th, 2004, 09:34 AM
forrest321 forrest321 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 12 forrest321 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Do you know how I could combine all the non-dupes along with one occurrence of each of the duplicated rows in the same recordset?

Reply With Quote
  #4  
Old April 8th, 2004, 09:43 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
First I would remove all of the duplicates.
But to do what you are asking just do a select distinct query.
Code:
SELECT DISTINCT TicketNumber
FROM edata

Reply With Quote
  #5  
Old April 8th, 2004, 03:48 PM
forrest321 forrest321 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 12 forrest321 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
By removing all of the duplicates, I would lose a row of important data, so I would need to either remove all but one row of each duplicated record and then just SELECT * from edata, or I would need to somehow get only one occurrence of each row - duplicated or not - in a recordset.

The problem with SELECT DISTINCT TicketNumber FROM edata is that I need to have all columns in the recordset. Also, would that return one instance of a duplicated row, or would it only return the rows that were not duplicated?

I hope this is making sense...thanks for your help btw.

Reply With Quote
  #6  
Old April 8th, 2004, 05:41 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
if each row contains important data (should be unique), then I don't understand why you are allowing duplicates?

Reply With Quote
  #7  
Old April 8th, 2004, 05:42 PM
sbaxter sbaxter is offline
Moderator: Access, SQL
ASP Free God (5000 - 5499 posts)
 
Join Date: Oct 2003
Posts: 5,126 sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 1 h 2 m 51 sec
Reputation Power: 13
"would that return one instance of a duplicated row, or would it only return the rows that were not duplicated"

It would return instance of a duplicated row


You also stated "By removing all of the duplicates, I would lose a row of important data"

If there is data that is going to be lost then it must not truly be duplicate data. If all data in every field was the same and you removed one, whats lost?

S-

Reply With Quote
  #8  
Old April 8th, 2004, 05:58 PM
forrest321 forrest321 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 12 forrest321 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry, I must have misunderstood. I was thinking that removing duplicates would remove the original row as well as the dupe. My mistake.

Say I have the following in a table:

#-name-ticketnumber
---------------------
1-joe-1234
1-joe-1234
2-bob-4321

I would like to get the following recordset:
1-joe-1234
2-bob-4321

i.e., I need distinct rows, but I need to return all of the fields/columns. The table that is getting all the dupes in it to begin with is generated by a proprietary bit of code that I can not change, so preventing the dupes from occuring in the first place is unfortunately not a possibility.

Again, thanks for your help and patience.

Reply With Quote
  #9  
Old April 8th, 2004, 06:33 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
Code:
SELECT DISTINCT Id, pName, ticketNumber
FROM TableName

Reply With Quote
  #10  
Old April 13th, 2004, 10:26 AM
forrest321 forrest321 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 12 forrest321 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up Thats it

I can't believe I was making that so difficult. Thanks for all your help.

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Select Distinct...kind of


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT