jeudi 26 mai 2016

SQL INTERSECT with most matched results

I have a question related to SQL script:

If I do this: 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)

this will sort the results that most matched with Profile = 1 on top.

But if I add some "INTERSECT" clauses to construct multiple search, will pop up a error "Incorrect syntax near the keyword 'INTERSECT'"

the final clause is :

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)

The running SQL Server is SQL Server 2005. Could you tell me which part is wrong to cause this error and how to solve

this to make it work? Thanks.

Aucun commentaire:

Enregistrer un commentaire