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
 Development Tools
 Other Development Tools
 Subqueries

Author  Topic 

timn
Starting Member

2 Posts

Posted - 2007-06-18 : 02:44:20
I have a query in Access that uses a another query, I would like some help on translating it into one SQL statement as a subquery so I can use it in an application.

the queries in Access are
the subquery... (query4)
SELECT Movement.DateTime, Movement.StudentID, Movement.Subject, Movement.Teacher, Movement.Destination, Movement.Lesson
FROM Movement
WHERE (((Movement.DateTime) Between #6/15/2007 7:0:0# And #6/15/2007 19:0:0#) AND ((Movement.Destination)="Away" Or (Movement.Destination)="sign out" Or (Movement.Destination)="sign in") AND ((Movement.Lesson)=3)) OR (((Movement.DateTime) Between #6/15/2007 7:0:0# And #6/15/2007 19:0:0#) AND ((Movement.Destination)="sign in"));


the main query...
SELECT NStudTT.Student, NStudTT.Day, NStudTT.Lesson, NStudTT.Subject, Query4.DateTime, Query4.Subject, Query4.Teacher, Query4.Destination, Query4.Lesson, NStudTT.StudentID
FROM Query4 RIGHT JOIN NStudTT ON Query4.StudentID=NStudTT.StudentID
WHERE (((NStudTT.Day)=5) AND ((NStudTT.Lesson)=3) AND ((NStudTT.Subject) Like "stu*"));


thank you

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-18 : 03:05:36
Try this
SELECT NStudTT.Student, NStudTT.Day, NStudTT.Lesson, NStudTT.Subject, Q.DateTime, Q.Subject, Q.Teacher, 
Q.Destination, Q.Lesson, NStudTT.StudentID
FROM
(
SELECT
Movement.DateTime, Movement.StudentID, Movement.Subject, Movement.Teacher, Movement.Destination, Movement.Lesson
FROM
Movement
WHERE (((Movement.DateTime) Between #6/15/2007 7:0:0# And #6/15/2007 19:0:0#) AND ((Movement.Destination)="Away" Or
(Movement.Destination)="sign out" Or (Movement.Destination)="sign in") AND ((Movement.Lesson)=3)) OR
(((Movement.DateTime) Between #6/15/2007 7:0:0# And #6/15/2007 19:0:0#) AND ((Movement.Destination)="sign in"));
) Q
RIGHT JOIN NStudTT ON Q.StudentID=NStudTT.StudentID
WHERE
(((NStudTT.Day)=5) AND ((NStudTT.Lesson)=3) AND ((NStudTT.Subject) Like "stu*"));


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

timn
Starting Member

2 Posts

Posted - 2007-06-18 : 19:09:28
What a Star, Thankyou
Go to Top of Page
   

- Advertisement -