SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

jamie_pattison
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

SELECT
*
FROM
Rate

INNER JOIN UserRate ON Rate.ID = UserRate.RateID

WHERE
(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?

Thanks

bandi
Flowing Fount of Yak Knowledge

India
2170 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....

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

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

[code]
..
WHERE
(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
http://visakhm.blogspot.com/

Go to Top of Page

ScottPletcher
Constraint Violating Yak Guru

USA
279 Posts

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

WHERE 
(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  
 New 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.08 seconds. Powered By: Snitz Forums 2000