Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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
Default SELECT TOP FROM HAVING

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:

INSERT INTO Table1
SELECT TOP 100 *
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.

Reply With Quote
  #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
Default

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:

INSERT INTO Table1
SELECT TOP 100 *
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.
www.custom-apps.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:09 PM.


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