PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.sqlserver.server > DELETE
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
DELETE

Réponse
 
LinkBack Outils de la discussion
Vieux 19/05/2008, 18h33   #1
morphius
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut DELETE

I need to delete ID '1' and '3' using SQL:
ID Customer DateTime
1 1 3/06/2005 03:00:00
2 1 3/06/2005 00:00:00
3 2 3/06/2005 03:00:00
4 2 3/06/2005 00:00:00

Thanks..
  Réponse avec citation
Vieux 19/05/2008, 18h59   #2
Plamen Ratchev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: DELETE

Try:

DELETE MyTable
WHERE id IN (1, 3);

HTH,

Plamen Ratchev
http://www.SQLStudio.com
  Réponse avec citation
Vieux 19/05/2008, 19h03   #3
Aaron Bertrand [SQL Server MVP]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: DELETE

I assume you don't just want

DELETE table_name WHERE ID IN (1,3)

Could you please indicate what version of SQL Server you are using? Also
what do you want to delete if there is another row:

5 2 3/06/2005 02:00

?

Here is answer that assumes SQL Server 2005, and also assumes you only want
to delete the newest row (as opposed to keeping only the oldest row, which
is different).


CREATE TABLE #foo(ID INT, Customer INT, [DateTime] DATETIME);

INSERT #foo SELECT 1,1,'20050306 03:00'
UNION ALL SELECT 2,1,'20050306'
UNION ALL SELECT 3,2,'20050306 03:00'
UNION ALL SELECT 4,2,'20050306';

SELECT * FROM #foo;

WITH RowsToDelete AS
(
SELECT ID FROM
(
SELECT ID,Customer,[DateTime],
[row_number] = ROW_NUMBER() OVER
(
PARTITION BY Customer
ORDER BY [DateTime] DESC
)
FROM #foo
) x
WHERE [row_number] = 1
)
DELETE RowsToDelete;

SELECT * FROM #foo;

DROP TABLE #foo;


However, please don't make us assume so much.




"morphius" <morphius@discussions.microsoft.com> wrote in message
news:0FF88A24-4B80-458D-A20E-92A2AF83A8BE@microsoft.com...
>I need to delete ID '1' and '3' using SQL:
> ID Customer DateTime
> 1 1 3/06/2005 03:00:00
> 2 1 3/06/2005 00:00:00
> 3 2 3/06/2005 03:00:00
> 4 2 3/06/2005 00:00:00
>
> Thanks..


  Réponse avec citation
Vieux 19/05/2008, 20h08   #4
morphius
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: DELETE

Aaron, your assumption is correct. I just dont want to delete (1,3). I want
to keep the oldest row which is the one with 00:00:00. I am using SQL server
2000. Thanks for the .


"Aaron Bertrand [SQL Server MVP]" wrote:

> I assume you don't just want
>
> DELETE table_name WHERE ID IN (1,3)
>
> Could you please indicate what version of SQL Server you are using? Also
> what do you want to delete if there is another row:
>
> 5 2 3/06/2005 02:00
>
> ?
>
> Here is answer that assumes SQL Server 2005, and also assumes you only want
> to delete the newest row (as opposed to keeping only the oldest row, which
> is different).
>
>
> CREATE TABLE #foo(ID INT, Customer INT, [DateTime] DATETIME);
>
> INSERT #foo SELECT 1,1,'20050306 03:00'
> UNION ALL SELECT 2,1,'20050306'
> UNION ALL SELECT 3,2,'20050306 03:00'
> UNION ALL SELECT 4,2,'20050306';
>
> SELECT * FROM #foo;
>
> WITH RowsToDelete AS
> (
> SELECT ID FROM
> (
> SELECT ID,Customer,[DateTime],
> [row_number] = ROW_NUMBER() OVER
> (
> PARTITION BY Customer
> ORDER BY [DateTime] DESC
> )
> FROM #foo
> ) x
> WHERE [row_number] = 1
> )
> DELETE RowsToDelete;
>
> SELECT * FROM #foo;
>
> DROP TABLE #foo;
>
>
> However, please don't make us assume so much.
>
>
>
>
> "morphius" <morphius@discussions.microsoft.com> wrote in message
> news:0FF88A24-4B80-458D-A20E-92A2AF83A8BE@microsoft.com...
> >I need to delete ID '1' and '3' using SQL:
> > ID Customer DateTime
> > 1 1 3/06/2005 03:00:00
> > 2 1 3/06/2005 00:00:00
> > 3 2 3/06/2005 03:00:00
> > 4 2 3/06/2005 00:00:00
> >
> > Thanks..

>

  Réponse avec citation
Vieux 19/05/2008, 20h15   #5
Aaron Bertrand [SQL Server MVP]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: DELETE

Then I would probably solve the problem this way. Selecting into a work
table first will allow you to inspect the results... e.g. are these the rows
I want to keep?



SELECT ID, Customer, dt = MIN([DateTime])
INTO #foo
FROM table_name
WHERE ID IS NOT NULL
AND Customer IS NOT NULL
GROUP BY ID, Customer;

SELECT * FROM #foo;

-- if the above are the results you want to keep,
-- then uncomment this chunk and run again:

/*
DELETE table_name
WHERE ID NOT IN
(
SELECT ID FROM #foo
);
*/

DROP TABLE #foo;



In the future please give clearer requirements and always indicate what
version(s) you need to target. It prevents people like me from doing a
whole bunch of work for nothing (and gets you your answer quicker, too).






"morphius" <morphius@discussions.microsoft.com> wrote in message
news:E1F0B299-41C8-4988-A7E4-7EAC5F50C29A@microsoft.com...
> Aaron, your assumption is correct. I just dont want to delete (1,3). I
> want
> to keep the oldest row which is the one with 00:00:00. I am using SQL
> server
> 2000. Thanks for the .


  Réponse avec citation
Vieux 19/05/2008, 20h20   #6
Aaron Bertrand [SQL Server MVP]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: DELETE

Sorry, disregard, let me actually try it first.
  Réponse avec citation
Vieux 19/05/2008, 20h26   #7
Aaron Bertrand [SQL Server MVP]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: DELETE

Again, I have been way too spoiled by CTEs, and forget how to do this kind
of thing the old way.

Note that you may keep multiple results if you have ties with the same
customer having multiple rows having the same [DateTime] value. Bad name
for a column, by the way (well, so is ID, but DateTime is a data type).


CREATE TABLE #foo(ID INT, Customer INT, [DateTime] DATETIME);

INSERT #foo SELECT 1,1,'20050306 03:00'
UNION ALL SELECT 2,1,'20050306'
UNION ALL SELECT 3,2,'20050306 03:00'
UNION ALL SELECT 4,2,'20050306';

SELECT * FROM #foo;


SELECT ID
INTO #bar
FROM #foo f1
INNER JOIN
(
SELECT Customer, dt = MIN([DateTime])
FROM #foo
GROUP BY Customer
) f2
ON f1.Customer = f2.Customer
AND f1.[DateTime] = f2.dt;

SELECT * FROM #bar;

-- if the above are the results you want to keep,
-- then uncomment this chunk and run again:

/*
DELETE table_name
WHERE ID NOT IN
(
SELECT ID FROM #bar
);
*/

DROP TABLE #bar, #foo;



--A

  Réponse avec citation
Vieux 19/05/2008, 21h44   #8
Plamen Ratchev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: DELETE

Here is alternative with single query:

DELETE #foo
WHERE NOT EXISTS
(SELECT *
FROM #foo AS B
WHERE B.customer = #foo.customer
AND B.[datetime] > #foo.[datetime]);

Plamen Ratchev
http://www.SQLStudio.com
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h21.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18124 seconds with 16 queries