help with an sql statement

Reply

Join Date: Jan 2008
Posts: 5
Reputation: lebanese007 is an unknown quantity at this point 
Solved Threads: 0
lebanese007 lebanese007 is offline Offline
Newbie Poster

help with a sql statement

 
0
  #1
Jan 29th, 2008
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
Last edited by lebanese007; Jan 29th, 2008 at 4:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: help with an sql statement

 
0
  #2
Jan 29th, 2008
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.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 5
Reputation: lebanese007 is an unknown quantity at this point 
Solved Threads: 0
lebanese007 lebanese007 is offline Offline
Newbie Poster

Re: help with an sql statement

 
0
  #3
Jan 30th, 2008
Hi Ramy,
when i tried your query, it erased all the records in the table. and i don't see where i'm specifying if Date2 is less than one minute!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 8
Reputation: Zadj is an unknown quantity at this point 
Solved Threads: 0
Zadj Zadj is offline Offline
Newbie Poster

Re: help with an sql statement

 
0
  #4
Jan 31st, 2008
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.
  1. CREATE TABLE #test
  2. (ID INT,
  3. val INT,
  4. txt VARCHAR(50),
  5. dt DATETIME)
  6.  
  7. INSERT INTO #test
  8. VALUES (1, 10, 'a', '2008-1-31 12:00:00')
  9. INSERT INTO #test
  10. VALUES (2, 10, 'a', '2008-1-31 12:00:30')
  11. INSERT INTO #test
  12. VALUES (3, 11, 'b', '2008-1-31 12:00:00')
  13. INSERT INTO #test
  14. VALUES (4, 10, 'a', '2008-1-31 12:02:00')
  15.  
  16. SELECT * FROM #test
  17.  
  18. DELETE FROM later --Delete the later result
  19. FROM
  20. --Join the table to itself where the values match
  21. #test later INNER JOIN
  22. #test ealier ON later.val = ealier.val and later.txt = ealier.txt
  23. WHERE
  24. -- make sure the ones in the ealier table are before the ones in the later table (stops the rows matching themselves)
  25. ealier.dt < later.dt AND
  26. -- make sure the later date is less(or equal) than one minute from the earlier date
  27. later.dt <= DATEADD(minute,1, ealier.dt)
  28.  
  29.  
  30. SELECT * FROM #test
  31.  
  32. DROP TABLE #test

Let me know how you get on with that.

Zadjil
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 123
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: help with a sql statement

 
0
  #5
Jan 31st, 2008
Originally Posted by lebanese007 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 5
Reputation: lebanese007 is an unknown quantity at this point 
Solved Threads: 0
lebanese007 lebanese007 is offline Offline
Newbie Poster

Re: help with an sql statement

 
0
  #6
Jan 31st, 2008
Originally Posted by Zadj View Post
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.
  1. CREATE TABLE #test
  2. (ID INT,
  3. val INT,
  4. txt VARCHAR(50),
  5. dt DATETIME)
  6.  
  7. INSERT INTO #test
  8. VALUES (1, 10, 'a', '2008-1-31 12:00:00')
  9. INSERT INTO #test
  10. VALUES (2, 10, 'a', '2008-1-31 12:00:30')
  11. INSERT INTO #test
  12. VALUES (3, 11, 'b', '2008-1-31 12:00:00')
  13. INSERT INTO #test
  14. VALUES (4, 10, 'a', '2008-1-31 12:02:00')
  15.  
  16. SELECT * FROM #test
  17.  
  18. DELETE FROM later --Delete the later result
  19. FROM
  20. --Join the table to itself where the values match
  21. #test later INNER JOIN
  22. #test ealier ON later.val = ealier.val and later.txt = ealier.txt
  23. WHERE
  24. -- make sure the ones in the ealier table are before the ones in the later table (stops the rows matching themselves)
  25. ealier.dt < later.dt AND
  26. -- make sure the later date is less(or equal) than one minute from the earlier date
  27. later.dt <= DATEADD(minute,1, ealier.dt)
  28.  
  29.  
  30. SELECT * FROM #test
  31.  
  32. 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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 123
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: help with an sql statement

 
0
  #7
Jan 31st, 2008
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
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 5
Reputation: lebanese007 is an unknown quantity at this point 
Solved Threads: 0
lebanese007 lebanese007 is offline Offline
Newbie Poster

Re: help with an sql statement

 
0
  #8
Jan 31st, 2008
Originally Posted by serkansendur View Post
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
Hi serkan
can you please use pure sql, i don't know what my cursor means.

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 123
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: help with an sql statement

 
0
  #9
Jan 31st, 2008
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
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 123
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: help with an sql statement

 
0
  #10
Jan 31st, 2008
Originally Posted by lebanese007 View Post
Hi serkan
can you please use pure sql, i don't know what my cursor means.

Thanks in advance
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2476 | Replies: 13
Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC