Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 20 total
Thread Can I do this in SQL?
Thu, Mar 29 2012 12:08 PMPermanent Link

Lucian

Hi,

I have this query:

SELECT
 EXTRACT(YEAR FROM XDate) as YearNo,
 EXTRACT(MONTH FROM XDate) as MonthNo,
 SUM(IntNumber) as IntNumber,
 UserName
FROM jobs
GROUP BY YearNo, MonthNo, UserName
ORDER BY YearNo, MonthNo, IntNumber DESC

It returns something like this:

2007 6 500 Paul....
2007 6 399 Randy...
2007 6 345 Tom.....
2007 6 299 I don't want this row
2007 6 295 I don't want this row
2007 6 256 I don't want this row
2007 5 800 Greg....
2007 5 689 Zoran...
2007 5 556 Edy.....
2007 5 398 I don't want this row
2007 5 201 I don't want this row
2007 4 700 Greg....
2007 4 449 Randy...
2007 4 256 Tom.....
2007 4 198 I don't want this row
2007 4 101 I don't want this row

However, I only want only the top 3 rows for each group Year, Month. Is it possible in SQL?

TIA
Lucian
Thu, Mar 29 2012 2:56 PMPermanent Link

Uli Becker

Lucian,

ORDER BY YearNo, MonthNo, IntNumber DESC

Just add

Range 1 to 3

to your query.

regards Uli
Fri, Mar 30 2012 3:34 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Ulrich

>Just add
>
>Range 1 to 3

Does that work for each group? I thought it was for the query as a whole.

Roy Lambert [Team Elevate]
Fri, Mar 30 2012 4:36 AMPermanent Link

Uli Becker

Roy,

> Does that work for each group? I thought it was for the query as a whole.

You are right: I should have read more carefully.

Uli
Fri, Mar 30 2012 4:37 AMPermanent Link

Lucian

>Range 1 to 3

As Roy pointed out, that does not work.
regards
Lucian
Fri, Mar 30 2012 4:45 AMPermanent Link

Lucian

I have some old DBISAM code that I'm converting to ElevateDB. There, after running the query, I was manually removing unwanted records from the result ... this however is something that does not work in ElevateDB anymore.
Fri, Mar 30 2012 6:29 AMPermanent Link

Uli Becker

Lucian,

> As Roy pointed out, that does not work.

I am not an expert in SQL, but I fount this:

http://rickosborne.org/blog/2008/01/sql-getting-top-n-rows-for-a-grouped-query/

Maybe it'll give you some hints how to proceed.

Regards Uli
Fri, Mar 30 2012 8:02 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Uli


Brilliant link. Looks like a good book as well. I might just treat myself to a second hand copy unless we can persuade John Hay to write a DBISAM / ElevateDB specific one.

Roy Lambert
Fri, Mar 30 2012 8:07 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Lucian

>I have some old DBISAM code that I'm converting to ElevateDB. There, after running the query, I was manually removing unwanted records from the result ... this however is something that does not work in ElevateDB anymore.

I personally think its sad that canned (non-sensitive) queries can't be edited. The options are now to create either a temporary table or an in-memory table and edit that.

I've tended to use in-memory tables. A bit more difficult to create than with DBIASM but Tim gave me a script which I modified and now use. Temporary tables are a doddle to create (look up CREATE TEMPORARY TABLE) and the reason I don't use them is I like the self cleaning nature of in-memory tables in the event of a crash.

Roy Lambert
Sat, Mar 31 2012 2:49 AMPermanent Link

Uli Becker

Roy,

<< unless we can persuade John Hay to write a DBISAM / ElevateDB specific one. >>

I'd like to place my order for that one without even knowing the price. Smile

Uli
Page 1 of 2Next Page »
Jump to Page:  1 2
Image