Friday, March 23, 2012

order by Desc,third data

in sql
I use "select top 3 from.....order by (tablename) desc,that can give 3 datas
but i want third data to my program
So i use other function:
SELECT TOP 1 'answer'=AAA.A FROM
(
select distinct top 3 'A'=month FROM dataTable
where month < '200511'
ORDER BY month DESC
) as AAA
ORDER BY AAA.A
But ,how can i do fast?
Can use other function?
This should be very fast if you have the right indexes. Do you have an
index on Month?
Andrew J. Kelly SQL MVP
"Sam_Chou" <SamChou@.discussions.microsoft.com> wrote in message
news:75531889-BD27-4AAF-AA1B-86A081FF1B54@.microsoft.com...
> in sql
> I use "select top 3 from.....order by (tablename) desc,that can give 3
> datas
> but i want third data to my program
> So i use other function:
> SELECT TOP 1 'answer'=AAA.A FROM
> (
> select distinct top 3 'A'=month FROM dataTable
> where month < '200511'
> ORDER BY month DESC
> ) as AAA
> ORDER BY AAA.A
> But ,how can i do fast?
> Can use other function?
|||sorry...I have't an index on Month.
"Andrew J. Kelly" wrote:

> This should be very fast if you have the right indexes. Do you have an
> index on Month?
> --
> Andrew J. Kelly SQL MVP
>
> "Sam_Chou" <SamChou@.discussions.microsoft.com> wrote in message
> news:75531889-BD27-4AAF-AA1B-86A081FF1B54@.microsoft.com...
>
>
|||Well then it will scan the table each time. If you need to find the top 3
by month on a regular basis then you need an index on that column.
Andrew J. Kelly SQL MVP
"Sam_Chou" <SamChou@.discussions.microsoft.com> wrote in message
news:BFE52A0A-1F72-44EC-B599-5074816B5575@.microsoft.com...[vbcol=seagreen]
> sorry...I have't an index on Month.
> "Andrew J. Kelly" wrote:

No comments:

Post a Comment