Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 17th, 2007, 01:08 PM
Registered User
 
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Delete by Date

STORE_NUM UPC COST TYPE EFFECTIVE_DATE
48 1060962862 .6053 1 03/19/06
10 1060962862 .6053 1 03/19/06
48 1060962862 .6340 1 05/06/07
48 7047918000 2.7500 1 11/22/06
48 7824301862 1.4800 1 07/20/05
10 4133040880 1.1800 1 10/17/06
48 4133040880 1.1800 1 10/17/06
10 7830009900 4.4383 1 11/19/06
10 1060962862 .6340 1 05/06/07

From the above table, I am hoping someone can get me going in the right direction. I need to remove rows that have the same STORE_NUM and UPC by using the EFFECTIVE_DATE. I need to keep the row with the most recent EFFECTIVE_DATE.
Thank you,

Tad
 
Old May 18th, 2007, 12:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

So which is the primary key column here? I dont think you have one. You may have to write a cursor to accomplish this.

_________________________
- Vijay G
Strive for Perfection
 
Old May 18th, 2007, 07:02 AM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I was thinking you might be able to do the same thing with a CTE (Common Table Expession) but without a Primary Key, it is very difficult.

While in most cases i don't like cursors, I think that is your best bet in this case.

If the table has a primary key, i'll show you how to do it very easily with a CTE.

Scott Klein
Author - Professional SQL Server 2005 XML
http://www.wrox.com/WileyCDA/WroxTit...764597922.html
 
Old May 18th, 2007, 07:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yes, I was thinking it the same way. Else the OP will have to hit the cursors for doing this.

_________________________
- Vijay G
Strive for Perfection
 
Old May 18th, 2007, 11:28 AM
Registered User
 
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No primary key. I found, and modified, some code which uses a cursor similar to what I need. I ran the query, but it kept running for about 4-5 minutes so I figured I must have an endless loop. Here is the code I used:

EXECUTE ('ALTER TABLE dbo.Cost_Import_TEMP
 ADD FLAG_MAX_EDATE varchar(50)
        ')



--Declare variables that hold record fetched from cursor
EXECUTE ('DECLARE @STORE varchar(50), @UPC varchar(50)

--Declare cursor
DECLARE STOREAndUPC_Cursor CURSOR FOR
SELECT DISTINCT STORE, UPC
FROM dbo.Cost_Import_TEMP

--Open cursor
OPEN STOREAndUPC_Cursor

--Place values for STORE and UPC from first record into variables
FETCH NEXT FROM STOREAndUPC_Cursor
INTO @STORE, @UPC

--Begin loop which iterates through all records in cursor
WHILE @@FETCH_STATUS = 0

      BEGIN

--Update flag used to identify duplicate records with the most current effective date
UPDATE Cost_Import_TEMP
SET FLAG_MAX_EDate = 1
WHERE STORE = @STORE
AND UPC = @UPC
AND CONVERT(datetime, EffectiveDate) IN (SELECT MAX(CONVERT(datetime, EffectiveDate))
                                      FROM Cost_Import_TEMP
                                      WHERE STORE = @STORE
                                      AND UPC = @UPC)


--Place values for Zone and UPC from next record into variables
FETCH NEXT FROM STOREAndUPC_Cursor
INTO @STORE, @UPC

     END --End Loop

--Close and deallocate cursor so it can be used again
CLOSE STOREAndUPC_Cursor
DEALLOCATE STOREAndUPC_Cursor


--Insert filtered out records with the most current effective date back to import table
INSERT INTO Cost_Import (STORE, UPC, Cost, EffectiveDate)
SELECT DISTINCT STORE, UPC, Cost, EffectiveDate
FROM Cost_Import_TEMP
WHERE IsMaxEffectiveDate = 1



     ')

Tad
 
Old May 21st, 2007, 12:19 AM
Authorized User
 
Join Date: Jan 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to hossrad
Default

hi try this:

delete from table1 t1
where (storenum+upc+cost+type) not in (select top 1 storenum+upc+cost+type from table1 where t1.storenum+t1.upc+t1.tcost+t1.type = storenum+upc+cost+type order by effectdate desc)
and t1.storenum+t1.upc in(
SELECT StoreNum+Upc,Count(*) FROM Table1
GROUP BY StoreNum,Upc
having count(*)>1
)


but be sure that you cast each of the strenum , .... as varchar before concating!


Be Sure,
Hossein





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to delete file System.IO.Delete error maricar C# 13 March 14th, 2014 06:50 AM
php/mysql delete button and delete query dungey PHP Databases 17 April 11th, 2009 12:24 PM
Auto delete by date XXL PHP How-To 1 March 29th, 2006 01:32 AM
delete data when date expires dannyphw Classic ASP Databases 6 October 27th, 2004 12:34 AM
how to delete a row when click delete hyperlink naveenkumarg1 Pro JSP 1 August 16th, 2004 01:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.