| Author |
Topic |
|
ronedin
Starting Member
29 Posts |
Posted - 2007-10-18 : 03:41:33
|
| Hi,I am writing a query to find nonmatching entries (empid) in 'both' the tables table1 and table2SELECT empid FROM(SELECT empid FROM table1 UNION ALL SELECT empid FROM table2) A GROUP BY empid HAVING COUNT(*) = 1It gives me the desired results. Problem : I want to delete the records physically now from the table1 and table2 which were returned by the above query.Something like :DELETE FROM table1,table2 where empid = (SELECT empid FROM(SELECT empid FROM table1 UNION ALL SELECT empid FROM table2) A GROUP BY empid HAVING COUNT(*) = 1)How to write this query? Please suggest me a query by keeping the inner query the same as it gives me the desired results.thanksthanks. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-18 : 03:54:45
|
You can only delete from one table at a given time.DECLARE @Sample TABLE (dt DATETIME, Price MONEY)SET DATEFORMAT DMYINSERT @SampleSELECT '16/10/2007', 16.75 UNION ALLSELECT '16/10/2007', 16.25 UNION ALLSELECT '12/10/2007', 15.7 UNION ALLSELECT '12/10/2007', 15.64 UNION ALLSELECT '12/10/2007', 15.17 UNION ALLSELECT '12/10/2007', 14.9 UNION ALLSELECT '12/10/2007', 14.75 UNION ALLSELECT '04/10/2007', 14.52 UNION ALLSELECT '04/10/2007', 14.84 UNION ALLSELECT '05/10/2007', 14.9 UNION ALLSELECT '01/10/2007', 14.84 UNION ALLSELECT '28/09/2007', 15.3SELECT * FROM @SampleDELETE fFROM ( SELECT ROW_NUMBER() OVER (PARTITION BY dt ORDER BY Price DESC) AS RecID FROM @Sample ) AS fWHERE RecID > 1SELECT * FROM @Sample E 12°55'05.25"N 56°04'39.16" |
 |
|
|
ronedin
Starting Member
29 Posts |
Posted - 2007-10-18 : 04:46:15
|
| Hi peso,I wrote thisDECLARE @table1 TABLE (empid int, empname nvarchar(20))DECLARE @table2 TABLE (empid int, empname nvarchar(20))INSERT @table1select 1,'emp1' UNION ALLselect 2,'emp2' UNION ALLselect 3,'emp3' UNION ALLselect 4,'emp4' UNION ALLselect 5,'emp5' UNION ALLselect 6,'emp6' UNION ALLselect 7,'emp7' UNION ALLselect 8,'emp8'INSERT @table2select 1,'emp1' UNION ALLselect 2,'emp2' UNION ALLselect 13,'emp3' UNION ALLselect 4,'emp4' UNION ALLselect 5,'emp5' UNION ALLselect 6,'emp6' UNION ALLselect 7,'emp7' UNION ALLselect 8,'emp8'The 3rd row in table1/table2 is not identical. So i want to delete that physically.I wrote the query like this but it doesnt work.Delete B FROM (SELECT empid FROM(SELECT empid FROM @table1 UNION ALL SELECT empid FROM @table2) A GROUP BY empid HAVING COUNT(*) = 1) as B thanks. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-18 : 04:50:28
|
quote: You can only delete from one table at a given time.
E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-18 : 04:52:02
|
[code]DECLARE @table1 TABLE (empid int, empname nvarchar(20))DECLARE @table2 TABLE (empid int, empname nvarchar(20))INSERT @table1select 1,'emp1' UNION ALLselect 2,'emp2' UNION ALLselect 3,'emp3' UNION ALLselect 4,'emp4' UNION ALLselect 5,'emp5' UNION ALLselect 6,'emp6' UNION ALLselect 7,'emp7' UNION ALLselect 8,'emp8'INSERT @table2select 1,'emp1' UNION ALLselect 2,'emp2' UNION ALLselect 13,'emp3' UNION ALLselect 4,'emp4' UNION ALLselect 5,'emp5' UNION ALLselect 6,'emp6' UNION ALLselect 7,'emp7' UNION ALLselect 8,'emp8'DELETE t1FROM @Table1 AS t1LEFT JOIN @Table2 AS t2 ON t2.empid = t1.empidwhere t2.empid is nullDELETE t2FROM @Table2 AS t2LEFT JOIN @Table1 AS t1 ON t1.empid = t2.empidwhere t1.empid is nullselect * from @table1select * from @table2[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-18 : 04:58:03
|
If you try something like thisDELETE fFROM ( SELECT COALESCE(t1.EmpID, t2.EmpID) AS EmpID FROM @Table1 AS t1 FULL JOIN @Table2 AS t2 ON t2.EmpID = t1.EmpID WHERE t1.EmpID IS NULL OR t2.EmpID IS NULL ) AS f you will get following error messageMsg 4405, Level 16, State 1, Line 24View or function 'f' is not updatable because the modification affects multiple base tables. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
ronedin
Starting Member
29 Posts |
Posted - 2007-10-18 : 05:02:27
|
| thanks peso..what does this message mean?can u recommend me something to learn t-sql..any book or article.. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-18 : 05:15:56
|
It means exactly what is says!quote: You can only delete from one table at a given time.
Yes!When you installed SQL Server, you also installed a help file named BOOKS ONLINE.Read it. It will become your best friend. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
ronedin
Starting Member
29 Posts |
Posted - 2007-10-18 : 05:38:42
|
| ok..i wished u could give me a book name..as every beginner would agree with me that BOL contains way too much info..anyways thanks for the help. |
 |
|
|
Kristen
Test
22859 Posts |
|
|
ronedin
Starting Member
29 Posts |
Posted - 2007-10-18 : 06:38:27
|
| wow!! this is a bookmark item..thanks kristen.. |
 |
|
|
|