Author |
Topic |
pOrmsby
Starting Member
12 Posts |
Posted - 2007-10-09 : 08:50:02
|
Hi Team,I am having difficultly with what I thought was an easy sql statement.Within a stored proc. I create a table variable like so:declare @Result TABLE (listid int, score bigint)I insert into the table various collections of data, which work fine. When I am finished populating @Result, the data looks like this:@Result:listid score------------------1805 10004302 5004322 5003211 1000etc. Each listid within @Result will also appear in a table called listcountry, and may have many entries. The listcountry table data can look like the following:listcountry:listID CountryID------------------1805 2251805 1551805 414302 154322 154322 224322 225 I want to remove the records from @Result if none of the joined up countryID match a given value.The value is supplied via a stored proc parameter - @ci int (value of 225)I have tried the following:DELETE rFROM @Result r INNER JOIN ListCountry ON r.ListId = ListCountry.ListIdWHERE ListCountry.CountryId <> @ciBut this seems to remove the corresponding records in @result alltogether.Any ideas? Your help will be much appreaciated.Many thanksPeter |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-09 : 08:59:51
|
[code]-- Prepare sample dataDECLARE @Result TABLE (ListID INT, Score BIGINT)INSERT @ResultSELECT 1805, 1000 UNION ALLSELECT 4302, 500 UNION ALLSELECT 4322, 500 UNION ALLSELECT 3211, 1000DECLARE @ListCountry TABLE (ListID INT, CountryID INT)INSERT @ListCountrySELECT 1805, 225 UNION ALLSELECT 1805, 155 UNION ALLSELECT 1805, 41 UNION ALLSELECT 4302, 15 UNION ALLSELECT 4322, 15 UNION ALLSELECT 4322, 22 UNION ALLSELECT 4322, 225-- Check preconditionSELECT * FROM @Result-- Delete unwanted recordsDECLARE @ci INTSET @ci = 225DELETE rFROM @Result AS rINNER JOIN ( SELECT ListID FROM @ListCountry GROUP BY ListID HAVING MAX(CASE WHEN CountryID = @ci THEN 1 ELSE 0 END) = 0 ) AS lc ON lc.ListId = r.ListId-- Check postconditionSELECT * FROM @Result[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-09 : 09:00:37
|
PreconditionListID Score1805 10004302 5004322 5003211 1000 PostconditionListID Score1805 10004322 5003211 1000 E 12°55'05.25"N 56°04'39.16" |
 |
|
pOrmsby
Starting Member
12 Posts |
Posted - 2007-10-09 : 09:07:52
|
Thanks PesoIt took just 9 minutes to get a reply that fixed it for me.Well done.This is the bit that I needed >>DELETE rFROM @Result AS rINNER JOIN ( SELECT ListID FROM @ListCountry GROUP BY ListID HAVING MAX(CASE WHEN CountryID = @ci THEN 1 ELSE 0 END) = 0 ) AS lc ON lc.ListId = r.ListId Many many thanksRegardsPeter |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-09 : 09:20:10
|
Of course. The first part is just to mimic your environment which I don't have access to. E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-09 : 09:24:22
|
Or this? This also removes the 3211 recordDELETE rFROM @Result AS rWHERE NOT EXISTS (SELECT * FROM @ListCountry AS lc WHERE lc.CountryID = @ci AND lc.ListID = r.ListID) E 12°55'05.25"N 56°04'39.16" |
 |
|
pOrmsby
Starting Member
12 Posts |
Posted - 2007-10-09 : 09:30:27
|
Yes, I need to also remove the 3211 record, I did not mean to exclude it from the listcountry table, it will be in there. If the listid is in the @result table and does not appear within the listcountry table with the corresponding countryid then it should be removed.There would your last post be more appropriate?Peter |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-09 : 09:34:57
|
Yes. E 12°55'05.25"N 56°04'39.16" |
 |
|
pOrmsby
Starting Member
12 Posts |
Posted - 2007-10-09 : 09:39:01
|
Its seems to remove all of them again?Previous code seems ok to me. |
 |
|
spirit1
Cybernetic Yak Master
11752 Posts |
|
|