Simple query I cannot figure out

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 118
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: Simple query I cannot figure out

 
1
  #11
Jul 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 118
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: Simple query I cannot figure out

 
0
  #12
Jul 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 18
Reputation: LAMDB is an unknown quantity at this point 
Solved Threads: 0
LAMDB LAMDB is offline Offline
Newbie Poster

Re: Simple query I cannot figure out

 
0
  #13
Jul 7th, 2009
Originally Posted by cgyrob View Post
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 118
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: Simple query I cannot figure out

 
0
  #14
Jul 7th, 2009
I see what you mean, I am in the middle of something but i will work on a query for you.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 18
Reputation: LAMDB is an unknown quantity at this point 
Solved Threads: 0
LAMDB LAMDB is offline Offline
Newbie Poster

Re: Simple query I cannot figure out

 
0
  #15
Jul 7th, 2009
Originally Posted by cgyrob View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 118
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: Simple query I cannot figure out

 
0
  #16
Jul 7th, 2009
As you mentioned in an earlier post Intersect is the way to go if you are using SQL Server 2005 or later.

  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.

  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC