Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old August 23rd, 2004, 12:51 PM
Registered User
Join Date: Aug 2004
Location: Livonia, MI, USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

I am relatively new to SQL so I don't know a lot of things yet so I am hoping someone can help me. I am trying to insert into one table from another table a certain number of records but the inserted records have limitations. For example:

FROM Table2
WHERE EXISTS (SELECT field1, field2, count(*)
    from Table2
    group by field1, field2
    having count(*) > 10)

So the records have to have a count of more then 10 by field1 then field2. Does that even make sense, if so why won't it work??? I keep getting records that have a count of less then 10.

  #2 (permalink)  
Old August 23rd, 2004, 02:41 PM
Friend of Wrox
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post

You're misunderstanding the EXISTS predicate.

Your subquery:

SELECT field1, field2, count(*)
    from Table2
    group by field1, field2
    having count(*) > 10

will return a resultset listing all the field1, field2 combinations (and their count) which have more than 10 rows in your table. If you run it all by itself, you should see a listing of all the rows which have a count greater than 10.

Presumably there is at least one row in this resultset.

The EXISTS predicate returns true if the subquery as its argument has any rows at all in it. In your case it does, so the predicate is true for every row in the main SELECT query. You are selecting the TOP 100 rows of this, which presumably has at least one set of rows with a count less than 10, which is what you are seeing.

Usually, you use a correlated subquery as the argument of the EXISTS clause.

Something like:

FROM Table2 T0
WHERE EXISTS (SELECT T1.field1, T1.field2, count(*)
    from Table2 T1
    where T0.field1 = T1.field1 AND T0.field2 = T1.field2
    group by T1.field1, T1.field2
    having count(*) > 10)

Jeff Mason
Custom Apps, Inc.

Similar Threads
Thread Thread Starter Forum Replies Last Post
SELECT TOP n rgerald SQL Server 2000 3 May 12th, 2006 04:03 PM
select top problem keyvanjan SQL Server 2000 5 January 7th, 2006 07:11 AM
SELECT TOP n NOT SELECTING TOP n! ibi SQL Language 8 March 30th, 2005 08:08 PM
SELECT TOP !!!! Jane SQL Language 2 October 17th, 2003 11:22 AM
SELECT TOP ciko1973 SQL Language 5 August 8th, 2003 04:35 AM

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