April 10, 2012 at 4:46 pm
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
April 10, 2012 at 5:51 pm
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!!
April 10, 2012 at 5:57 pm
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.
April 10, 2012 at 6:05 pm
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!
April 10, 2012 at 11:54 pm
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
------------
April 11, 2012 at 9:24 am
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