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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Hell query using 2 tables. It seems easy but not !

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: jose
id2: william
id3: john
id4: sandra

Table: COURSES with 2 fields: [name], [course]
id1: jose, saxo
id2: william, cook
id3: john, saxo
id4: sandra, garden

I 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
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-08-12 : 05:45:29
[code]DECLARE @students TABLE(name varchar(20))
INSERT INTO @students
SELECT 'jose' UNION ALL
SELECT 'william' UNION ALL
SELECT 'john' UNION ALL
SELECT 'sandra'

DECLARE @courses TABLE(name varchar(20), course varchar(20))
INSERT INTO @courses
SELECT 'jose', 'saxo' UNION ALL
SELECT 'william', 'cook' UNION ALL
SELECT 'john', 'saxo' UNION ALL
SELECT 'sandra', 'garden'

SELECT s.name, CASE WHEN course = 'saxo' then 1 else 0 END
FROM @students s join @courses c on s.name=c.name[/code]


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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 @students
SELECT 'jose' UNION ALL
SELECT 'william' UNION ALL
SELECT 'john' UNION ALL
SELECT 'sandra'

DECLARE @courses TABLE(name varchar(20), course varchar(20))
INSERT INTO @courses
SELECT 'jose', 'saxo' UNION ALL
SELECT 'william', 'cook' UNION ALL
SELECT 'john', 'saxo' UNION ALL
SELECT 'sandra', 'garden'

SELECT s.name, CASE WHEN course = 'saxo' then 1 else 0 END
FROM @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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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...
Go to Top of Page

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 folks


from Barcelona, Spain (F.C. BARCELONA Oheeee!!)
Go to Top of Page
   

- Advertisement -