sql help

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
adams06
Participant
Posts: 92
Joined: Sun Mar 12, 2006 3:00 pm

sql help

Post by adams06 »

select count(*),BookNbr
from tablename
where BookNbr like '0%'
group by BookNbr asc
;

0012
0013
0014
0015
0112
0113
0114
0115
0212
0213
0214
0215

How can i get the top 3 records from each group .depending on first 2 positions of BookNbr

Thanks in advance
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Generate a counter within each group and pass only those rows for which the counter value is less than or equal to 3.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
prasannakumarkk
Participant
Posts: 117
Joined: Wed Feb 06, 2013 9:24 am
Location: Chennai,TN, India

Post by prasannakumarkk »

In which DB you are trying to acheive this. or you are trying to acheive in Datastage
Thanks,
Prasanna
chandra.shekhar@tcs.com
Premium Member
Premium Member
Posts: 353
Joined: Mon Jan 17, 2011 5:03 am
Location: Mumbai, India

Post by chandra.shekhar@tcs.com »

If Its DB2 then below query should work

Code: Select all

SELECT
    BOOKNBR
FROM
    (
        SELECT
            BOOKNBR,
            RANK() OVER (PARTITION BY BOOKNBR ORDER BY BOOKNBR) RNK
        FROM
            TABLENAME
        WHERE
            BOOKNBR LIKE '0%')
WHERE
    RNK <=3
Thanx and Regards,
ETL User
adams06
Participant
Posts: 92
Joined: Sun Mar 12, 2006 3:00 pm

Post by adams06 »

Oracle sql
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

As noted, you don't need to do this in SQL... why not just implement the logic in job as Ray suggested?
-craig

"You can never have too many knives" -- Logan Nine Fingers
priyadarshikunal
Premium Member
Premium Member
Posts: 1735
Joined: Thu Mar 01, 2007 5:44 am
Location: Troy, MI

Post by priyadarshikunal »

You can do both way either as Ray suggested or the Query provided by Chandra, It will work for oracle as well.
Priyadarshi Kunal

Genius may have its limitations, but stupidity is not thus handicapped. :wink:
Post Reply