Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Different result from SQL
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Yak Posting Veteran

65 Posts

Posted - 03/25/2013 :  06:22:51  Show Profile  Reply with Quote
I have some SQL code which retrieves a row (if exists) from a table. The code is

Declare @Max money
Declare @Min money
declare @UserID int
declare @ProfileID int
declare @SubjectID int

set @Min = 0
set @Max = 500
set @UserID = 29
set @ProfileID = 1
set @SubjectID = 1


INNER JOIN UserRate ON Rate.ID = UserRate.RateID

(UserID =@UserID)AND
(ProfileID = @ProfileID) AND
(SubjectID = @SubjectID) AND
(@Min >= MinAmount and @Min <= MaxAmount) OR
(@Max >= MinAmount and @Max <= MaxAmount) OR
(@Min <= MinAmount and @Max > MinAmount)

What im trying to get is to see if a rate exist for a User based on their profile, subject and the rates.

You can have multiple rates but not for the same min and max amount of if the rate falls into this range.

When i pass in the values (min,max, userid, subjectid and profileid) i get a row back for the userID which does NOT belong to the UserID i passed in but has that rate available

So as an example, if i pass in User 1 i get a row back with his details along with the min and max amounts.
If i pass in user 2 i get back User 1 results but the userID shown is for USER 1 - So i think my WHERE clause is a little wrong somewhere?

Could anyone advise?


Flowing Fount of Yak Knowledge

2242 Posts

Posted - 03/25/2013 :  08:38:57  Show Profile  Reply with Quote
Can you exlain your problem with some sample data?
Take some input data for Rate and UserRate tables and then explain your problem....

Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 03/25/2013 :  09:46:45  Show Profile  Reply with Quote
make where clause like below and see

(UserID =@UserID)AND
(ProfileID = @ProfileID) AND
(SubjectID = @SubjectID) AND
(@Min >= MinAmount and @Min <= MaxAmount) OR
(@Max >= MinAmount and @Max <= MaxAmount) OR
(@Min <= MinAmount and @Max > MinAmount))

SQL Server MVP

Go to Top of Page

Aged Yak Warrior

550 Posts

Posted - 03/25/2013 :  14:29:48  Show Profile  Reply with Quote

(UserID = @UserID) AND
(ProfileID = @ProfileID) AND
(SubjectID = @SubjectID) AND
NOT (@Min > MaxAmount OR @Max < MinAmount)
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000