20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

FROM dbo.MemberDetails<br />

INNER JOIN dbo.Attendance<br />

ON dbo.MemberDetails.MemberId = dbo.Attendance.MemberId<br />

INNER JOIN dbo.Location<br />

ON dbo.Attendance.LocationId = dbo.Location.LocationId<br />

Having run this <strong>SQL</strong> statement, you now have a view named MemberAttendance. If you build a query<br />

selecting all the fields from that view, you end up with a much more readable statement:<br />

SELECT LastName, FirstName, DateOfBirth, Street, City, State, ZipCode, Email,<br />

DateOfJoining, MeetingDate, MemberAttended, [Meeting Street], [Meeting City],<br />

[Meeting State]<br />

FROM MemberAttendance<br />

Executing this statement provides the following results shown in Figure 10-2.<br />

Figure 10-2<br />

More important, you now have a base upon which you can build the various queries you might want.<br />

The name of the base view, MemberAttendance, is used in the subsequent query just as if it were a real<br />

table, and the field names from that view are used just as if they were real field names in a real table. In<br />

appearance, MemberAttendance is a table. As you have just seen, however, it is not, but rather a query<br />

pulling specific fields from specific tables. Because the view definition is saved in the database, the<br />

DBMS can perform all the necessary work required to translate the <strong>SQL</strong> statement into a data set, which<br />

is then presented to the new query to use as if it really were a table.<br />

Row Views<br />

Using a WHERE clause to filter down a results set is often useful when pulling all of the fields for a given<br />

set of records. Row views are views that select a subset of all the rows in the larger dataset. Perhaps you<br />

want to pull the records for all the members who attended any meetings.<br />

SELECT MemberAttendance.*<br />

FROM MemberAttendance<br />

WHERE (MemberAttended = ‘Y’)<br />

ORDER BY LastName, FirstName, MeetingDate<br />

Should you need to build other views of members who actively attend meetings, you could simply save<br />

this query as a view:<br />

CREATE VIEW ActiveMemberAttendance AS<br />

SELECT MemberAttendance.*<br />

FROM MemberAttendance<br />

WHERE (MemberAttended = ‘Y’)<br />

ORDER BY LastName, FirstName, MeetingDate<br />

Views<br />

291

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!