Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 20th, 2006, 09:07 PM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default count on duplicate value

greeting,

i have 1 question that need hints to proceed for the rest of my work.
i have a table: VENDOR. its column c/w vendorid, name...

PROBLEM: i need a query that can find the all duplicate name exist in the table since the vendorid is PK.

example:
vendorid name
001 ABC.com
002 ABC.com

my idea is used this condition :

if count(name) > 1 /* which NAME appear more than 1 times, duplicate */
then return the vendorid

anyone pls guide me how on it? just simply query that can extract the data from the table..

thank you
 
Old April 21st, 2006, 01:39 AM
Authorized User
 
Join Date: Mar 2006
Posts: 80
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
and which vendorid will be returned if you have e.g. three same names with different vendorid. The first, second, or third?
Peko

 
Old April 21st, 2006, 06:42 AM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

You can do something like this:

SELECT name
        FROM VENDOR
        GROUP BY name
        HAVING count(name) > 1

This will return all duplicate names.

 
Old April 21st, 2006, 12:53 PM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

1st and foremost, thanks for answering

ok, using group by and having clause did really can sort accordingly. but somehow drawback is it doesn't shown the related vendorid c/w the name.

seems easy in logic but somehow hard to apply over query

condition is i dunno how to apply search condition over WHERE clause

e.g: WHERE vendorid = ???
     group by name
     having count(name)>1

how can i find out any duplicate name, with diffrent vendorid. sequence is not important, as long as it show all the tuples that meet this requirement

any query that can return the vendorid that come along with its related duplicate name?

thank you

 
Old April 21st, 2006, 01:26 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

A CTE (Common Table Expression) is what you need. Such as:

WITH Test AS (SELECT name
        FROM Vendor
        GROUP BY name
        HAVING count(name) > 1)
SELECT Vendor.VendorID,
                Test_1.name
FROM Vendor
INNER JOIN Test AS Test_1 ON Test_1.name = dbo.Vendor.name

Hope this helps...

 
Old April 21st, 2006, 02:00 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

My apologies if you are not using SQL Server 2005. I posted the previous answer without even thinking about it. I will put together a SQL 2000 answer and repost.

Again, my apologies.

Scott

 
Old April 21st, 2006, 02:08 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

OK, here you go. For SQL Server 2000...

select VendorID, name from Vendor WHERE name in (SELECT name
        FROM Vendor
        GROUP BY name
        HAVING count(name ) > 1)

Scott


 
Old April 23rd, 2006, 08:16 PM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for all precious ideas

it do really work on what i want now...

thank you






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL: Count = Count + 1 elayaraja.s XSLT 3 July 18th, 2008 03:21 AM
Removing the Duplicate anukagni Access 3 August 31st, 2006 12:57 AM
is there any in built function to count page count g.tamilselvan MySQL 1 February 15th, 2006 07:43 AM
Count, sum, count a value, return records CongoGrey Access 1 April 18th, 2005 02:25 PM
Duplicate invoice k0023382 Access 4 April 7th, 2004 04:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.