Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete certain records in table


Delete certain records in table

Author
Message
Mr. Newbie
Mr. Newbie
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 30
Hello,

This is my first post in this forum. Hope you will have patience on me since I am not SQL expert.

I currently have been thinking, how can I delete records in a table that is being returned by a select statement?

SELECT  A.intFactoryID, RTRIM(A.strSAPAsset#) strSAPAsset# , RTRIM(A.strConnector) strConnector, A.lngDate, RTRIM(A.strShift) strShift, 
LTRIM(RTRIM(A.strAlarmText)) strAlarmText, A.intAlarmCnt
FROM dbo.tblAlarmMaster A
inner join
(select [intFactoryID],[strSAPAsset#],[lngDate],[strShift],[strAlarmText]
from [tblAlarmMaster]
group by [intFactoryID],[strSAPAsset#],[lngDate],[strShift],[strAlarmText], intAlarmCnt
having count([intFactoryID]) > 1) B
on A.[intFactoryID] = B.[intFactoryID]
AND A.[strSAPAsset#] = B.[strSAPAsset#]
AND A.[lngDate] = B.[lngDate]
AND A.[strShift] = B.[strShift]
AND A.[strAlarmText] = B.[strAlarmText]
order by A.[intFactoryID],A.[strSAPAsset#],A.[lngDate],A.[strShift],A.[strAlarmText]



I currently have this select statement, what I want is that I would like to delete the records returned by this select statement on my same table
tblAlarmMaster.

The keys in my tables are, intFactoryID,strSAPAsset, lngDate,strShift and strAlarmText.

I am not sure how to create the delete statement part. I have just been using only certain simple delete statements until I was face with this task. Hope someone will give pointers. Thank you.
Ghanta
Ghanta
SSC Veteran
SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)

Group: General Forum Members
Points: 233 Visits: 698
Use the key to join the table to this subquery and delete .... one way to do that
drew.allen
drew.allen
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2698 Visits: 9978
I prefer using
DELETE
FROM [TableName]
WHERE RowID IN (
<Subquery here>
)



I like this, because it's very clear which table you are deleting rows from.

Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
Amit-1103237
Amit-1103237
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 41
Allen really gave a very precise and to the point solution.

But Newbie as u seem to be new this can be helpful to you to certain extent.....

Delete from dbo.tblAlarmMaster
where intFactoryID in
(
SELECT A.intFactoryID
FROM dbo.tblAlarmMaster A
inner join
(select [intFactoryID],[strSAPAsset#],[lngDate],[strShift],[strAlarmText]
from [tblAlarmMaster]
group by [intFactoryID],[strSAPAsset#],[lngDate],[strShift],[strAlarmText], intAlarmCnt
having count([intFactoryID]) > 1) B
on A.[intFactoryID] = B.[intFactoryID]
AND A.[strSAPAsset#] = B.[strSAPAsset#]
AND A.[lngDate] = B.[lngDate]
AND A.[strShift] = B.[strShift]
AND A.[strAlarmText] = B.[strAlarmText]
order by A.[intFactoryID],A.[strSAPAsset#],A.[lngDate],A.[strShift],A.[strAlarmText])

Hope you will get the result as desired by you.

Thanks,
Amit
Mr. Newbie
Mr. Newbie
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 30
Hi All,

Thanks to your inputs, I was able to do what I want.

DELETE TABLE
FROM TABLE
JOIN (SUBQUERY)



I think this was how I first perceived the SQL statements but I was surprised that there was a RowID field in MSSQL 2k5.

I googled for it and tried it also, its fast and concise. Thanks for your help again . I might post other questions though later (because of all of you being friendly) :-D
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search