September 14, 2006 at 3:54 am
Hello Everybody,
I am a Beginner in MS-SQL. I am facing a simple problem in deleting some records from a MS-SQL-2000-Server Database. Kindly go through the Contents of the Table and my question::
TABLE NAME: ABCD
ACCTNO LOTDATE BALANCE
--------------------------------
25 11-12-2005 500
25 13-12-2005 600
25 14-12-2005 450
26 01-05-2005 800
26 05-05-2005 900
26 15-05-2005 700
27 06-09-2005 600
27 08-09-2005 700
27 14-09-2005 800
No Primary or Foreign Key's are Difined.
Please see that the same record (eg: ACCTNO) is getting
repeated. The only difference is in the LOTDATE and
BALANCE.
Kindly give a solution (Script) to delete only those
repeating Records where the LOTDATE is smallest irrespective
of the Balance.
The Result should look like::
ACCTNO LOTDATE BALANCE
--------------------------------
25 14-12-2005 450
26 15-05-2005 700
27 14-09-2005 800
Thanks for a reply,
Barun Kumar Tiwari
INDIA
September 14, 2006 at 4:40 am
DELETE ABCD
FROM ABCD
INNER JOIN
(
SELECT ACCTNO,MAX(LOTDATE) AS MAX_DATE
FROM ABCD
GROUP BY ACCTNO
) AS MOST_RECENT
ON MOST_RECENT.ACCTNO=ABCD.ACCTNO
AND ABCD.LOTDATE<MOST_RECENT.MAX_DATE
should do it
September 18, 2006 at 12:36 am
Hello Jo,
Thanks for your reply. The Script u provided worked well. It can delete the required records. But Sir I am facing one more problem:
I need to send the remaining records (after deleting unwanted ones) to MS-Excel Spreadsheet. I am able to send it in CSV format directly from Query Analyser to Excel. But when the Records reach Excel, there the date (lotdat) coloumn shows only 00.00.0. The dates which are visible in the SELECT result set (after Delete) are not showing/coming up as it is. Now dates are visible in excel.
Please give a solution,
With regards, Bye
Barun Kumar Tiwari
INDIA
September 18, 2006 at 1:00 am
Buran Kumar Tiwari,
Can you make a seperate topic of the issue with CSV-Excel, eventually with a link to this post? The logdates showing as 00.00.0, are they displayed as real-dates in query analyzer?
September 18, 2006 at 1:20 am
It's just the excel formatting. Nothing wrong with the data
Highlight the cells, and apply one of the formats under date.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
September 19, 2006 at 6:37 am
Hello JO and GilaMonster,
Thanks for your co-operation and extending a helping hand for me. Jo's solution was OK. As GilaMOnster said, the problem was with the Excel Formating. I have changed the formating to dd/mm/yyyy, now everything is just fine.
Thanks.........alot.......Bye,
Barun Kumar Tiwari
INDIA
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply