strange SQL selection

Hi there!

The following code works as expected:

select pagenumber from dbo.table1 where (UserID = 5 AND MaterialID = 258 AND PageNumber > 9)
EXCEPT
(select pagenumber from dbo.table2 where (UserID = 5 and MaterialID = 258))

In the sense that we get all "pagenumbers" greater than 9 such that they are present at table 1 but not present at table 2.

My question: how could I get ONLY the first pagenumber (I mean, the smaller pagenumber that satisfies this) from this selection?

I´ve tried to put top 1 in the very beginning (as select top 1 pagenumber ...) and it got nothing...

Important: please do not worry about 5 or 258... this numbers will be variables after the problem is solved, of course.

Thanks,
fskilnik.
fskilnikAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
Did you try:

select min(pagenumber) PageNumber from dbo.table1 where (UserID = 5 AND MaterialID = 258 AND PageNumber > 9) 
EXCEPT
(select pagenumber from dbo.table2 where (UserID = 5 and MaterialID = 258))

Open in new window

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
perhaps directly:

select min(pagenumber) pagenumber  from dbo.table1 where (UserID = 5 AND MaterialID = 258 AND PageNumber > 9) 

Open in new window


? I think you no need to exclude the records in your second SQL statement.
fskilnikAuthor Commented:
Sorry for the delay...

@Kyle: yes, I´ve tried this before, and it gets no values... (not right)

@Ryan: I guess you did not understand my idea. The second SQL statement is a list of pagenumbers that should not be consider as "candidates" (to the final selection).
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Kyle AbrahamsSenior .Net DeveloperCommented:
select min(t1.pagenumber) pagenumber  from
dbo.table1  t1
left join dbo.table2 t2 on t1.UserId = t2.UserId and t1.MaterialID = t2.MaterialID
where
t1.UserID = 5 AND t1.MaterialID = 258 AND t1.PageNumber > 9
and t2.UserId is null

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fskilnikAuthor Commented:
Hi, Kyle. Thanks on insisting on the matter.

Now I got a "NULL" value on the pagenumber field... guess we are nearer the solution...

(I´ve tried to change the number 9 to other possibilities, but the "NULL" answer persists...)

While you keeping trying (please), I will try too, based on your strange (but clever) idea...
fskilnikAuthor Commented:
Hello, Kyle.

Congrats, Sir!  You got it, with a small modification:

select min(t1.pagenumber) pagenumber  from
dbo.table1  t1
left join dbo.table2 t2 on t1.UserId = t2.UserId and t1.MaterialID = t2.MaterialID and t1.MatDoneID = t2.MatDoneID
where
t1.UserID = 5 AND t1.MaterialID = 258 AND t1.PageNumber > 9
and t2.UserId is null

Thanks a lot!

Regards,
fskilnik.
Kyle AbrahamsSenior .Net DeveloperCommented:
The other solutions should work as well then . . . you would just need to pass in the matDoneID as well.  

Getting your keys correct is crucial to getting proper results.  Glad you got it.
fskilnikAuthor Commented:
> The other solutions should work as well then . . . you would just need to pass in the matDoneID as well.  

You are very right, Kyle!

After I realized the (original) "empty" value was probably due to the fact that my query/view was NOT able to "filter things in a proper way", everything made sense...  

Thanks for the explanation. Now I am sure I got it right, not just the problem, but its understanding.

Kind Regards,
fskilnik.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.