Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:48:26+00:00 2026-05-29T09:48:26+00:00

I need to delete only the first appear of a record. This have a

  • 0

I need to delete only the first appear of a record. This have a PK (preld_item, IDENTITY).

This is the very slow DELETE..

DELETE pd
FROM   preliquidaciones_deta pd
WHERE  pd.preld_item IN (SELECT MAX(p.preld_item)
                         FROM   preliquidaciones_deta p
                         WHERE  p.id_preliquidacion = '216'
                         GROUP  BY p.id_sds_linea_equipo,
                                   p.id_concepto
                         HAVING COUNT(p.id_sds_linea_equipo) > 1) 

Thanks

EDIT:

  • The table has more than 5 million rows
  • id_preliquidacion has a clustered index (pk)

EDIT 2:
@vulkanino i think in you answer and i modify the delete…

DECLARE @loPreldItem INT
DECLARE curItems CURSOR FOR (select max(p.preld_item)
    from preliquidaciones_deta p 
    where p.id_preliquidacion = '216' 
    group by p.id_sds_linea_equipo, p.id_concepto
    having count(p.id_sds_linea_equipo) > 1)

OPEN curItems
FETCH NEXT FROM curItems INTO @loPreldItem
WHILE @@FETCH_STATUS = 0
BEGIN
    DELETE FROM preliquidaciones_deta WHERE preld_item = @loPreldItem
    FETCH NEXT FROM curItems INTO @loPreldItem
END
CLOSE curItems
DEALLOCATE curItems

This works best but continue slow

EDIT 3: The execution plan of the first delete

delete from preliquidaciones_deta where preld_item in (  select max(preld_item)  from preliquidaciones_deta   where id_preliquidacion = '216'  group by id_sds_linea_equipo, id_concepto, preld_nse, preld_linea  having count(id_preliquidacion) > 1)
  |--Assert(WHERE:(CASE WHEN NOT [Expr1018] IS NULL THEN (0) ELSE CASE WHEN NOT [Expr1019] IS NULL THEN (1) ELSE NULL END END))
       |--Nested Loops(Left Semi Join, OUTER REFERENCES:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item]), DEFINE:([Expr1019] = [PROBE VALUE]))
            |--Nested Loops(Left Semi Join, OUTER REFERENCES:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item]), DEFINE:([Expr1018] = [PROBE VALUE]))
            |    |--Clustered Index Delete(OBJECT:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[PK_preliquidaciones_deta]), OBJECT:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[IX_id_sds]), OBJECT:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[IX_id_sds_linea_equipo]))
            |    |    |--Top(ROWCOUNT est 0)
            |    |         |--Sort(DISTINCT ORDER BY:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item] ASC))
            |    |              |--Nested Loops(Inner Join, OUTER REFERENCES:([Expr1007]) OPTIMIZED)
            |    |                   |--Filter(WHERE:([Expr1006]>(1)))
            |    |                   |    |--Compute Scalar(DEFINE:([Expr1006]=CONVERT_IMPLICIT(int,[Expr1023],0)))
            |    |                   |         |--Stream Aggregate(GROUP BY:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[id_sds_linea_equipo], [data_dealernet_lucom].[dbo].[preliquidaciones_deta].[id_concepto], [data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_nse], [data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_linea]) DEFINE:([Expr1023]=Count(*), [Expr1007]=MAX([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item])))
            |    |                   |              |--Sort(ORDER BY:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[id_sds_linea_equipo] ASC, [data_dealernet_lucom].[dbo].[preliquidaciones_deta].[id_concepto] ASC, [data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_nse] ASC, [data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_linea] ASC))
            |    |                   |                   |--Clustered Index Scan(OBJECT:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[PK_preliquidaciones_deta]), WHERE:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[id_preliquidacion]='216'))
            |    |                   |--Clustered Index Seek(OBJECT:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[PK_preliquidaciones_deta]), SEEK:([data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item]=[Expr1007]) ORDERED FORWARD)
            |    |--Clustered Index Scan(OBJECT:([data_dealernet_lucom].[dbo].[liquidaciones_deta].[PK_liquidaciones_deta]), WHERE:([data_dealernet_lucom].[dbo].[liquidaciones_deta].[preld_item]=[data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item]))
            |--Table Scan(OBJECT:([data_dealernet_lucom].[dbo].[liquidaciones_diferidas]), WHERE:([data_dealernet_lucom].[dbo].[liquidaciones_diferidas].[preld_item]=[data_dealernet_lucom].[dbo].[preliquidaciones_deta].[preld_item]))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:48:27+00:00Added an answer on May 29, 2026 at 9:48 am

    You could try this alternative (as you state there won’t be more than 2 rows per id_sds_linea_equipo, id_concepto this has the same semantics as your original query)

    WITH T
         AS (SELECT *,
                    RN = ROW_NUMBER() OVER 
                                     (PARTITION BY id_sds_linea_equipo, id_concepto
                                          ORDER BY preld_item)
    
             FROM   preliquidaciones_deta
             WHERE  id_preliquidacion = '216')
    DELETE FROM T
    WHERE RN > 1  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, I need all rows from the first that don't appear
I need to delete an option from a select in certain circumstances. Basically: if(mystatement
I need to delete rows in a table, such as DELETE FROM TABLE_X WHERE
I need to delete and erase object pointer on a vector. I see this
I need to delete a picture from a folder where pictures are stored. For
I need to delete a few fields from the table in the database. I
I need to delete a file. Occasionally, the file may be locked, in this
I need to delete old and unmaintained branches from our remote repository. I'm trying
I have this homework problem where I need to use regex to remove every
I have a mysql table that I need to only contain 20 newest records

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.