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.

For example, you might want a list of all the members who did not attend a specific meeting, with their<br />

email addresses so that you could send an email outlining what you did at that meeting and inviting<br />

them to attend the next meeting.<br />

SELECT LastName, FirstName, Email, MeetingDate, MemberAttended<br />

FROM MemberAttendance<br />

WHERE (MemberAttended = ‘N’) AND (MeetingDate = CONVERT(DATETIME, ‘2004-01-01’,<br />

102))<br />

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

Figure 10-5<br />

Or perhaps you need the names and birth dates of members who have attended any meeting so that you<br />

can invite active members to a birthday bash to thank them for their participation.<br />

SELECT DISTINCT LastName, FirstName, Email<br />

FROM MemberAttendance<br />

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

Figure 10-6 displays the results of this query.<br />

Figure 10-6<br />

As you can see, windowed queries are trivial to build when you have a good base view to work with.<br />

Summary Views<br />

Finally, you might wish to perform data analysis, perhaps determining the most (or least) active<br />

members.<br />

SELECT COUNT(MemberAttended) AS CntOfAttendance, FirstName, LastName<br />

FROM Attendance INNER JOIN<br />

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

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

GROUP BY FirstName, LastName<br />

Executing this statement gives you the results depicted in Figure 10-7.<br />

Views<br />

293

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

Saved successfully!

Ooh no, something went wrong!