MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old April 22nd, 2004, 07:37 PM
scottyardley scottyardley is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 5 scottyardley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Another SQL syntax challenge

NOTE this is a new question, I know I showed these 3 tabels before.

Im having difficulty writting this sql. I have the three following tables. <br>
I want to be able to query all users in tblUsers who have all of a given selection of skills (tblSkills)

Senerio:
For example the query form would list all the skills from tblSkill with a check box next to each skill. The return for the form should list all users from tblUser who have EACH AND EVERY skill checked.


tblUser................tblUserToSkill.........tblSkill
+----+------+.....+----+-----+.....+----+-------+
| UID | USER |.....| UID | SID |.....| SID | SKILL |
+----+------+.....+----+-----+.....+----+-------+
| 124 | JOHN |.....| 124 | 100 |.....| 100 | SINGS |
| 258 | CARL |.....| 124 | 200 |.....| 200 | DANCE |
| 300 | LENY |.....| 300 | 100 |.....| 300 | BARKS |
+----+------+.....| 258 | 300 |.....+----+--------+
........................+-----|-----+


For the life of me I cannot figure out a sql or programming solution to this one. We'll if my life did count on it, I guess I could write the 60 so lines of code to jump around this problem. But it would be messy.

Reply With Quote
  #2  
Old April 22nd, 2004, 08:57 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Is the Skills table gonna be changing??...just like you stated in your previous question? Or is it going to remain static w/only those three skills? Either way this should work...
Code:
select  U.UID, User from tblUser U
join    tblUserToSkill UTS on U.UID = UTS.UID
group   by U.UID, User
having  count(distinct SID) = 
           (select  count(skill) from tblSkill)

Last edited by Username=NULL : April 22nd, 2004 at 09:58 PM.

Reply With Quote
  #3  
Old April 22nd, 2004, 10:52 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
very good, NULL

but you somehow have to work it so that it's not checking all the skills in tblSkill, but merely the ones selected in the checkboxes (or whatever)

let's say that only 100 and 200 were selected

thus a user could actually have more skills than the ones selected

so therefore you need something like this --
Code:
having  count(distinct SID) >= 
           ( select count(skill) from tblSkill
                where SID in ( 100, 200 ) )

and of course if you look at it even closer, you don't need to count the tblSkill rows at all, just count the number of selections

but wait -- what if the user has 100 and 300? that's 2 distinct, and the subquery from tblSkill returns 2 also...

so that's obviously not going to give a correct answer

i think you must add a WHERE clause as well

Code:
select U.UID
     , User 
  from tblUser U
inner
  join tblUserToSkill UTS 
     on U.UID = UTS.UID
 where UTS.SID in ( 200, 300 )  -- list them here
group  
    by U.UID
     , User
having count(distinct UTS.SID) >= 2 -- count them here
__________________
r937.com | rudy.ca

Reply With Quote
  #4  
Old April 23rd, 2004, 01:30 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Thx rudy, this is what I was thinking though...

Quote:
Originally Posted by r937
...but you somehow have to work it so that it's not checking all the skills in tblSkill, but merely the ones selected in the checkboxes (or whatever)

let's say that only 100 and 200 were selected


Ah...I did have some pre-thoughts about what your bringing up, and I think we just have a diff interpretations of what the poster wants. This is where my focus was when I read his need... (the numbering 1) and 2) are my own) ...you're getting into a scenario where not all skills are listed, this is what I read to make me code otherwise though...
Quote:
Originally Posted by scottyardley
1) I have the three following tables. I want to be able to query all users in tblUsers who have all of a given selection of skills (tblSkills) (now look @ 2)

2) Senerio:
For example the query form would list all the skills from tblSkill with a check box next to each skill. The return for the form should list all users from tblUser who have EACH AND EVERY skill checked.

I'm interpreting that...
1) Every skill will be listed
2) The poster only wants records returned where the user has all the skills available...defined as:
Quote:
Originally Posted by scottyardley
...all of a given selection of skills (tblSkills) [<--I take from that that he's gonna be listing each and every skill from tblSkills, everytime]

...so based off that, I'm accounting for every skill in my count.

Another reason though, is that if the poster does get away from listing all the skills availabe, we can't really just hardcode the skills available from the checkbox group like that, @least it wouldn't be what I would do. If the poster is going to get into changing what skills are listed, then wouldn't we have to start touching the front end stuff to figure whether or not all of the choice boxes are selected? Not sure what the front end is though, maybe using a form writer directly from the DBMS or a front tier UI.

Last edited by Username=NULL : April 23rd, 2004 at 01:54 AM. Reason: quoting rudy on this edit

Reply With Quote
  #5  
Old April 23rd, 2004, 04:03 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
long explanation, so i will give you a short reply:

" ... EACH AND EVERY skill checked"

there seems little reason to assume that all skills will be listed and the user will always check all of them

if that's the case, then why even bother with the checkboxes at all

so yeah, the user is free to check any combination of checkboxes -- all will be listed, but not all will always be checked

then you said "we can't really just hardcode the skills available from the checkbox group like that"

well, no, there would be some sort of scripting logic which would examine which checkboxes have been checked, and string their values together -- in effect we're not hardcoding them, the script is doing it dynamically, but as far as the databse is concerned, by the time the query hits the database, they are hardcoded

Reply With Quote
  #6  
Old April 23rd, 2004, 05:13 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Lightbulb

Quote:
Originally Posted by scottyardley
Senerio:
For example the query form would list all the skills from tblSkill with a check box next to each skill.

Personally I don't see how this isn't sufficient to assume that every skill would be posted.

Quote:
Originally Posted by r937
" ... EACH AND EVERY skill checked"

there seems little reason to assume that all skills will be listed and the user will always check all of them

I am assuming that every skill will be listed.
I am not assuming that every skill will be checked everytime.

And it seems you agree??....
Quote:
Originally Posted by r937
so yeah, the user is free to check any combination of checkboxes -- all will be listed, but not all will always be checked

Not saying you're wrong, not by any means...would like to hear what the poster is really looking for.

Last edited by Username=NULL : April 23rd, 2004 at 05:23 AM.

Reply With Quote
  #7  
Old April 23rd, 2004, 06:59 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
yes, every skill will be listed on the form, but not all skills will be checked

so therefore the sql that gets passed to the database must include some way of identifying just those that have been checked

my query allows for that, but your query doesn't

sorry

Reply With Quote
  #8  
Old April 23rd, 2004, 07:11 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
man you've lost me. Let's get on the same foot, I think I'm thinking on diff terms than you. Let's take a scenerio.

10 skills in the tblSkills table.

We both agree all will be available to be checked.

The poster only wants users that have checked all 10 skills.

The skills checked will show up in the tblUserToSkill table.

So if the user only selects 5 skills of all 10 that are available...my query would exclude that user, which is what the poster wants, no?

Reply With Quote
  #9  
Old April 23rd, 2004, 07:23 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
i will keep trying until you get it

10 skills listed on the form

the user checks 5 of them

the poster only wants those userids pulled from the database where those 5 skills have been checked

i think you are confusing the user of the form with the userids in the table

the key part of the original poster's question is "... to be able to query all users in tblUsers who have ..."

Last edited by r937 : April 23rd, 2004 at 07:26 AM.

Reply With Quote
  #10  
Old April 23rd, 2004, 07:48 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
AHHHHH!......yes ok, that's exactly what I was doing! I was thinking the User of the form was a tblUser.UID. ...and the whole time thought that all 10 skills were available on the form, when they (a UID) selected all 10, return 'em, else don't. Gosh...maybe it's time to get some sleep now

Last edited by Username=NULL : April 23rd, 2004 at 07:50 AM.

Reply With Quote
  #11  
Old April 23rd, 2004, 03:41 PM
scottyardley scottyardley is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 5 scottyardley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
1) The tlbSkills is ever changing. I would never create a table for data that didn't have the potential for change. I would hard code that data.

2) Any combination of check boxes can be checked (that's what check boxes are for). The query must return every tblUser.user who has ALL skills that are check.

HERE is my attempt: Unfortunately it didn't work. Yes it contains php code but its tight code
<?
$sql='SELECT * FROM tblUser WHERE 1';
foreach ($_POST as $k => $v) {
$sql.='AND tblUser.UID IN (SELECT UID FROM tblSKills WHERE SID='.$v.') ';
}
?>


SQL Consultant
Your code worked! I'm just starting to grasp how it works.
You rule!

Thanks.

Last edited by scottyardley : April 23rd, 2004 at 04:20 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Another SQL syntax challenge


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump