× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Good catch Elvis!

I missed deleting that.

Charles


-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx
[mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Elvis Budimlic
Sent: Wednesday, August 01, 2007 10:14 AM
To: 'Midrange Systems Technical Discussion'
Subject: RE: Limiting SQL results returned

What Charles has should work for you Jim, just take out the
2nd WITH clause (CTEs list only requires WITH at the
beginning of the list).

Elvis

Celebrating 10-Years of SQL Performance Excellence
http://centerfieldtechnology.com/training.asp

-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx
[mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Wilt, Charles
Sent: Wednesday, August 01, 2007 7:10 AM
To: Midrange Systems Technical Discussion
Subject: RE: Limiting SQL results returned

Jim,

Off the top of my head....

With T1 as (select status, infoField, rand() as randomNbr
from filename
),
With T2 AS (SELECT Status,
InfoField,
ROW_NUMBER() over (PARTITION BY Status ORDER BY Status,
randomNbr) as Nbr
from T1 ORDER BY Status )

SELECT * FROM T2 WHERE Nbr < 6


Sorry, don't have v5r4 to test against.

HTH,
Charles

-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx
[mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Jim Essinger
Sent: Tuesday, July 31, 2007 7:02 PM
To: Midrange Systems Technical Discussion
Subject: Re: Limiting SQL results returned

This list is great!

Elvis

Thanks for pointing me in the right direction.

After doing it the hard way, this worked for me;

With T1 AS (SELECT Status,
InfoField,
ROW_NUMBER() over (PARTITION BY Status ORDER BY Status) as Nbr
from FileName ORDER BY Status )

SELECT * FROM T1 WHERE Nbr < 6

Any thoughts on how to get a random 5 selected instead of
the first 5?

Thanks again.

Jim

On 7/31/07, Elvis Budimlic
<ebudimlic@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Looks like ROW_NUMBER would work. Try this in Run Sql Scripts if
you're on
V5R4:

SELECT * FROM
(SELECT A.*,
ROW_NUMBER() OVER(PARTITION BY status ORDER BY
status) AS RN
FROM myfile A) B
WHERE RN IN (1,2,3,4,5)

or using common table expression:

WITH CTE AS
(SELECT A.*,
ROW_NUMBER() OVER(PARTITION BY status ORDER BY
status) AS RN
FROM myfile A)
SELECT * FROM CTE WHERE RN IN (1,2,3,4,5)

NOTE: It errored out on my system, but we're way behind on
db fixpacks.

Elvis

Celebrating 10-Years of SQL Performance Excellence
http://centerfieldtechnology.com/training.asp

-----Original Message-----
Subject: RE: Limiting SQL results returned

Scratch that syntax, I just realized it won't work. Try
this instead

WITH CTE1 AS
(SELECT * FROM MYFILE WHERE STATUS = 'status 1' FETCH
FIRST 5 ROWS
ONLY),
CTE2 AS
(SELECT * FROM MYFILE WHERE STATUS = 'status 2' FETCH
FIRST 5 ROWS
ONLY),
CTE3 AS
(SELECT * FROM MYFILE WHERE STATUS = 'status 1' FETCH
FIRST 5 ROWS
ONLY), ......
SELECT * FROM CTE1
UNION ALL
SELECT * FROM CTE2
UNION ALL
SELECT * FROM CTE3
....

Ugly, I know.

Celebrating 10-Years of SQL Performance Excellence
http://centerfieldtechnology.com/training.asp

-----Original Message-----
Subject: RE: Limiting SQL results returned

Brute force it by running something like:

SELECT * FROM MYFILE WHERE STATUS = 'status 1' FETCH FIRST
5 ROWS ONLY
UNION ALL SELECT * FROM MYFILE WHERE STATUS = 'status 2'
FETCH FIRST 5
ROWS ONLY UNION ALL SELECT * FROM MYFILE WHERE STATUS =
'status 1'
FETCH FIRST 5 ROWS ONLY ......

and do it as many times as you have unique statuses in your
table (you
said about 20 or so).

I haven't used new RANK, PARTITION and recursive CTEs yet
so I'm not
sure if there is more elegant way to address this.
I'd be curious to see one as well.

Elvis

-----Original Message-----
Subject: Limiting SQL results returned

SQL Guru's

Is there a way to limit the number of rows returned from a
SQL statement?
For instance, I want a sampling of 5 records for each
status from a file.

Status, Fn1, .... Fn6

For _each_ status that appears in the file, I want 5 and only 5
resulting rows, could be the first 5 or last 5 or a random 5 -
although a random 5 would be awesome - would help with
another project I have ahead of me.
There could be 20 different status codes in the file, and I
want 5 records
for all 20 status codes. I guess that there might not be
5 for some
statuses, so getting up to 5 results would be OK too.

Any thoughts?

Jim


--
This is the Midrange Systems Technical Discussion
(MIDRANGE-L) mailing
list To post a message email: MIDRANGE-L@xxxxxxxxxxxx To
subscribe,
unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting,
please take
a moment to review the archives at
http://archive.midrange.com/midrange-l.




--
This is the Midrange Systems Technical Discussion
(MIDRANGE-L) mailing
list To post a message email: MIDRANGE-L@xxxxxxxxxxxx To
subscribe,
unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting,
please take
a moment to review the archives at
http://archive.midrange.com/midrange-l.


--
This is the Midrange Systems Technical Discussion
(MIDRANGE-L) mailing list To post a message email:
MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list
options,
visit: http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting,
please take
a moment to review the archives at
http://archive.midrange.com/midrange-l.




This e-mail transmission contains information that is
intended to be confidential and privileged. If you receive
this e-mail and you are not a named addressee you are hereby
notified that you are not authorized to read, print, retain,
copy or disseminate this communication without the consent of
the sender and that doing so is prohibited and may be
unlawful. Please reply to the message immediately by
informing the sender that the message was misdirected. After
replying, please delete and otherwise erase it and any
attachments from your computer system. Your assistance in
correcting this error is appreciated.

--
This is the Midrange Systems Technical Discussion
(MIDRANGE-L) mailing list To post a message email:
MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change
list options,
visit: http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting,
please take a moment to review the archives at
http://archive.midrange.com/midrange-l.




--
This is the Midrange Systems Technical Discussion
(MIDRANGE-L) mailing list
To post a message email: MIDRANGE-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/midrange-l.




This e-mail transmission contains information that is intended to be confidential and privileged. If you receive this e-mail and you are not a named addressee you are hereby notified that you are not authorized to read, print, retain, copy or disseminate this communication without the consent of the sender and that doing so is prohibited and may be unlawful. Please reply to the message immediately by informing the sender that the message was misdirected. After replying, please delete and otherwise erase it and any attachments from your computer system. Your assistance in correcting this error is appreciated.


As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.