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 May 29th, 2009, 04:07 AM
wicd-pixel wicd-pixel is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 13 wicd-pixel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 57 sec
Reputation Power: 0
VBScript - Database - General - Finding and displaying duplicates

hi,

I'm trying to write a bit of SQL to find and display the duplicates in my DB.

Let's say I have a list of books that have been read by a number of people

so, I have a table called Books_read

Book_Name ------ Reader_name
book 1------------- Jim
book 2------------- Jim
book 3------------- Fred
book 4------------- Bob
book 1------------- bob

the query below (I was hoping) would give me

Book 1 -------- Jim
book 1 -------- bob

so thereby identifying the book that has been read by more than one person, and the names of the readers

strSQL = "select * from books_read where book_name in (select book_name from books_read group by book_name having count(distinct reader_name) >1) order by book_name"

but, this gives me a syntax error.

can anyone help?

thanks

Reply With Quote
  #2  
Old May 29th, 2009, 04:26 AM
sync_or_swim's Avatar
sync_or_swim sync_or_swim is offline
Moderator
Click here for more information.
 
Join Date: Mar 2006
Location: South Wales
Posts: 3,461 sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Day 16 h 50 m 26 sec
Reputation Power: 1806
--> Moved to SQL forum

I think this should work with extra brackets:
Code:
strSQL = "SELECT book_name, reader_name FROM books_read " & _
"WHERE (book_name IN " & _
"(SELECT book_name FROM books_read GROUP BY book_name " & _
"HAVING (COUNT(reader_name) >1))) " & _
"ORDER BY book_name"

Reply With Quote
  #3  
Old May 29th, 2009, 05:30 AM
wicd-pixel wicd-pixel is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 13 wicd-pixel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 57 sec
Reputation Power: 0
Perfect. Thank you very much. You wont believe how many different variations of code I've tried (even copying the data to a second table and doing compares!!)

Reply With Quote
  #4  
Old May 29th, 2009, 05:37 AM
sync_or_swim's Avatar
sync_or_swim sync_or_swim is offline
Moderator
Click here for more information.
 
Join Date: Mar 2006
Location: South Wales
Posts: 3,461 sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Day 16 h 50 m 26 sec
Reputation Power: 1806
Quote:
Originally Posted by wicd-pixel
Perfect. Thank you very much. You wont believe how many different variations of code I've tried (even copying the data to a second table and doing compares!!)

You're welcome, glad it helped!!

Reply With Quote
  #5  
Old May 29th, 2009, 06:40 AM
wicd-pixel wicd-pixel is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 13 wicd-pixel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 57 sec
Reputation Power: 0
I think I'm also trying to do stuff which is too complicated for me which is why I'm struggling.

in my explanation above I simplified the DB structure to show reader_name. Because I spent some time normalising my DB, the reader_name is actually a number which references a second table

so, I've actually got

Book_Name ------ Reader_name
book 1------------- 1
book 2------------- 1
book 3------------- 2
book 4------------- 3
book 1------------- 3

where the number 1, corresponds to the user table record which has Jim as the name. Therefore if Jim ever changes his name, all corresponding read books will reference correctly, (a tip for those who are just starting out with DB design - if Jim changed his name and I had my original DB structure, I would have to change his name in more than one place).

anyway the result of the query you supplied (very grateful) gives me

book 1 - 1
book 1 - 3

Ideally, I could join to the user_table to convert the '1' to 'Jim'

so..that gives me something like

strSQL = "SELECT book_name, reader_record FROM books_read " & _
"WHERE (book_record IN " & _
"(SELECT book_name FROM books_read GROUP BY book_name " & _
"HAVING (COUNT(reader_record) >1))) " & _
"ORDER BY book_name" & _
"INNER JOIN books_read ON USERS.record_id = books_read.reader_record "


which, I was hoping would give me

book 1 - Jim
book 1 - Bob

because the recod IDs 1 and 3 would be replaced with the user name from the second table. I'm guessing this is possible but I don't have a clue.

At the moment I'm using a second query after the first.

so I'm running the first query, and then running a second query based on the results of the first, and within the loop. Which works, but it's very infficient I'm guessing

i.e.
1st Query
select something from somewhere
while not rs.eof
something = rs("something")
2nd query based on the results of the first
select something_else from somewhere_else where something = x
loop back to first query

Reply With Quote
  #6  
Old May 29th, 2009, 06:59 AM
sync_or_swim's Avatar
sync_or_swim sync_or_swim is offline
Moderator
Click here for more information.
 
Join Date: Mar 2006
Location: South Wales
Posts: 3,461 sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Day 16 h 50 m 26 sec
Reputation Power: 1806
Hi, you should be able to adapt the query to join to your table of names - I have called it readers and I assume that it has two columms reader_id and reader_name:
Code:
strSQL = "SELECT B.book_name, R.reader_name FROM books_read B LEFT JOIN readers R ON B.reader_id = R.reader_id " & _
"WHERE (B.book_name IN " & _
"(SELECT book_name FROM books_read GROUP BY book_name " & _
"HAVING (COUNT(reader_record) >1))) " & _
"ORDER BY book_name"

Reply With Quote
  #7  
Old May 29th, 2009, 07:10 AM
wicd-pixel wicd-pixel is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 13 wicd-pixel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 57 sec
Reputation Power: 0
What I wouldn't give to have your skills!!!

genius, thanks.

Reply With Quote
  #8  
Old May 29th, 2009, 08:11 AM
sync_or_swim's Avatar
sync_or_swim sync_or_swim is offline
Moderator
Click here for more information.
 
Join Date: Mar 2006
Location: South Wales
Posts: 3,461 sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Day 16 h 50 m 26 sec
Reputation Power: 1806
Quote:
Originally Posted by wicd-pixel
What I wouldn't give to have your skills!!!

genius, thanks.

You're welcome, glad to help!!

Reply With Quote
  #9  
Old October 21st, 2009, 01:26 AM
sakthi.tnj sakthi.tnj is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 23 sakthi.tnj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 48 m 35 sec
Reputation Power: 0
Re: Finding & displaying duplicates

Hi, I hope that the below query will be useful for you

Quote:

Select book_name, Reader_name from test2 where book_name in(select book_name from
(Select book_name, Count(book_name)co from test2 group by book_name)tbl where tbl.co>1)



Note: test2 is a table name

Thanks & Regards
Sakthimeenakshi.S

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > VBScript - Database - General - Finding and displaying duplicates


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





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

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





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek