SunQuest
           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:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old April 21st, 2004, 12:18 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
Red face Query Syntax Help

Im having difficulty writting this sql. I have the three following tables and I want to output as listed below.


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 |.....+----+--------+
........................+-----|-----+


Output

+-----+------+-------+-------+------+
| UID | USER | SINGS | DANCE | BARK |
+-----+------+-------+-------+------+
| 124 | JOHN | ...1.... | ...1.... | NULL.|
| 258 | CARL | .NULL.. | .NULL..| ...1.. |
| 300 | TONY | ...1.... | .NULL..| .NULL|
+-----+------+------+-------+------+

Last edited by scottyardley : April 21st, 2004 at 12:41 PM.

Reply With Quote
  #2  
Old April 21st, 2004, 02:49 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,766 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 12 m 39 sec
Reputation Power: 37
Code:
select uid,user,
      (select 1 from tblUserToSkill where uid = tblUser.uid
                   and SID in (select sid from tblSkill 
                                where SKILL = 'SINGS')) as SINGS,
      (select 1 from tblUserToSkill where uid = tblUser.uid
                   and SID in (select sid from tblSkill 
                                where SKILL = 'DANCE')) as DANCE,
      (select 1 from tblUserToSkill where uid = tblUser.uid
                   and SID in (select sid from tblSkill 
                                where SKILL = 'BARKS')) as BARKS
from tblUser

Reply With Quote
  #3  
Old April 21st, 2004, 03:05 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
Dynamic tbl content needs dynamic sql

The entries into the tables are ever changing so I cannot specify the field values in the query (i.e. dance, sing)

Reply With Quote
  #4  
Old April 21st, 2004, 09:01 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
This may work

I can't test this...but see what you get...
Code:
Select	UID, User,
	Case
	  When US.SID = 100 Then '1'
	  Eslse 'null'
	End [Sings],
	Case
	  When US.SID = 200 Then '1'
	  Else 'null'
	End [Dance],
	Case
	  When US.SID = 300 Then '1'
	  Else 'null'
	End [Bark]
from	tblUser U
join	tblUserToSkill US on U.UID = US.UID

Reply With Quote
  #5  
Old April 21st, 2004, 09:32 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,331 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 7 h 21 m 33 sec
Reputation Power: 891
Quote:
Originally Posted by scottyardley
The entries into the tables are ever changing so I cannot specify the field values in the query (i.e. dance, sing)

then the only way that i know how to do it is with microsoft access as a crosstab query, or something similar in a programming language

yes, technically you could do it with a cursor in a stored proc, but i don't consider that to be an sql solution, it's actually a programming solution
__________________
r937.com | rudy.ca

Reply With Quote
  #6  
Old April 22nd, 2004, 07:24 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
I've aready chased down the crosstab query route. Here's and article for doing crosstab in MySQL:
http://dev.mysql.com/tech-resources...nt_version.html

I burned at least an hour on it. Some neat stuff and in the general direction of what Im looking for...but no cigar.

I would call it a programing solution for lack of a sql solution.
A many-to-many table is a great resourse for keeping a database small. I use them oftern. It would seem that the sql language would lend to a solution. I feel this is a short comming in sql language.

In a programming solution I would have to query, loop through the query then query for each item in the loop. This seems like alot of quering. Also too much code. Unfortunately that what I had to do.


Reply With Quote
  #7  
Old April 23rd, 2004, 03:48 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,766 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 12 m 39 sec
Reputation Power: 37

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Query Syntax Help


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


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway