Query to pull different grade level students

  • How can I write one query to limit records?

    Schoolname, Grade

    Pathfinder K

    South Shore 2

    Whittier K

    Lawton 2

    Washington 06

    Garfield 09

    I want to select all the students in each school but with different grade.

    So for example I want to select * from students,

    when they are in Path finder school, I only select grade is K, if the are in South shore, I only select grade 2 student.

    How can I write the select query in one batch?

    Thanks

  • sqlfriends (4/10/2012)


    How can I write one query to limit records?

    Schoolname, Grade

    Pathfinder K

    South Shore 2

    Whittier K

    Lawton 2

    Washington 06

    Garfield 09

    I want to select all the students in each school but with different grade.

    So for example I want to select * from students,

    when they are in Path finder school, I only select grade is K, if the are in South shore, I only select grade 2 student.

    How can I write the select query in one batch?

    Thanks

    WOW.. With around 1000 plus posts and 1000 plus visits, you still did not know how to post a question with readily consumable data! Wow, I'm, outta here!!

  • Never mind, I figure it out.

    I don't want to post the table structure out for some reasons, but for this one I think it is simple enough and I do use a simple select statement example for you to use.

  • Man, your select was the most basics of select 😀

    select * from students

    Anyways, good that you got it working. And i did not ask you to post you entire table structure! Read my post again, u ll know that whatever you have posted, should be in readily consumable format!

    Enjoy, bro!

  • I would put your choices into a second table. This second table could be a temporary table or a table variable.

    I agree that you should have provided the table structure and data with your question, so I'm just guessing at what your actual structure is.

    -- Create and load a sample table with school, student, and grade data

    DECLARE @Enrollments TABLE (

    school varchar(50),

    student varchar(50),

    grade varchar(10)

    )

    INSERT INTO @Enrollments values ('1st Street School', 'Able, Alice', '2')

    INSERT INTO @Enrollments values ('Lincoln School',' Boop, Betty', 'K')

    INSERT INTO @Enrollments values ('1st Street School', 'Clown, Charlie', 'K')

    INSERT INTO @Enrollments values ('1st Street School', 'Delta, Doug', '2')

    INSERT INTO @Enrollments values ('Lincoln School','Echo, Edgar', '2')

    INSERT INTO @Enrollments values ('Lincoln School', 'Fox, Fredrika', '2')

    INSERT INTO @Enrollments values ('1st Street School', 'Green, Gilda', 'K')

    -- Load the selection choices into a second table

    DECLARE @Choices TABLE (

    school varchar(50),

    grade varchar(10)

    )

    INSERT INTO @Choices VALUES ('1st Street School', 'K')

    INSERT INTO @Choices VALUES ('Lincoln School', '2')

    SELECT

    Enroll.school,

    Enroll.student,

    Enroll.grade

    FROM @Enrollments AS Enroll

    INNER JOIN @Choices AS Choice

    ON Enroll.school = Choice.school AND Enroll.grade = Choice.grade

    ORDER BY Enroll.school, Enroll.grade, Enroll.student

    Chuck Hoffman
    ------------
    I'm not sure why we're here, but I am sure that while
    we're here we're supposed to help each other
    ------------

  • Thank you Chuck, I will give a try about this.

Viewing 6 posts - 1 through 5 (of 5 total)

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