|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
SQL advice
Hi all,
this is my first post and I'm a newb in MS SQL and have a basic understanding in SQL but I hope to get better at it soon ![]() I have a problem and can't figure out how to setup my tables properly. You will find table constructs and their values, the SQL query and it's output. To me the Query is wrong, not in the way it returns the result but in it's use. I am trying to retrieve the Language Skills of my users. Table Request Code:
IDRequest int Name nvarchar(50) LanguageSkillNL int LanguageSkillFR int LanguageSkillEN int Table Values Code:
1 John 3 1 1 2 Frank 3 2 1 Table LanguageSkill Code:
IDLanguageSkill int LanguageSkill nchar(30) Table Values Code:
1 Good 2 Average 3 Bad SQL Query Code:
SELECT t1.IDRequest, t1.Name, t2.LanguageSkill as 'Dutch', t3.LanguageSkill as 'French', t4.LanguageSkill as 'English' FROM Request t1 LEFT JOIN LanguageSkill t2 ON (t1.LanguageSkillNL = t2.IDLanguageSkill) LEFT JOIN LanguageSkill t3 ON (t1.LanguageSkillFR = t3.IDLanguageSkill) LEFT JOIN LanguageSkill t4 ON (t1.LanguageSkillEN = t4.IDLanguageSkill) ORDER BY IDRequest; SQL Output Code:
1 John Bad Good Good 2 Frank Bad Average Good How would you deal with the above without hardcoding the values in the Request Table? Many thanks for your help and/or pointers to documentation. -eco22 |
|
#2
|
||||
|
||||
|
i would do it like this --
Code:
CREATE TABLE Persons
( ID INTEGER NOT NULL PRIMARY KEY
, Name NVARCHAR(50) NOT NULL
);
INSERT INTO Persons VALUES ( 1, 'John' );
INSERT INTO Persons VALUES ( 2, 'Frank' );
CREATE TABLE Languages
( Lang CHAR(2) NOT NULL PRIMARY KEY
, Descr VARCHAR(10) NOT NULL
);
INSERT INTO Languages VALUES ( 'NL', 'Dutch' );
INSERT INTO Languages VALUES ( 'FR', 'French' );
INSERT INTO Languages VALUES ( 'EN', 'English' );
CREATE TABLE Requests
( ID INTEGER NOT NULL
, FOREIGN KEY ( ID ) REFERENCES Persons ( ID )
, Lang CHAR(2) NOT NULL
, FOREIGN KEY ( Lang ) REFERENCES Languages ( Lang )
, PRIMARY KEY ( ID , Lang )
, Skill VARCHAR(7) NOT NULL
);
INSERT INTO Requests VALUES ( 1, 'NL', 'Bad' );
INSERT INTO Requests VALUES ( 1, 'FR', 'Good' );
INSERT INTO Requests VALUES ( 1, 'EN', 'Good' );
INSERT INTO Requests VALUES ( 2, 'NL', 'Bad' );
INSERT INTO Requests VALUES ( 2, 'FR', 'Average' );
INSERT INTO Requests VALUES ( 2, 'EN', 'Good' );
SELECT Persons.ID
, Persons.Name
, Requests.Lang
, Requests.Skill
FROM Persons
LEFT OUTER
JOIN Requests
ON Requests.ID = Persons.ID
results -- Code:
ID Name Lang Skill 1 John EN Good 1 John FR Good 1 John NL Bad 2 Frank EN Good 2 Frank FR Average 2 Frank NL Bad Code:
SELECT Persons.ID
, Persons.Name
, Languages.Descr
, Requests.Skill
FROM Persons
LEFT OUTER
JOIN Requests
ON Requests.ID = Persons.ID
LEFT OUTER
JOIN Languages
ON Languages.Lang = Requests.Lang
results -- Code:
ID Name Lang Skill 1 John English Good 1 John French Good 1 John Dutch Bad 2 Frank English Good 2 Frank French Average 2 Frank Dutch Bad ![]() Last edited by r937 : July 1st, 2009 at 09:58 AM. |
|
#3
|
|||
|
|||
|
Much appreciated r937,
I'll give it a go... It certainly looks more 'ethical' ![]() Thanks -eco22 |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > SQL advice |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|