Results 1 to 5 of 5

Thread: SQL query help

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    SQL query help

    I'm trying to delete a row in a table using a sub-query, like as below:

    Code:
    delete from MyTable where id = '(select id from MyTable order by ctime limit 1)'
    id is a string value & ctime is a timestamp value. So basically the sub-query gets the id value from the row with the oldest timestamp & the first part of the query deletes the row having that id value. My problem is that I don't know how the handle the single quotes that are required around the id value. I can do this using 2 separate queries, but I'd like to combine them into 1 query. Thanks for any help...

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,748

    Re: SQL query help

    No quotes needed

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,269

    Re: SQL query help

    And why in blazes is ID a String?

    And which DBMS?

    Untested
    Code:
    WITH
      CT AS (SELECT ID, ROW_NUMBER() OVER(ORDER BY ctime) AS RN FROM MyTable)
    
    DELETE FROM MyTable AS MT
    INNER JOIN CT ON MT.ID=CT.ID AND CT.RN=1
    Without CTE
    Code:
    DELETE FROM MyTable AS MT
    INNER JOIN
      (SELECT ID, ROW_NUMBER() OVER(ORDER BY ctime) AS RN FROM MyTable) AS CT 
    ON MT.ID=CT.ID AND CT.RN=1
    Last edited by Zvoni; Jan 16th, 2025 at 04:01 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    New Member
    Join Date
    Jan 2025
    Posts
    2

    Re: SQL query help

    Hi bro i seen you was have issue in draw line and you got it resolve can you also help me i have same issue thank you

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: SQL query help

    Arnoutdv said it best.

    Here's a pretty version (defensive in case ctime is not distinct)

    Code:
    DELETE MT
    FROM   dbo.MyTable AS MT
    WHERE  MT.id =
    (
        SELECT TOP (1) id
        FROM   dbo.MyTable
        ORDER BY
               ctime,
               id
    )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width