Help constructing a complex query

  • I need to pull data from 3 tables where one of the columns is a reference column and is used twice in the return data. So I have table three tables

    Student s

    id name age

    Grades g

    studentid class grade

    Codes c

    studentid cde_variable cde_value

    I need SELECT s.id, c.cde.value AS 'teacher' where c.cde_varlable = 'a', c.cde_value AS 'school' where c.cde_variable = 'b' and where s.id= g.studentid AND g.studentid = c.studentid.

    The reference column is cde_value, so I need to search it using two different filters in the same query. So I thought about using a union.

    StudentID Teacher School

    123 Jones ' '

    345 Thomas ' '

    UNION

    StudentID Teacher School

    123 ' ' Memorial High

    345 ' ' Science Academy

    But I get (Below are column but I couldn't draw a table)

    StudentID Teacher School

    123 Jones ' '

    345 Thomas ' '

    123 ' ' Memorial High

    345 ' ' Science Academy

    How can I get this query together?

  • Hi......if you could provide some scripts that give table structure / sample data / expected results (based on sample data)...then that will help us all.

    http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • You just need to use 2 joins in your query.

    There's an example at the bottom of this page: http://www.sql-join.com/

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply