473,468 Members | 4,540 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

selecting x records from table n times according to variable criteria?

Hi All,

Sorry if the subject line is too obscure -- I couldn't think of a way
of describing this request.

I have a table that contains approximately 1 million records.

I want to be able to be able to select the top x records out of this
table matching variable criteria.

Pseudo table records:

custid, category, segment
1,1,1
2,1,1
3,1,1
4,1,1
5,1,2
6,1,2
7,1,2
8,1,2
9,2,1
10,2,1
11,2,1
12,2,1
13,2,2
14,2,2
15,2,2
16,2,2
17,2,3
18,2,3
19,2,3
20,2,3
So, what I'm trying to do is return a recordset, for example, that
contains the top 2 of each variation of category and segment.

ie:

1,1,1
2,1,1
5,1,2
6,1,2
9,2,1
10,2,1
13,2,2
14,2,2
17,2,3
18,2,3

The only way I can think to achieve this is in a while statement,
performing individual selects against each combination, feeding the
where criteria by variables that I automatically increment.

I can't help thinking there's a much more graceful way of achieving
this?

If anyone can give me any insight into this I'd be incredibly
appreciative!

Many thanks in advance!

Much warmth,

Murray
Jul 20 '05 #1
1 2991
> So, what I'm trying to do is return a recordset, for example, that
contains the top 2 of each variation of category and segment.
Here's one method, assuming the 'top 2' are the 2 rows with the lowest
custid values and custid is unique within category and segment.

CREATE TABLE MyTable
(
custid int,
category int,
segment int
)
INSERT INTO MyTable
SELECT 1,1,1
UNION ALL SELECT 2,1,1
UNION ALL SELECT 3,1,1
UNION ALL SELECT 4,1,1
UNION ALL SELECT 5,1,2
UNION ALL SELECT 6,1,2
UNION ALL SELECT 7,1,2
UNION ALL SELECT 8,1,2
UNION ALL SELECT 9,2,1
UNION ALL SELECT 10,2,1
UNION ALL SELECT 11,2,1
UNION ALL SELECT 12,2,1
UNION ALL SELECT 13,2,2
UNION ALL SELECT 14,2,2
UNION ALL SELECT 15,2,2
UNION ALL SELECT 16,2,2
UNION ALL SELECT 17,2,3
UNION ALL SELECT 18,2,3
UNION ALL SELECT 19,2,3
UNION ALL SELECT 20,2,3

SELECT
custid,
category,
segment
FROM MyTable a
WHERE
(
SELECT COUNT(*)
FROM MyTable b
WHERE
b.category = a.category AND
b.segment = a.segment AND
b.custid <= a.custid
) <= 2
ORDER BY
category,
segment,
custid

--
Hope this helps.

Dan Guzman
SQL Server MVP

"M Wells" <pl**********@planetthoughtful.org> wrote in message
news:km********************************@4ax.com... Hi All,

Sorry if the subject line is too obscure -- I couldn't think of a way
of describing this request.

I have a table that contains approximately 1 million records.

I want to be able to be able to select the top x records out of this
table matching variable criteria.

Pseudo table records:

custid, category, segment
1,1,1
2,1,1
3,1,1
4,1,1
5,1,2
6,1,2
7,1,2
8,1,2
9,2,1
10,2,1
11,2,1
12,2,1
13,2,2
14,2,2
15,2,2
16,2,2
17,2,3
18,2,3
19,2,3
20,2,3
So, what I'm trying to do is return a recordset, for example, that
contains the top 2 of each variation of category and segment.

ie:

1,1,1
2,1,1
5,1,2
6,1,2
9,2,1
10,2,1
13,2,2
14,2,2
17,2,3
18,2,3

The only way I can think to achieve this is in a while statement,
performing individual selects against each combination, feeding the
where criteria by variables that I automatically increment.

I can't help thinking there's a much more graceful way of achieving
this?

If anyone can give me any insight into this I'd be incredibly
appreciative!

Many thanks in advance!

Much warmth,

Murray

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Belcher | last post by:
While I'm sure this is simple I just can't figure it out. Table A (assignments) is on the One side of a One to Many relationship With Table B (assignees)There are many assignees assigned the one...
2
by: rivka.howley | last post by:
I wrote some code that creates a table with a date/time field at 15-minute intervals. Here's how I create and populate the table With tblDataTemp ..Fields.Append .CreateField("CT_ID", dbLong)...
12
by: jaYPee | last post by:
I have currently using a dataset to access my data from sql server 2000. The dataset contains 3 tables that is related to each other. parent/child/grandchild relationship. My problem is it's very...
10
by: Jeff | last post by:
Hey gang. i have a code that i will list. when varM = 8 or 16, the script works fine, and pulls the top 8 or top 16, but if it =32 or 64, it is only pulling the top 17 records from the DB. db is...
2
by: Catch_22 | last post by:
Hi, I have a stored procedure that has to extract the child records for particular parent records. The issue is that in some cases I do not want to extract all the child records only a...
11
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night)...
9
by: tbeers | last post by:
I apologize for what is probably a simple solution. I have a data entry form that I use to enter work times. I have the default view set to "continuos forms" and the form is enter through a...
4
by: Jeffrey Davis | last post by:
I'm hoping that someone here can give me some assistance with a database I'm trying to set up. My skills in Access are fairly basic, and I'm trying to skill up, but some of the stuff is a little...
5
by: hollyquinn | last post by:
Hi I am working with a web application where I am selecting values from a SQL Server 2005 database and then loading the values into different controls on my page. Most of the values load with no...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.