Unix Technical Forum

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

This is a discussion on selecting x records from table n times according to variable criteria? within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi All, Sorry if the subject line is too obscure -- I couldn't think of a way of describing ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 02:21 AM
M Wells
 
Posts: n/a
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 02:21 AM
Dan Guzman
 
Posts: n/a
Default Re: selecting x records from table n times according to variable criteria?

> 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" <planetquirky@planetthoughtful.org> wrote in message
news:kmb170da9um9m7jvappsf5a2jfon2qo8qu@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



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:45 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com