Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
prunera
Starting Member
2 Posts |
Posted - 2009-08-12 : 05:37:14
|
| Hi Team!Anybody could help me with this hell query?I got 2 tables:Table: STUDENTS with only 1 field: [name]id1: joseid2: williamid3: johnid4: sandraTable: COURSES with 2 fields: [name], [course]id1: jose, saxoid2: william, cookid3: john, saxoid4: sandra, gardenI wanna make something "as easy" like a query that displays ALL the students, and, on the right side of each student, a 1 if that student is a student of the saxo course or 0 if don't.I'm sure that it is easy! but I'm not success with that.Thanks in anticipation for your help !from Barcelona, Spain (F.C. BARCELONA Oheeee!!) |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-08-12 : 05:40:52
|
| Please provide expected output |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-12 : 05:45:29
|
[code]DECLARE @students TABLE(name varchar(20))INSERT INTO @studentsSELECT 'jose' UNION ALLSELECT 'william' UNION ALLSELECT 'john' UNION ALLSELECT 'sandra' DECLARE @courses TABLE(name varchar(20), course varchar(20))INSERT INTO @coursesSELECT 'jose', 'saxo' UNION ALLSELECT 'william', 'cook' UNION ALLSELECT 'john', 'saxo' UNION ALLSELECT 'sandra', 'garden'SELECT s.name, CASE WHEN course = 'saxo' then 1 else 0 ENDFROM @students s join @courses c on s.name=c.name[/code] Hope can help...but advise to wait pros with confirmation... |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-08-12 : 05:51:04
|
Hi Prunera,If your tables are actually like this then you are breaking first normal form which isn't good.You should probably have table like this:TABLE Students ( <SOME KEY> -- Could be a surrogate Identity or similar but it has to be unique , [name] -- Student Name )TABLE Courses ( <SOME KEY> -- Again if there is no good unique candidate then a surrogate key could be used , [name] -- Course Name )TABLE classAssignment ( <SOME KEY> -- this could be a compound key over the following two columns or a surrogate key , [student_ID] FOREIGN KEY to <PRIMARY_KEY> on students table , [course_ID] FOREIGN KEY to <PRIMARY_KEY> on courses table ) This data format would obey normalisation.For your current set this might work....SELECT s.[name] , CASE WHEN c.[course] IS NULL THEN 0 ELSE 1 END AS [Student of SAXO]FROM students s LEFT JOIN courses c ON c.[name] = s.[name] AND c.[course] = 'saxo' But be aware that you haven't got a good candidate for a primary key on either of those tables (name is not a good choice as names are not unique).Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-08-12 : 05:53:28
|
quote: Originally posted by waterduck
DECLARE @students TABLE(name varchar(20))INSERT INTO @studentsSELECT 'jose' UNION ALLSELECT 'william' UNION ALLSELECT 'john' UNION ALLSELECT 'sandra' DECLARE @courses TABLE(name varchar(20), course varchar(20))INSERT INTO @coursesSELECT 'jose', 'saxo' UNION ALLSELECT 'william', 'cook' UNION ALLSELECT 'john', 'saxo' UNION ALLSELECT 'sandra', 'garden'SELECT s.name, CASE WHEN course = 'saxo' then 1 else 0 ENDFROM @students s join @courses c on s.name=c.name Hope can help...but advise to wait pros with confirmation...
Hi Waterduck.This works for the sample data OP has posted. However, what if there are extra students in the students table that have no lines in the courses table.......The table design is bad. It needs to be changed.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-12 : 05:54:59
|
hehe tc...this is what im doing or facing in the office...everyday handle null value or nulls row~ Hope can help...but advise to wait pros with confirmation... |
 |
|
|
prunera
Starting Member
2 Posts |
Posted - 2009-08-12 : 08:16:50
|
| Many thanks Transact & Water, It works!Sorry, I omitted the complete table definitions (with primary keys) that are exact as you describe.Thanks again folksfrom Barcelona, Spain (F.C. BARCELONA Oheeee!!) |
 |
|
|
|
|
|
|
|