SQL INTERSECT 与最匹配的结果
SQL INTERSECT with most matched results
我有一个与 SQL 脚本相关的问题:
如果我这样做:
DECLARE @ProfileID int
SET @ProfileID = 1
SELECT [SetID],[ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileConditionTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [ConditionID] = 1 AND [CondMin] between 0 and 0 AND [CondMax] between 1000 and 1000)
group by [SetID], [ProfileID]
ORDER BY ABS([ProfileID] - @ProfileID)
这会将与 Profile = 1 最匹配的结果排在最前面。
但是如果我加一些INTERSECT
子句构造多重搜索,会报错"Incorrect syntax near the keyword 'INTERSECT'"
最后一个子句是:
DECLARE @ProfileID int
SET @ProfileID = 1
SELECT [SetID],[ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileConditionTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [ConditionID] = 1 AND [CondMin] between 0 and 0 AND [CondMax] between 1000 and 1000)
group by [SetID], [ProfileID]
ORDER BY ABS([ProfileID] - @ProfileID)
INTERSECT
SELECT [SetID], [ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileConditionTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [ConditionID] = 3 AND [CondMin] between 0 and 0 AND [CondMax] between 200 and 200)
group by [SetID], [ProfileID]
ORDER BY ABS([ProfileID] - @ProfileID)
INTERSECT
SELECT [SetID], [ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileParameterSetTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [BondClassificationID] = 0 AND [BondObjectID] = 0)
group by [SetID], [ProfileID]
ORDER BY ABS([ProfileID] - @ProfileID)
运行SQL服务器是SQLServer 2005,请问是哪部分的错误导致了这个错误,如何解决
这样才能让它发挥作用?谢谢
比我最初想象的要复杂一点,但这应该可行(虽然我没有测试...):
DECLARE @ProfileID int
SET @ProfileID = 1
SELECT [SetID],[ProfileID]
FROM (
SELECT [SetID],[ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileConditionTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [ConditionID] = 1 AND [CondMin] between 0 and 0 AND [CondMax] between 1000 and 1000)
group by [SetID], [ProfileID]
INTERSECT
SELECT [SetID], [ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileConditionTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [ConditionID] = 3 AND [CondMin] between 0 and 0 AND [CondMax] between 200 and 200)
group by [SetID], [ProfileID]
INTERSECT
SELECT [SetID], [ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileParameterSetTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [BondClassificationID] = 0 AND [BondObjectID] = 0)
group by [SetID], [ProfileID]
) a
ORDER BY ABS([ProfileID] - @ProfileID)
我有一个与 SQL 脚本相关的问题:
如果我这样做:
DECLARE @ProfileID int
SET @ProfileID = 1
SELECT [SetID],[ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileConditionTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [ConditionID] = 1 AND [CondMin] between 0 and 0 AND [CondMax] between 1000 and 1000)
group by [SetID], [ProfileID]
ORDER BY ABS([ProfileID] - @ProfileID)
这会将与 Profile = 1 最匹配的结果排在最前面。
但是如果我加一些INTERSECT
子句构造多重搜索,会报错"Incorrect syntax near the keyword 'INTERSECT'"
最后一个子句是:
DECLARE @ProfileID int
SET @ProfileID = 1
SELECT [SetID],[ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileConditionTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [ConditionID] = 1 AND [CondMin] between 0 and 0 AND [CondMax] between 1000 and 1000)
group by [SetID], [ProfileID]
ORDER BY ABS([ProfileID] - @ProfileID)
INTERSECT
SELECT [SetID], [ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileConditionTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [ConditionID] = 3 AND [CondMin] between 0 and 0 AND [CondMax] between 200 and 200)
group by [SetID], [ProfileID]
ORDER BY ABS([ProfileID] - @ProfileID)
INTERSECT
SELECT [SetID], [ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileParameterSetTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [BondClassificationID] = 0 AND [BondObjectID] = 0)
group by [SetID], [ProfileID]
ORDER BY ABS([ProfileID] - @ProfileID)
运行SQL服务器是SQLServer 2005,请问是哪部分的错误导致了这个错误,如何解决
这样才能让它发挥作用?谢谢
比我最初想象的要复杂一点,但这应该可行(虽然我没有测试...):
DECLARE @ProfileID int
SET @ProfileID = 1
SELECT [SetID],[ProfileID]
FROM (
SELECT [SetID],[ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileConditionTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [ConditionID] = 1 AND [CondMin] between 0 and 0 AND [CondMax] between 1000 and 1000)
group by [SetID], [ProfileID]
INTERSECT
SELECT [SetID], [ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileConditionTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [ConditionID] = 3 AND [CondMin] between 0 and 0 AND [CondMax] between 200 and 200)
group by [SetID], [ProfileID]
INTERSECT
SELECT [SetID], [ProfileID]
FROM [GeneralizedTable].[dbo].[DBProfileParameterSetTable]
where ([DBTypeID] = 4 AND ([ProfileID] = 1 OR [ProfileID] = 3 OR [ProfileID] = 13) AND [BondClassificationID] = 0 AND [BondObjectID] = 0)
group by [SetID], [ProfileID]
) a
ORDER BY ABS([ProfileID] - @ProfileID)