Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 rowsets in blocks via a query
 New Topic  Topic Locked
 Printer Friendly
Next Page
Author Previous Topic Topic Next Topic
Page: of 2

Etymon
Advanced Member

United States
2395 Posts

Posted - 11 June 2007 :  10:06:26  Show Profile  Visit Etymon's Homepage
I'm trying to place records into groups of 100, so I need a query that will return records but not based upon an autonumber type field. For instance, if I had a set of records that had been sequentially numbered (by autonumber) with IDs to greater than 3,600 but there were actually only 300 IDs remaining ranging from 1 to 3600 (say the others were deleted), then I would want to use a query to make three groups of 100 of the remaining IDs.

I can use SELECT TOP 100 for the first 100 records. How do I get subsequent groups of 100?

It's probably obvious and I am thinking too hard.


Edited by - Etymon on 11 June 2007 10:10:37

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 11 June 2007 :  10:09:50  Show Profile  Send ruirib a Yahoo! Message
Use a SELECT TOP within another SELECT TOP, such as in

SELECT TOP 100 * FROM (
SELECT TOP 200 * ORDER BY ID ASC
) As Temp
ORDER BY ID DESC

This will get you the middle block. To get the last block, just change the 200 to 300.
This should work in SQL Server and maybe Access, but the Access syntax may need a little tweaking.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Etymon
Advanced Member

United States
2395 Posts

Posted - 11 June 2007 :  10:13:33  Show Profile  Visit Etymon's Homepage
Awesome! Thanks Rui.

I just needed it for MS SQL.

Edited by - Etymon on 11 June 2007 10:14:31
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 11 June 2007 :  10:19:43  Show Profile  Send ruirib a Yahoo! Message
You're welcome .


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Etymon
Advanced Member

United States
2395 Posts

Posted - 11 June 2007 :  10:28:28  Show Profile  Visit Etymon's Homepage
Ah gee! Hi again Rui ... I hate to bother you,

I'm having trouble with the query. For now I'm using the query in Visual Studio 2005. I chose to try it out in the FORUM_A_REPLY table, but I get errors.




Edited by - Etymon on 11 June 2007 10:29:40
Go to Top of Page

Etymon
Advanced Member

United States
2395 Posts

Posted - 11 June 2007 :  12:37:06  Show Profile  Visit Etymon's Homepage
Ok, I got it to return results, but I'm still having trouble getting it to return results unique to a second grouping of 100.

My first query is:

SELECT TOP 100 TOPIC_ID
FROM FORUM_TOPICS
ORDER BY TOPIC_ID

My second query is:

SELECT TOP 100 TOPIC_ID
FROM (SELECT TOP 200 TOPIC_ID
FROM FORUM_TOPICS AS NEXT_200_TOPICS
ORDER BY TOPIC_ID) AS FORUM_TOPICS
ORDER BY TOPIC_ID

Both queries are returning the same results for me.

Am I doing something wrong?

Thanks again, Rui.
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 11 June 2007 :  13:16:38  Show Profile  Send ruirib a Yahoo! Message
Look at the DESC and ASC in my code. They are very important to get the results you want. Use them just as I did - ASC in the internal query, DESC in the external query.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Etymon
Advanced Member

United States
2395 Posts

Posted - 11 June 2007 :  13:46:46  Show Profile  Visit Etymon's Homepage
Right! I see!

When I checked the query in Visual Studio, ASC (understood as a default keyword) was removed by the syntax checker when I used "Verify SQL Syntax" against the query ... resulting in:

SELECT TOP 100 TOPIC_ID
FROM (SELECT TOP 200 TOPIC_ID
FROM FORUM_TOPICS AS NEXT_200_TOPICS
ORDER BY TOPIC_ID) AS FORUM_TOPICS
ORDER BY TOPIC_ID DESC

I was wanting the results to list from smallest to largest ID, so I put ASC instead of DESC on the last line. I altered this keyword first and then executed the query before executing the query using DESC.

Thanks again Rui!

Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 11 June 2007 :  14:55:00  Show Profile  Send ruirib a Yahoo! Message
If you want the results in another order, you need ANOTHER outer query, just to order the records them again... The order in the two existing queries is important to get the desired records. You can't change it, or you won't get the 2nd 100 records block.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Etymon
Advanced Member

United States
2395 Posts

Posted - 11 June 2007 :  15:43:33  Show Profile  Visit Etymon's Homepage
Help??!

Edited by - Etymon on 11 June 2007 16:14:49
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 11 June 2007 :  15:59:40  Show Profile  Send ruirib a Yahoo! Message

SELECT * FROM (
SELECT TOP 100 TOPIC_ID
FROM (SELECT TOP 200 TOPIC_ID
FROM FORUM_TOPICS AS NEXT_200_TOPICS
ORDER BY TOPIC_ID) AS FORUM_TOPICS
ORDER BY TOPIC_ID DESC
) As T2
Order by TOPIC_ID ASC;


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Etymon
Advanced Member

United States
2395 Posts

Posted - 11 June 2007 :  16:16:08  Show Profile  Visit Etymon's Homepage
Sehr Gute! Danke! Danke! (Very Good! Thanks! Thanks!)

Edited by - Etymon on 11 June 2007 17:25:30
Go to Top of Page

Etymon
Advanced Member

United States
2395 Posts

Posted - 12 June 2007 :  11:25:03  Show Profile  Visit Etymon's Homepage
Hmmm ...

Next problem.

If I have say 150+ Topic IDs and I select the second group of 100 ...

The problem I am having is that the second query for the second set of 100 is selecting the Topic IDs from largest to smallest and then the outer query is selecting from largest to smallest and then sorting them from smallest to largest. However, the first query for the first 100 is selecting from smallest to largest and sorting from smallest to largest. This is creating a union of the two sets of 100 creating an overlap in returned Topic IDs.

How do I work around the union?

Edited by - Etymon on 12 June 2007 11:54:39
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 12 June 2007 :  14:41:13  Show Profile  Send ruirib a Yahoo! Message
That happens because you don't have 200 different topics? If so, you need to get the first hundred and then just the rest, not one hundred again...

Anyway, why can't you, somehow, use a temporary table or a field in the same table or another table to signal the records you have previously selected? It would be way much easier.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Etymon
Advanced Member

United States
2395 Posts

Posted - 12 June 2007 :  14:47:46  Show Profile  Visit Etymon's Homepage
Hmmmm. That does sound easier.

Did you get the email that I sent to you through the forums?
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 12 June 2007 :  14:53:09  Show Profile  Send ruirib a Yahoo! Message
Yeah, just got it. Will reply.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Next Page
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.3 seconds. Powered By: Snitz Forums 2000 Version 3.4.07