DELETE MIN??

mercoledì 19 marzo 2008 - 16.08

aleplgr Profilo | Junior Member

Hi! I have a patient's table with 3 fields:
NUMPAT is the number of the patient
TREATMENT is a treatment code
DATE a date
There are many dates for each patient and for selecting the minimun date for each patient this SQL works fine:

SELECT T.NUMPAT,T.TREATMEN T,T.DATE
FROM MYTABLE T
WHERE
T.DATE=
(SELECT MIN(Q.DATE) FROM
MYTABLE Q WHERE T.NUMPAT = Q.NUMPAT )
order by T.NUMPAT

But for deleting the minimum date for each patient I'm trying this:
DELETE
FROM MYTABLE T
WHERE
T.DATE=
(SELECT MIN(Q.DATE) FROM
MYTABLE Q WHERE T.NUMPAT = Q.NUMPAT )

But deletes the whole table, I don't know why..
Sorry for the language

lbenaglia Profilo | Guru

>...But deletes the whole table, I don't know why..

Alex,

What RDBMS are you using?
Look at this code for SQL Server:

USE tempdb; CREATE TABLE dbo.Patients( NUMPAT int NOT NULL, TREATMENT varchar(15) NOT NULL, DATE datetime NOT NULL ); INSERT dbo.Patients VALUES(1, 'Treatment 1', '20080101'); INSERT dbo.Patients VALUES(1, 'Treatment 2', '20080102'); INSERT dbo.Patients VALUES(1, 'Treatment 3', '20080103'); INSERT dbo.Patients VALUES(2, 'Treatment 4', '20080201'); INSERT dbo.Patients VALUES(2, 'Treatment 5', '20080202'); INSERT dbo.Patients VALUES(3, 'Treatment 6', '20080301'); INSERT dbo.Patients VALUES(3, 'Treatment 7', '20080302'); INSERT dbo.Patients VALUES(3, 'Treatment 8', '20080303'); INSERT dbo.Patients VALUES(3, 'Treatment 9', '20080304'); DELETE dbo.Patients FROM dbo.Patients T WHERE T.DATE = ( SELECT MIN(Q.DATE) FROM dbo.Patients Q WHERE T.NUMPAT = Q.NUMPAT ); /* Output: (3 row(s) affected) */ SELECT * FROM dbo.Patients; /* Output: NUMPAT TREATMENT DATE ----------- --------------- ----------------------- 1 Treatment 2 2008-01-02 00:00:00.000 1 Treatment 3 2008-01-03 00:00:00.000 2 Treatment 5 2008-02-02 00:00:00.000 3 Treatment 7 2008-03-02 00:00:00.000 3 Treatment 8 2008-03-03 00:00:00.000 3 Treatment 9 2008-03-04 00:00:00.000 (6 row(s) affected) */ DROP TABLE dbo.Patients;

Ciao!
--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/
http://italy.mvps.org

aleplgr Profilo | Junior Member

It's very strange. My SQL is quite similar to yours:

DELETE
FROM
MYTABLE T
WHERE T.DATA = (
SELECT MIN(Q.DATA)
FROM MYTABLE Q
WHERE T.NUMPAC = Q.NUMPAC
)

But it DELETES the whole table, asi if the WHERE clause is useless, just clears all the table.
I'm in InterBase 6.5.

Your answer works fine as you show and it's correct and thanks for it, I tried to do the same but don't know why it's not working for me

lbenaglia Profilo | Guru

>I'm in InterBase 6.5.
Unfortunately I don't know InterBase...

Good luck

--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/
http://italy.mvps.org
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5