|
-
Jan 2nd, 2025, 04:46 PM
#1
Thread Starter
Frenzied Member
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...
-
Jan 2nd, 2025, 04:59 PM
#2
-
Jan 3rd, 2025, 02:03 AM
#3
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
-
Jan 14th, 2025, 01:37 PM
#4
New Member
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
-
Feb 10th, 2026, 09:29 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|