質問

I have found several solutions for my type of problem, but I having trouble applying it in my situation.

Essentially I have a Vehicle Table:

License     VIN     Region
1           1       1
1           2       2
2           3       1
2           3       2
3           4       1
3           4       2
3           5       3

I want to take the license and vin from region 1 and see if the vin matches in all other regions based on the license. If it doesn't I want all the rows that don't match, but if it does match I don't want the row. So a complexity does come in when say I have 3 licenses and region 1 matches one row, but not the other, I want both the unmatched and region 1; however, when I have 3 licenses that all match I don't want any rows including region 1.

So my results in this case would be:

License     VIN     Region
1           1       1
1           2       2
3           4       1
3           5       3

I am using SQL Server 2005.

役に立ちましたか?

解決

I think this is what you're looking for

SELECT DISTINCT a.* 
FROM Vehicle AS a
INNER JOIN Vehicle AS b
ON a.License = b.License
WHERE a.VIN != b.VIN 
AND a.Region != b.Region 
AND (a.Region = 1 OR b.Region = 1)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top