| | |
help with an sql statement
Please support our MS SQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2008
Posts: 5
Reputation:
Solved Threads: 0
Hi all,
This is my first time... and i'm not an sql guy by any means, but i can insert, update, blah blah...
I have a question to ask, i have a table that has
UniqueInt | Date1 | int1 | Date2 | varchar 1
I have duplicate records that i need to delete and i need to write an sql statement.
it goes like this,
UniqueInt | Date1 | int1 | Date2 | varchar 1
1 | 1/1/2008 10:00:00 am | 1 | 12/15/2007 10:00:00 am| 123456
2 | 1/1/2008 10:00:00 am | 1 | 12/15/2007 10:30:00 am| 123456
delete the first record where int1 =1 and varchar1 is the same in both records and the difference between date2 in both records is less than 1 minute.
I know how to do the less than a minute "(DATEDIFF(minute, field1, field2) < 1)" within one record, but i don't how to do compare the same field name in 2 records
Is that possible?
Thanks in advance
This is my first time... and i'm not an sql guy by any means, but i can insert, update, blah blah...
I have a question to ask, i have a table that has
UniqueInt | Date1 | int1 | Date2 | varchar 1
I have duplicate records that i need to delete and i need to write an sql statement.
it goes like this,
UniqueInt | Date1 | int1 | Date2 | varchar 1
1 | 1/1/2008 10:00:00 am | 1 | 12/15/2007 10:00:00 am| 123456
2 | 1/1/2008 10:00:00 am | 1 | 12/15/2007 10:30:00 am| 123456
delete the first record where int1 =1 and varchar1 is the same in both records and the difference between date2 in both records is less than 1 minute.
I know how to do the less than a minute "(DATEDIFF(minute, field1, field2) < 1)" within one record, but i don't how to do compare the same field name in 2 records
Is that possible?
Thanks in advance
Last edited by lebanese007; Jan 29th, 2008 at 4:27 pm.
I can play with SQL Server in this problem
1- define a trigger [on delete] let this trigger commit delete if deleted row > 1 and let it insert one of deleted rows into table again.
DELETE FROM table WHERE date1 in (SELECT date1 FROM table) AND varchar1 in (SELECT varchar1 FROM table
that's not the optimal solution but I think it works, I just wake up now and found this question.
1- define a trigger [on delete] let this trigger commit delete if deleted row > 1 and let it insert one of deleted rows into table again.
DELETE FROM table WHERE date1 in (SELECT date1 FROM table) AND varchar1 in (SELECT varchar1 FROM table
that's not the optimal solution but I think it works, I just wake up now and found this question.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: May 2007
Posts: 8
Reputation:
Solved Threads: 0
Hi lebanese007,
If you are wanting a script that you can run to clean up the data in the table you can try the solution below. If this is an ongoing issue then it may be worth looking at putting a check during the insert to stop these additional rows. This may not be the best performance if the inserts are very frequent.
Anyway, here is a sample solution. I have simplified the table to the relevant parts only.
Let me know how you get on with that.
Zadjil
If you are wanting a script that you can run to clean up the data in the table you can try the solution below. If this is an ongoing issue then it may be worth looking at putting a check during the insert to stop these additional rows. This may not be the best performance if the inserts are very frequent.
Anyway, here is a sample solution. I have simplified the table to the relevant parts only.
MS SQL Syntax (Toggle Plain Text)
CREATE TABLE #test (ID INT, val INT, txt VARCHAR(50), dt DATETIME) INSERT INTO #test VALUES (1, 10, 'a', '2008-1-31 12:00:00') INSERT INTO #test VALUES (2, 10, 'a', '2008-1-31 12:00:30') INSERT INTO #test VALUES (3, 11, 'b', '2008-1-31 12:00:00') INSERT INTO #test VALUES (4, 10, 'a', '2008-1-31 12:02:00') SELECT * FROM #test DELETE FROM later --Delete the later result FROM --Join the table to itself where the values match #test later INNER JOIN #test ealier ON later.val = ealier.val and later.txt = ealier.txt WHERE -- make sure the ones in the ealier table are before the ones in the later table (stops the rows matching themselves) ealier.dt < later.dt AND -- make sure the later date is less(or equal) than one minute from the earlier date later.dt <= DATEADD(minute,1, ealier.dt) SELECT * FROM #test DROP TABLE #test
Let me know how you get on with that.
Zadjil
•
•
Join Date: Jan 2008
Posts: 2,052
Reputation:
Solved Threads: 123
•
•
•
•
Hi all,
This is my first time... and i'm not an sql guy by any means, but i can insert, update, blah blah...
I have a question to ask, i have a table that has
UniqueInt | Date1 | int1 | Date2 | varchar 1
I have duplicate records that i need to delete and i need to write an sql statement.
it goes like this,
UniqueInt | Date1 | int1 | Date2 | varchar 1
1 | 1/1/2008 10:00:00 am | 1 | 12/15/2007 10:00:00 am| 123456
2 | 1/1/2008 10:00:00 am | 1 | 12/15/2007 10:30:00 am| 123456
delete the first record where int1 =1 and varchar1 is the same in both records and the difference between date2 in both records is less than 1 minute.
I know how to do the less than a minute "(DATEDIFF(minute, field1, field2) < 1)" within one record, but i don't how to do compare the same field name in 2 records
Is that possible?
Thanks in advance
you can do such things using cursor as follows :
declare @varchar1 nvarchar(50)
declare mycursor cursor for
select varchar1,
from mytable
group by varchar1
having count(*) > 1
open mycursor
FETCH NEXT FROM mycursor INTO @varchar1
WHILE @@FETCH_STATUS = 0
begin
delete from mytable where (@varchar1=varchar1 and int1=1)
FETCH NEXT FROM mycursor INTO @varchar1
end
close mycursor
deallocate mycursor
Due to lack of freedom of speech, i no longer post on this website.
•
•
Join Date: Jan 2008
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
Hi lebanese007,
If you are wanting a script that you can run to clean up the data in the table you can try the solution below. If this is an ongoing issue then it may be worth looking at putting a check during the insert to stop these additional rows. This may not be the best performance if the inserts are very frequent.
Anyway, here is a sample solution. I have simplified the table to the relevant parts only.
MS SQL Syntax (Toggle Plain Text)
CREATE TABLE #test (ID INT, val INT, txt VARCHAR(50), dt DATETIME) INSERT INTO #test VALUES (1, 10, 'a', '2008-1-31 12:00:00') INSERT INTO #test VALUES (2, 10, 'a', '2008-1-31 12:00:30') INSERT INTO #test VALUES (3, 11, 'b', '2008-1-31 12:00:00') INSERT INTO #test VALUES (4, 10, 'a', '2008-1-31 12:02:00') SELECT * FROM #test DELETE FROM later --Delete the later result FROM --Join the table to itself where the values match #test later INNER JOIN #test ealier ON later.val = ealier.val and later.txt = ealier.txt WHERE -- make sure the ones in the ealier table are before the ones in the later table (stops the rows matching themselves) ealier.dt < later.dt AND -- make sure the later date is less(or equal) than one minute from the earlier date later.dt <= DATEADD(minute,1, ealier.dt) SELECT * FROM #test DROP TABLE #test
Let me know how you get on with that.
Zadjil
Zadj,
thanks so much for your help, but with all that i got lost... can you please reflect my table with your code with the whole sql statement?
for your question, yes, i'm trying to clean out the table, it is not an ongoing problem anymore, so i just want to clean up this table.
Thanks alot man.
•
•
Join Date: Jan 2008
Posts: 2,052
Reputation:
Solved Threads: 123
by the way, my code erases all the rows having duplicate values in varchar1 column and value of 1 for the int1 column. if you want to delete the first row, then you have to set a break statement in the cursor as follows :
begin
delete top (1) from mytable where (@varchar1=varchar1 and int1=1)
break
FETCH NEXT FROM mycursor INTO @varchar1
end
begin
delete top (1) from mytable where (@varchar1=varchar1 and int1=1)
break
FETCH NEXT FROM mycursor INTO @varchar1
end
Due to lack of freedom of speech, i no longer post on this website.
•
•
Join Date: Jan 2008
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
by the way, my code erases all the rows having duplicate values in varchar1 column and value of 1 for the int1 column. if you want to delete the first row, then you have to set a break statement in the cursor as follows :
begin
delete top (1) from mytable where (@varchar1=varchar1 and int1=1)
break
FETCH NEXT FROM mycursor INTO @varchar1
end
can you please use pure sql, i don't know what my cursor means.
Thanks in advance
•
•
Join Date: Jan 2008
Posts: 2,052
Reputation:
Solved Threads: 123
and if you want to delete only first rows of the grouped row sets, just remove the break statement from the code above as follows
begin
delete top (1) from mytable where (@varchar1=varchar1 and int1=1)
FETCH NEXT FROM mycursor INTO @varchar1
end
begin
delete top (1) from mytable where (@varchar1=varchar1 and int1=1)
FETCH NEXT FROM mycursor INTO @varchar1
end
Due to lack of freedom of speech, i no longer post on this website.
•
•
Join Date: Jan 2008
Posts: 2,052
Reputation:
Solved Threads: 123
Cursor is a reserved keyword of t-sql for the ms sql server. Cursor is a built-in structure in ms sql's sql specification. Cursors work similar to foreach statements in c#, so if you want to do sequential operations on tables, you can use cursors. if you want to do some repeatition of commands in sql, using cursor is the best way to achieve that.
Due to lack of freedom of speech, i no longer post on this website.
![]() |
Similar Threads
- is there something wrong with my SQL statement? (PHP)
- Syntax error in "like" in sql statement (ASP.NET)
- Question about SQL statement (MS SQL)
- SQL statement error (ASP.NET)
- sql statement (Visual Basic 4 / 5 / 6)
- run sql statement in asp (ASP)
Other Threads in the MS SQL Forum
- Previous Thread: Complex Store Procedure
- Next Thread: questions in views and some others
Views: 2476 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last age autogrowth business connectingtodatabaseinuse count cursor data database dateadd datediff datepart day" dbsize deadlock delete_trigger exploit getdate hack highperformancecomputing hpc hpcserver2008 ibm iis limit live loop maximum microsoft ms mssql multiple multithreading news number password permission position query reporting result security server services sets single source sql sql-injection sqlserver sqlserver2005 subtype supercomputing supertype tables uniqueid update view weekday






