Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language 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 September 25th, 2005, 07:58 PM
Registered User
 
Join Date: Sep 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Subquery. Any help would be appreciated.

Dataset below

Fields Num Name
            1 A
            1 A
            1 A
            1 B
            2 C
            3 D
            3 D

I think this would be a subquery, but I only want to return records where the number has more than one name associated with it. Like in the dataset above, I would not care about Num2 and Num3, but would want to return Num1. The trick is I want to filter out the duplicates... so in this case the final return I would be looking for is:

Fields Num Name
            1 A
            1 B

Any takers?

rick
 
Old September 26th, 2005, 09:17 PM
Authorized User
 
Join Date: Sep 2005
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

select num,name
from table
group by num,name
having count(*) > 1

this should do it I think (it's getting late)
http://sqlservercode.blogspot.com/
 
Old September 29th, 2005, 08:58 AM
Registered User
 
Join Date: Sep 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to gemsuis Send a message via Yahoo to gemsuis
Default

select num,name
from table_name
group by num,name
having num = 1;

try this

 
Old October 3rd, 2005, 12:04 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You are correct that you need a subquery, if I understand your requirement correctly. I don't think that either of the prior responses correctly give what you are looking for.

This is going to get a bit ugly, so let's take it in steps.

First off, this would be a lot easier if we had a table where the duplicates were removed. So, lets just group your data to remove them:
Code:
    SELECT Num, Name
    FROM yourtable
    GROUP BY Num, Name
will give the following output:
Code:
    1    A
    1    B
    2    C
    3    D
Now, let's pretend we have a table with this data in it. I'm going to call this table 'grouptable'.

We want to group this table by Num, and find those groups with more than one row in them:
Code:
    SELECT Num
    FROM grouptable
    GROUP BY Num
    HAVING count(*)>1
will return:
Code:
    1
Now we need to select the rows in 'grouptable' where the Num value is in this last resultset, so we combine these two queries:
Code:
    SELECT Num, Name
    FROM grouptable
    WHERE Num IN 
        (SELECT Num
         FROM grouptable
         GROUP BY Num
         HAVING count(*)>1)
Of course, 'grouptable' doesn't actually exist in the database. But, we can materialize such a table at will. In fact that's what a SELECT statement does. This is called a 'derived table'. We can use a derived table anywhere we use a "real" one; the only requirement is that we must give it a name/alias.

So, now we just just substitute the definition of 'grouptable' in both places:
Code:
    SELECT Num, Name
    FROM 
        (SELECT Num, Name
         FROM yourtable
         GROUP BY Num, Name) as gt1
    WHERE Num IN 
        (SELECT Num
         FROM
            (SELECT Num, Name
             FROM yourtable
             GROUP BY Num, Name) as gt2
         GROUP BY Num
         HAVING count(*)>1)
yuck.

BTW, what is the primary key in the original table? You do have one, don't you? You're not going to get very far trying to maintain that table with those rows without one...

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Do i have to use a subquery, if yes then how? code_lover SQL Language 2 January 2nd, 2007 02:22 PM
please help it would be appreciated ELHEK J2EE 0 May 27th, 2006 04:46 AM
Cannot get it started - help appreciated tm BOOK: ASP.NET Website Programming Problem-Design-Solution 3 April 26th, 2006 01:40 PM
subquery khansa MySQL 1 February 21st, 2006 02:49 AM
MSXML2.ServerXMLHTTP help appreciated!! Steven1984 Classic ASP Professional 0 June 4th, 2005 12:49 PM





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