|
-
Jul 25th, 2008, 11:41 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Query help
I am using sql server 2000.
I have a query like this to get 7 records:
Code:
select max(id),db_num from xPTS group by db_num
I need run another query to delete all records in table XPTS except this 7 records.
Can anyone help? Thanks.
-
Jul 25th, 2008, 11:47 AM
#2
Re: Query help
it's all depend upon whether your table is indexed or not and also depend upon no. of rows in table.
__________________
Rate the posts that helped you 
-
Jul 25th, 2008, 11:51 AM
#3
Thread Starter
Hyperactive Member
Re: Query help
 Originally Posted by riteshjain1982
it's all depend upon whether your table is indexed or not and also depend upon no. of rows in table.
Thanks. It is not indexexd and there are 1.5 m rows in table.
-
Jul 25th, 2008, 11:59 AM
#4
Re: Query help
i would say try putting PK constaint on column id (which i assume must be a unique/record) and if you alredy know these 7 records ID in advanced try to put it like
Code:
delete from xPTS
WHERE id not in (1,2,3,4,5,6,7)
also if possible for you to re-enter this 7 rows than simply use
Code:
TRUNCATE TABLE xPTS
and re-add above 7 rows,this will be much more faster.
__________________
Rate the posts that helped you 
-
Jul 25th, 2008, 12:06 PM
#5
Thread Starter
Hyperactive Member
Re: Query help
 Originally Posted by riteshjain1982
i would say try putting PK constaint on column id (which i assume must be a unique/record) and if you alredy know these 7 records ID in advanced try to put it like
Code:
delete from xPTS
WHERE id not in (1,2,3,4,5,6,7)
also if possible for you to re-enter this 7 rows than simply use
Code:
TRUNCATE TABLE xPTS
and re-add above 7 rows,this will be much more faster.
No, the ID is not a PK and the 7 rows changes everytime I run the query.
-
Jul 25th, 2008, 12:23 PM
#6
Re: Query help
May be you can try this
1>move these 7 rows to some Temp. table
2>Call TRUNCATE TABLE xPTS
3>Re-Move the Data from Temp Table to xPTS
4>DROP TempTable
No, the ID is not a PK and the 7 rows changes everytime I run the query.
wondering each time you 1.5 million rows ??
__________________
Rate the posts that helped you 
-
Jul 25th, 2008, 12:31 PM
#7
Thread Starter
Hyperactive Member
Re: Query help
 Originally Posted by riteshjain1982
May be you can try this
1>move these 7 rows to some Temp. table
2>Call TRUNCATE TABLE xPTS
3>Re-Move the Data from Temp Table to xPTS
4>DROP TempTable
No, the ID is not a PK and the 7 rows changes everytime I run the query.
wondering each time you 1.5 million rows ??
Yes,put them in temp table is a way. And also, I can use array to hold these Ids and then use them in 'delete' query. I just wonder if there is a better way--one query can solve all.
Thank you.
-
Jul 25th, 2008, 12:37 PM
#8
Re: Query help
ok so here is your query,Hope this is what you are looking for.
Code:
DELETE FROM xPTS
WHERE id NOT IN
(select max(id) from xPTS group by db_num)
EDIT: Sorry i overlook I need run another query to delete all records in table XPTS except this 7 records. In your first post
__________________
Rate the posts that helped you 
-
Jul 25th, 2008, 01:42 PM
#9
Thread Starter
Hyperactive Member
Re: Query help
Thank you, it worked great!
-
Jul 25th, 2008, 01:43 PM
#10
Re: Query help
 Originally Posted by lucia
Thank you, it worked great!
and sorry for all those post just becs. i missed last line of post #1
__________________
Rate the posts that helped you 
-
Jul 25th, 2008, 01:47 PM
#11
Thread Starter
Hyperactive Member
Re: Query help
 Originally Posted by riteshjain1982
 and sorry for all those post just becs. i missed last line of post #1 
I appreciated your time and help.
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
|