Can somebody please, help me with a query to delete only 500 rows from a table which has 20000 rows. Also has to be older than a particular date.
Thanks for your help,
Soofy
You can use the Top keyword like you would in a select
Delete Top (500)
From myTable
Where Date < '01/01/2009'
If you're using SQL Server 2005, you can do this:
DELETE TOP (500) FROM your_table
WHERE date_field < #my_particular_date
or you can do this:
SET ROWCOUNT 500
DELETE your_table
WHERE date_field < #my_particular_date
in SQL Server 2000, you can do this:
DELETE your_table
WHERE pk_field IN (
SELECT TOP (500) * FROM your_table
WHERE date_field < #my_particular_date
)
DELETE FROM Table_Name WHERE Primary_Key_Column IN (
SELECT TOP 500 Primary_Key_Column FROM Table_Name WHERE [Date] < '01/01/2009' ORDER BY Primary_Key_Column ASC
)
SET ROWCOUNT 500
DELETE FROM TableName WHERE TheDate < #YourDate
Top only works in Transact Sql, each sql has it's own version
For mySql and posGres
Delete
From myTable
Where Date < '01/01/2009'
LIMIT 10;
For oracle:
SELECT
FROM myTable
WHERE Date < '01/01/2009'
and ROWNUM <= 10
The only thing I'll add is you probably want to use "ORDER BY [DATE] DESC" at the end of most of these queries. And if you have multiple matching dates, you'll want to add additional column ordering to get the correct values deleted.
This presumes of course, that you actually have a "createddate" or "modifieddate" you can use to sort by. You didn't specify whether it was the oldest created or the oldest unmodified. (ie if ID=1 is the oldest but I modified it yesterday should it still be deleted?) which you'll need to be careful with if you sort by the primary key - assuming you're using an incrementing primary key and not GUIDs or something...
Related
Im trying to update a field in an oracle table, but I would only like to update 75 rows based on the order by creationdate field ascending.
This is what I have so far, but its not working.
UPDATE extractcandidate
SET process = 15
WHERE process IN
(Select process from extractcandidate where process = 1500 and rownum <=75 order by creationdate);
As #Gordon mentioned, you need to do the ORDER BY before the ROWNUM.
Firstly, order by creationdate:
SELECT *
FROM extractcandidate
WHERE process=1500
ORDER BY creationdate;
Secondly, limit the number to 75:
SELECT *
FROM (
SELECT *
FROM extractcandidate
WHERE process=1500
ORDER BY creationdate
)
WHERE rownum <= 75;
Now you can feed it into the UPDATE. To find the correct rows, you would normally use a primary key column. This seems to be missing in your case, so you can fall back to Oracle's internal ROWID:
UPDATE extractcandidate
SET process=15
WHERE rowid IN (
SELECT ri
FROM (
SELECT rowid as ri
FROM extractcandidate
WHERE process=1500
ORDER BY creationdate
)
WHERE rownum <= 75
);
You need an additional subquery for your method to work:
UPDATE extractcandidate
SET process = 15
WHERE ec.process = 1500 AND
creationdate IN (SELECT ec.creationdate
FROM (SELECT ec.*
FROM extractcandidate ec
WHERE ec.process = 1500
ORDER BY ec.creationdate
)
WHERE rownum <= 75
);
Notes:
You need to do the sorting before using rownum.
The comparison needs to be on creation date, not process.
I have a column in a table with dates like 2017-04-13, 2018-05-15. How can we find the oldest record using a SQL query?
You can use the top clause that SQL Server has:
select top 1 *
from yourTable
order by dateColumn asc
This way only the first row is returned and, combined with the ordering provided, that row will be the oldest one
Select top 1 * from table order by {nameofcolumn} Asc
You can try this:
SELECT * FROM YOURTABLE WHERE YOURDATEFIELD = (SELECT MIN(YOURDATEFIELD) FROM YOURTABLE)
select top 1 * from tablename
order by columnname desc
I have a SQL Server database table : History_KH
It look like:
Now I want to limit row number of this table (ex : 200 rows)
And when I insert new rows from code, It will insert on top of table. That's mean : Insert new rows on top of database table and the old rows over of 200 counts will be delete.
Please support me.
You can't really limit a table for a fix number of records but you can get rid of unwanted records. Assuming you are on SQL-Server
--STEP1 :Do your insert here
--STEP2: Delete older records over 200 ordering by dateTime column
;WITH CTE AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY DateTime DESC) rn
FROM YourTable
)
DELETE CTE WHERE rn > 200
You didn't even post what database you are using. I'm assuming mysql.
Assuming you want to order by Datetime and Username is unique, use this update...
update `History_KH` set `Username` = $myUser, `Datetime` = $myDatetime,
`Datechange` = $myDatechange, `Value` = $myValue where `Username` in
(select `Username` from `History_KH` order by `Datetime` limit 200)
I have a database with 1000 records.
I am trying to create an SQL statement so if the number of records grows above 1000, then the oldest records are deleted (i.e. the new records above 1000 'replace' the oldest records).
I am using SQLite, but I assume the usual SQL syntax will fit here.
If you use an auto-increment field, you can easily write this to delete the oldest 100 records:
DELETE FROM mytable WHERE id IN (SELECT id FROM mytable ORDER BY id ASC LIMIT 100)
Or, if no such field is present, use ROWID:
DELETE FROM mytable WHERE ROWID IN (SELECT ROWID FROM mytable ORDER BY ROWID ASC LIMIT 100)
Or, to leave only the latest 1000 records:
DELETE FROM mytable WHERE ROWID IN (SELECT ROWID FROM mytable ORDER BY ROWID DESC LIMIT -1 OFFSET 1000)
Assuming that your table has a Primary Key and a column with a timestamp indicating when the record was inserted), you can use a query along the lines of
delete from tableToDeleteFrom
where tablePK in
(select tablePK
from tableToDeleteFrom
where someThresholdDate <= #someThresholdDate)
For delete all records except the first record (min/max id) you can use:
SET #ls AS INT
SELECT #ls = MIN(id) FROM DATA
DELETE FROM DATA WHERE id <> #ls
i am running mysql and i would like to display the last 10 records that were added. what is the select statement for this?
If you have an auto-incrementing ID, you can do this:
SELECT * FROM my_table ORDER BY id DESC LIMIT 10;
If you don't, you'll need some criteria you can order by. An insertion date or something. The LIMIT 10 clause is what you're looking for here.
If you have an autoincrementing ID you can use this:
SELECT *
FROM yourtable
ORDER BY id DESC
LIMIT 10
If you have a column added_datetime that is set to the time of the insert then you can use that instead:
SELECT *
FROM yourtable
ORDER BY added_datetime DESC
LIMIT 10