MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Retaining first duplicate in my cursor fetch in a SP

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
mf_learner
Beginner


Joined: 04 May 2006
Posts: 11
Topics: 4

PostPosted: Sat Aug 26, 2006 10:48 am    Post subject: Retaining first duplicate in my cursor fetch in a SP Reply with quote

Hi,

I think this is more of a Sql tuning query.

I have a cursor defined in my Stored Proc which returns data as following -
Code:

Col1    Col2   Col3   Col4    Col5
-----  ------ ------ ------   ------
111111 121212 1A1313 20060825 100
111111 121212 1B1313 20060825 200
111111 121212 1C1313 20060826 200
111111 121212 1D1313 20060826 400
111111 121212 1E1313 20060827 300
111111 121212 1F1313 20060827 600



What I want is to have one record each from the above duplicate entries - as you can see there are two entries for a single date - I want to have only the first entry for any date as my output.

Output I desire will look like:


Code:

Col1    Col2   Col3   Col4    Col5
-----  ------ ------ ------   ------
111111 121212 1A1313 20060825 100
111111 121212 1C1313 20060826 200
111111 121212 1E1313 20060827 300


I have done a search on this thinking this to be sort of a common query that I am asking, but what I got is only by doing it by sort. I need to do this by SQL in my stored proc.

I am looking for the best possible way to do this.

Thanks,
_________________
Thanks
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12380
Topics: 75
Location: San Jose

PostPosted: Sat Aug 26, 2006 7:51 pm    Post subject: Reply with quote

mf_learner,

If you need COL3 and CCL5 then you cannot do within a single query.

kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mf_learner
Beginner


Joined: 04 May 2006
Posts: 11
Topics: 4

PostPosted: Sat Aug 26, 2006 8:18 pm    Post subject: Reply with quote

Thanks Kolusu for the reply.

I am posting the cursor below. Only thing is that it is defined inside a Stored Proc.

Code:


This is how the cursor is defined :
---------------------------------------

SELECT ms.loc, ms.item, cs.res,
       TRUNC(ms.start_dt, 'D') as sun_start_dt,
       SUM(ms.quantity) as sum_quantity
  FROM mstr_schd ms, prodmt c, prodmtstep cs
WHERE ms.item = c.item
   AND c.item = cs.item
   AND ((ms.loc = c.loc) OR  (ms.loc||'M' = c.loc))
   AND c.loc = cs.loc
   AND c.cppprodmethod = cs.cppprodmethod
   AND stepnum = 1
   AND c.priority in (SELECT MIN(priority)
                        FROM prodmt cp
                       WHERE c.item = cp.item
                         AND c.loc = cp.loc)
GROUP BY ms.item, ms.loc, cs.res, TRUNC(start_dt, 'D')
ORDER BY ms.item, ms.loc, sun_start_dt;

Let me give you the example with the data now (I handpicked this example and have edited the values for obvious reasons) -



Output now returned by the cursor for the combo of a particular item and loc -
------------------------------------------------------------------------------------

SELECT ms.loc, ms.item, cs.res,
       TRUNC(ms.start_dt, 'D') as sun_start_dt,
       SUM(ms.quantity) as sum_quantity
  FROM mstr_schd ms, prodmt c, prodmtstep cs
WHERE ms.item = c.item
   AND c.item = cs.item
  AND c.item = '11111111111111' AND c.loc = '222222M'
   AND ((ms.loc = c.loc) OR  (ms.loc||'M' = c.loc))
   AND c.loc = cs.loc
   AND c.cppprodmethod = cs.cppprodmethod
   AND stepnum = 1
   AND c.priority in (SELECT MIN(priority)
                        FROM prodmt cp
                       WHERE c.item = cp.item
                         AND c.loc = cp.loc)
GROUP BY ms.item, ms.loc, cs.res, TRUNC(start_dt, 'D')
ORDER BY ms.item, ms.loc, sun_start_dt;

LOC        ITEM                    RES                SUN_START_DT     SUM_QUANTITY

222222  11111111111111      A                   2006-08-20-00:00        19125
222222  11111111111111      B                   2006-08-20-00:00        38250
222222  11111111111111      A                   2006-08-27-00:00        14943
222222  11111111111111      B                   2006-08-27-00:00        29886

Desired output I am looking for:
-------------------------------------

LOC        ITEM                      RES        SUN_START_DT     SUM_QUANTITY

222222   11111111111111       A        2006-08-20-00:00        19125
222222   11111111111111       A        2006-08-27-00:00        14943



Hope this helps.

Thanks
_________________
Thanks
Back to top
View user's profile Send private message
mf_learner
Beginner


Joined: 04 May 2006
Posts: 11
Topics: 4

PostPosted: Sun Aug 27, 2006 2:48 pm    Post subject: Reply with quote

Hi,

This has been fixed. We have used ROW_NUMBER() . Like as below -

Code:


select loc, item, res, sun_start_dt, sum_quantity from
(
select t.*,
row_number() over (partition by loc, item, sun_start_dt order by res) rn
from   
(
SELECT ms.loc, ms.item, cs.res,TRUNC(ms.start_dt, 'D') as sun_start_dt,
SUM(ms.quantity) as sum_quantity
FROM   speed_mstr_schd ms, prodmt c, prodmtstep cs
WHERE  ms.item = c.item
AND    c.item = cs.item
AND    c.item = '11111111111111' AND c.loc = '222222M'
AND    ((ms.loc = c.loc) OR  (ms.loc||'M' = c.loc))
AND    c.loc = cs.loc 
AND    c.cppprodmethod = cs.cppprodmethod
AND    stepnum = 1     
AND    c.priority in
(
SELECT MIN(priority)
FROM   prodmt cp
WHERE  c.item = cp.item
AND    c.loc = cp.loc

GROUP BY ms.item, ms.loc, cs.res, TRUNC(start_dt, 'D')
) t
)
where rn = 1
ORDER BY item, loc, sun_start_dt;



Thanks to all for sparing their time in going through this post and trying to help me out.
_________________
Thanks
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group