Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
DELETE query to have deletion of the rows count from >40

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
tecnokrat
Warnings : 1

Active User


Joined: 22 May 2009
Posts: 160
Location: Bangalore

PostPosted: Mon Nov 25, 2013 12:19 am    Post subject: DELETE query to have deletion of the rows count from >40
Reply with quote

Hi,
I have a table with records more than 40 for each of the particular entry.

Now my program has a logic which checks for 40 occurences only in this table and then close the cursor.

But the records are more than 40 for each of these entries the flag is not set appropriately and the Cursor is not getting closed.

This is the reason when some other records are tried to fetch and the cursor never got closed.

The below query gave the total number of records having count more than 40 for each record in the table.
Quote:

SELECT A
from table TAB1
GROUP BY A
HAVING COUNT(*) > 40


Now I need to tweak the query where it should delete only the record's entries from 41st and not the whole of the record.


Quote:

DELETE
FROM TABLE TAB1 a
WHERE a.A IN (
SELECT b.A
FROM TABLE TAB1 b
GROUP BY b.A
HAVING COUNT(*) > 40)


Please can you help.
Back to top
View user's profile Send private message

Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 1932
Location: UK

PostPosted: Mon Nov 25, 2013 2:51 pm    Post subject:
Reply with quote

suppose the 41st, or greater, row is the latest? DB2 does not guarantee the order in which is returns rows so you could be getting back ones thaty you would perhaps want deleted. Why not read all the records, process the 40 that you need and then issue deletes for the remining using keys that you have stored in working storage?
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Mon Nov 25, 2013 2:59 pm    Post subject:
Reply with quote

1) Using Olap function row_number() partition by
Code:
delete from tableX X  where rid(x) in (
select RI from (                                     
select row_number() over                             
                       (partition by ColA
                        order by ColB) as RN       
        ,rid(A) as RI                                 
         from  tableX A) B               
where rn > 40                                        )

or
2) Using fetch first n Rows only in subselect
Code:
delete from tableX X where RID(X) not in (                               
select y.ri    from                                 
           (select ColA from tableX  group by ColA) A                   
, table  (select rid(B) as RI from TableX B       
         where a.ColA = b.ColA
            order by b.colB                                 
   fetch first 40 rows only) Y                     
)
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts query to fetch record which has only ... maxsubrat DB2 12 Mon Dec 11, 2017 5:03 pm
No new posts Merging 2 records at multiple rows wi... Bijesh DFSORT/ICETOOL 2 Wed Dec 06, 2017 1:50 am
No new posts ISMF Difference between volume count ... upendrasri IBM Tools 2 Tue Dec 05, 2017 12:40 pm
No new posts Can we combine the rows with same key... V S Amarendra Reddy DB2 12 Fri Dec 01, 2017 10:29 pm
No new posts How to query MAXGENS of an existing P... Chuchulo Hatyzak JCL & VSAM 4 Wed Nov 22, 2017 10:23 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us