943,928 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 1108
  • MS SQL RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jul 6th, 2009
1

Re: Simple query I cannot figure out

What you need to do is build the query for only the selected options within the code block. If you build a static select query with all the options then it will return nothing because no user will select all the options.

In the code you will have to pull all the options the user selected and build the query to include only those options. Also don't use the left join from the original query as that will give null results from the Users_AOI table which you don't want.
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Jul 6th, 2009
0

Re: Simple query I cannot figure out

If you have questions on how to dynamically build queries is C#, make a post in the C# forum.
Last edited by cgyrob; Jul 6th, 2009 at 9:44 pm.
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Jul 7th, 2009
0

Re: Simple query I cannot figure out

Click to Expand / Collapse  Quote originally posted by cgyrob ...
If you have questions on how to dynamically build queries is C#, make a post in the C# forum.
I am building the query dynamically in C# ( c# is not the issue here) with only the options selected by the user.

However, like I said in my previous example, if a user pre-selected "music" and "paiting" you cannot ask:
MS SQL Syntax (Toggle Plain Text)
  1. ...
  2. WHERE
  3. AOI = music AND
  4. AOI = Painting

Even if the user preselected these two Areas Of Interest there are in two separate records, and there is no record that is music AND painting at the same time, therefore, the query (even when dynamically created) will return nothing.

Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LAMDB is offline Offline
18 posts
since Jul 2008
Jul 7th, 2009
0

Re: Simple query I cannot figure out

I see what you mean, I am in the middle of something but i will work on a query for you.
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Jul 7th, 2009
0

Re: Simple query I cannot figure out

Click to Expand / Collapse  Quote originally posted by cgyrob ...
I see what you mean, I am in the middle of something but i will work on a query for you.
Thanks so much. I appreciate it.

I will post what I have done so far later today.

Thanks again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LAMDB is offline Offline
18 posts
since Jul 2008
Jul 7th, 2009
0

Re: Simple query I cannot figure out

As you mentioned in an earlier post Intersect is the way to go if you are using SQL Server 2005 or later.

sql Syntax (Toggle Plain Text)
  1. SELECT a.name,a.userid
  2. FROM users a, aoi b, users_aoi c
  3. WHERE a.userid = c.userid
  4. AND b.aoiid = c.aoiid
  5. AND b.aoiid = option1
  6. Intersect
  7. SELECT a.name, a.userid
  8. FROM users a, aoi b, users_aoi c
  9. WHERE a.userid = c.userid
  10. AND b.aoiid = c.aoiid
  11. AND b.aoiid = option2

Ofcourse if you are still using SQL Server 2000 you do not have access to Intersect. I have put together a query that I beleive will do the trick for you, give it a try and let me know.

sql Syntax (Toggle Plain Text)
  1. SELECT DISTINCT a.userid,a.name
  2. FROM Users a,efx_aoi b ,users_aoi c
  3. WHERE a.userid = c.userid
  4. AND b.aoiid = c.aoiid
  5. AND EXISTS
  6. (
  7. SELECT 1
  8. FROM users_aoi c
  9. WHERE a.userid = c.userid
  10. AND c.aoiid = 3
  11. AND EXISTS(SELECT 1
  12. FROM users_aoi c
  13. WHERE a.userid = c.userid
  14. AND c.aoiid = 4)
  15. )

You will have to build the exists portion dynamically to include the options selected.

hope this helps
Last edited by cgyrob; Jul 7th, 2009 at 2:39 pm.
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Sql Server 2008 Reporting services
Next Thread in MS SQL Forum Timeline: Error while transfering text data type data through linked server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC