Icon View Thread

The following is the text of the current message along with any replies.
Messages 11 to 20 of 20 total
Thread Can I do this in SQL?
Tue, Apr 3 2012 12:06 PMPermanent Link

John Hay

Ul
>
> 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

A very elegant solution.

John

Tue, Apr 3 2012 12:23 PMPermanent Link

Uli Becker

John,

> A very elegant solution.

I guess it was written by you using a pseudonym. Smile

Regards Uli
Tue, Apr 3 2012 12:33 PMPermanent Link

John Hay

Uli Becker" <johnmuller54@googlemail.com> wrote in message
news:BF2096F3-14C7-43B1-BC65-7D7857D379BD@news.elevatesoft.com...
> John,
>
> > A very elegant solution.
>
> I guess it was written by you using a pseudonym. Smile

I wish Smile

The use of a cartesian join in the solution did get me interested.  For all but really large datasets there is no issue.
If you do have a lot of different rows in each category (and I mean a lot!) it is possibble to use a combination of
EDB's RUNSUM() and MIN() functions to get the same result, quicker without resorting to a join.  The resulting sql is
(nearly) unintelligible.

Cheers

John

Wed, Apr 4 2012 3:45 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

John


Post it - I want to test my reading skills Smiley

Roy Lambert
Wed, Apr 4 2012 6:28 AMPermanent Link

John Hay

Roy

> Post it - I want to test my reading skills Smiley

Have fun Smiley

John

select yearno,monthno,intnumber,username,temp-lowest+1 as rank from
 (select yearno,monthno,username,intnumber,runsum(1) as temp from
   (select
      extract(year from xdate) as yearno,
      extract(month from xdate) as monthno,
      Username,sum(intnumber) as intnumber from jobs
    group by yearno,monthno,username
    order by yearno,monthno,intnumber desc) t1
  group by yearno,monthno,username,intnumber
  order by yearno,monthno,intnumber desc) t2
join
 (select yearno,monthno,min(temp) as lowest from
   (select yearno,monthno,username,intnumber,runsum(1) as temp from
     (select
        extract(year from xdate) as yearno,
        extract(month from xdate) as monthno,
        Username,sum(intnumber) as intnumber from jobs
      group by yearno,monthno,username
      order by yearno,monthno,intnumber desc) t3
    group by yearno,monthno,username,intnumber) t4
  group by yearno,monthno) t5
on t5.yearno=t2.yearno and t5.monthno=t2.monthno
group by yearno,monthno,username,intnumber,rank
having rank < 4
order by yearno desc,monthno desc,rank

Wed, Apr 4 2012 7:02 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

John


You are one seriously sad individual Smiley

Roy Lambert

ps reading skills about year 2
Wed, Apr 4 2012 10:29 AMPermanent Link

Uli Becker

> select yearno,monthno,intnumber,username,temp-lowest+1 as rank from
>    (select yearno,monthno,username,intnumber,runsum(1) as temp from
>      (select
>         extract(year from xdate) as yearno,
>         extract(month from xdate) as monthno,
>         Username,sum(intnumber) as intnumber from jobs
>       group by yearno,monthno,username
>       order by yearno,monthno,intnumber desc) t1
>     group by yearno,monthno,username,intnumber
>     order by yearno,monthno,intnumber desc) t2
> join
>    (select yearno,monthno,min(temp) as lowest from
>      (select yearno,monthno,username,intnumber,runsum(1) as temp from
>        (select
>           extract(year from xdate) as yearno,
>           extract(month from xdate) as monthno,
>           Username,sum(intnumber) as intnumber from jobs
>         group by yearno,monthno,username
>         order by yearno,monthno,intnumber desc) t3
>       group by yearno,monthno,username,intnumber) t4
>     group by yearno,monthno) t5
> on t5.yearno=t2.yearno and t5.monthno=t2.monthno
> group by yearno,monthno,username,intnumber,rank
> having rank<  4
> order by yearno desc,monthno desc,rank

Depressing!

Smile
Wed, Apr 4 2012 10:50 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Uli

Over quoting Smiley


Roy Lambert
Mon, Apr 23 2012 2:39 AMPermanent Link

Lucian

Sorry for being so late (was renovating my house and was not on Delphi for quite some time):
Thank you very much!

Lucian
Mon, Apr 23 2012 3:26 AMPermanent Link

Lucian

>Have fun Smiley

Thank you John, it did the trick!
Lucian
« Previous PagePage 2 of 2
Jump to Page:  1 2
Image