Re: how to minimize DataTable size with SQL specified rows?
- From: R Reyes <RReyes@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 31 Oct 2008 10:00:01 -0700
Right that is what I did. But as I said in my previous post, we are not
allowed to nest two SELECT TOP statements together, unless I am using it
wrong?
Please look at my SQL:
SELECT TOP 20 FROM (SELECT TOP 50 FROM TBL_Image ORDER BY
TBL_Image.DateSubmitted DESC) ORDER BY TBL_Image.DateSubmitted"
The error is reported at the first "SELECT TOP 20". If I change SELECT TOP
20, to SELECT *, it will work. But, I need the top 20...
I can't seem to get around this no matter how I change the words around. It
doesn't let me use two SELECT TOPs nested within another when specifying the
exact # of rows to return. Also, it errors when I try to nest two ORDER BY
statements.
Could you provide an example that works, that you've tested please? Thanks
much!
"Lee" wrote:
On Oct 30, 11:51 pm, R Reyes <RRe...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:.
It seems that there is no BOTTOM function for SQLServer2005. :(
So I tried using two nested SELECT TOPs with one returning the inverse order
of rows (acting as a BOTTOM function). However, it looks like I'm not
allowed to nest two SELECT TOPs either.
This was my SQL: "SELECT TOP 12 FROM (SELECT TOP 12 FROM TBL_Image ORDER BY
TBL_Image.DateSubmitted DESC) ORDER BY TBL_Image.DateSubmitted"
Any ideas on how to resolve this?
Thanks again for your time.
"R Reyes" wrote:
Awesome! Will give it a shot and have no doubt that it will work from the
way you explained.
Thank you
"Lee" wrote:
It's been a long time since I wrote SQL in SQL Server (My company is
an Oracle shop) but I remember being able to use the TOP and BOTTOM
keywords to get a set of data.
So, if you wanted the 21 thru 40th record:
SELECT BOTTOM 20
*
FROM
(SELECT TOP 40
*
FROM
MyTable
WHERE
....
)
So in essence, you select your max number, then from that selection
you select your final grouping, i.e.,
If these were the records:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49, .....
the select with the TOP clause limits the data to:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
by getting the top 40 records, then the select with the BOTOM clause
futher limits the data to:
21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
by getting the last 20 records from the prior select.
I'm sure that there are many newer and better ways too do this now -
especially if you are wanting to use Stored Procs., but this is how to
do it "Old School".
Hope this helps,
L. Lee Saunders
http://oldschooldotnet.blogspot.com- Hide quoted text -
- Show quoted text -
To simulate BOTTOM, just use TOP with the sort order reversed.
L. Lee Saunders
http://oldschooldotnet.blogspot.com
- References:
- how to minimize DataTable size with SQL specified rows?
- From: R Reyes
- Re: how to minimize DataTable size with SQL specified rows?
- From: Lee
- Re: how to minimize DataTable size with SQL specified rows?
- From: R Reyes
- Re: how to minimize DataTable size with SQL specified rows?
- From: R Reyes
- Re: how to minimize DataTable size with SQL specified rows?
- From: Lee
- how to minimize DataTable size with SQL specified rows?
- Prev by Date: How to pass more than 2 parameters in C#
- Next by Date: Re: Inserting hyphens into a string - covering all possibilities.... please help!
- Previous by thread: Re: how to minimize DataTable size with SQL specified rows?
- Next by thread: delegate object as an argument
- Index(es):
Relevant Pages
|