|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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
|
|
#3
|
|||
|
|||
|
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)
|
|
#4
|
|||
|
|||
|
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 |
|
#5
|
||||
|
||||
|
Quote:
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 |
|
#6
|
|||
|
|||
|
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. ![]() |
|
#7
|
|||
|
|||
|
|
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Query Syntax Help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|