1. Trang chủ
  2. » Công Nghệ Thông Tin

The Language of SQL- P23 docx

5 218 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Here’s an example of the DISTINCT keyword, used with the following SongTitles table: SongID Artist Album Title 1 The Beatles Abbey Road Come Together 2 The Beatles Abbey Road Sun King 3 The Beatles Revolver Yellow Submarine 4 The Rolling Stones Let It Bleed Monkey Man 5 The Rolling Stones Flowers Ruby Tuesday 6 Paul McCartney Ram Smile Away Let’s say you want to see a list of artists in the table. This can be accom- plished with: SELECT DISTINCT Artist FROM SongTitles ORDER BY Artist The results are: Artist Paul McCartney The Beatles The Rolling Stones The DISTINCT keyword is always placed immediately after the SELECT key- word. The DISTINCT specifies that only unique values of the columnlist that follow are to be brought back. In this case, there are only three unique artists, so only three rows are returned. If you want to see unique combinations of both artists and albums, you’d issue: SELECT DISTINCT Artist, Chapter 10 ■ Summarizing Data96 Album FROM SongTitles ORDER BY Artist, Album and the results would be: Artist Album Paul McCartney Ram The Beatles Abbey Road The Beatles Revolver The Rolling Stones Flowers The Rolling Stones Let It Bleed Notice that Abbey Road is only listed once, even though there are two songs from that album in the table. This is because the DISTINCT keyword causes only unique values from the listed columns to be shown. Aggregate Functions The functions we discussed in Chapter 4 were all scalar functions. These functions were all performed on a single number or value. In contrast, aggregate functions are meant to be used with groups of data. The mostly widely used aggregate functions are COUNT, SUM, AVG, MIN, and MAX. These provide counts, sums, averages, and minimum and maximum values of groups of data. All of our aggregate function examples will be taken from the following two tables with data about students, fees, and grades. The Fees table contains: FeeID Student FeeType Fee 1 George Gym 30 2 George Lunch 10 3 George Trip 8 4 Janet Gym 30 5 Alan Lunch 10 Aggregate Functions 97 Here’s the Grades table: GradeID Student GradeType Grade 1 Susan Quiz 92 2 Susan Quiz 95 3 Susan Homework 84 4 Kathy Quiz 62 5 Kathy Quiz 81 6 Kathy Homework NULL 7 Alec Quiz 58 8 Alec Quiz 74 9 Alec Homework 88 Starting with the SUM function, let’s say that you want to see the total amount of gym fees paid by all students. This can be accomplished with: SELECT SUM (Fee) AS 'Total Gym Fees' FROM Fees WHERE FeeType ¼ 'Gym' The resulting data is: Total Gym Fees 60 As can be seen, the SUM function sums up the total values for the Fee column, subject to selection in the WHERE clause. Since the only expression in the columnlist is an aggregate function, the query returns only one row of data, giving the aggregate amount. DATABASE DIFFERENCES: MySQL As noted in Chapter 4, MySQL sometimes requires that there be no space between a function name and the left parenthesis. This is also true of most aggregate functions. For example, the previous statement needs to be written as the following in MySQL: SELECT SUM(Fee) AS 'Total Gym Fees' Chapter 10 ■ Summarizing Data98 FROM Fees WHERE FeeType ¼ 'Gym' The AVG, MIN, and MAX functions are quite similar. Here’s an example of the AVG function. In this case, we’re seeking to obtain the average grade of all quizzes in the Grades table: SELECT AVG (Grade) AS 'Average Quiz Score' FROM Grades WHERE GradeType ¼ 'Quiz' The result is: Average Quiz Score 77 More than one aggregate function can be used in a single SELECT statement. Here’s a SELECT that illustrates AVG, MIN, and MAX in a single statement: SELECT AVG (Grade) AS 'Average Quiz Score', MIN (Grade) AS 'Minimum Quiz Score', MAX (Grade) AS 'Maximum Quiz Score' FROM Grades WHERE GradeType ¼ 'Quiz' The result is: Average Quiz Score Minimum Quiz Score Maximum Quiz Score 77 58 95 The numbers you see have been computed separately. The output shows the average, minimum, and maximum of all qui zzes in the Grades table. The COU NT Function The COUNT function is slightly more complex, in that it can be used in three different ways. The COUNT Function 99 First, the COUNT function can be used to return a count of all selected rows, regardless of the values in any particular column. As an example, the following returns a count of all rows with homework grades: SELECT COUNT (*) AS 'Count of Homework Rows' FROM Grades WHERE GradeType ¼ 'Homework' The result is: Count of Homework Rows 3 The asterisk in the parentheses means ‘‘all columns.’’ SQL retrieves all columns in those rows that are selected, and then it returns a count of the number of rows. In the second format of the COUNT function, a specific column is specified rather than the asterisk. Here’s an example: SELECT COUNT (Grade) AS 'Count of Homework Scores' FROM Grades WHERE GradeType ¼ 'Homework' The result is: Count of Homework Scores 2 Notice the subtle difference between the previous two SELECT statements. In the first, you’re merely counting rows where the GradeType equals ‘‘Homework.’’ There are three of these rows. In the second, you’re counting occurrences of the Grade column where the GradeType column has a value of ‘‘Homework.’’ In this case, one of the three rows has a value of NULL in the Grade column, so it isn’t counted. As you remember, NULL means that the data doesn’t exist. Chapter 10 ■ Summarizing Data100 . ‘‘Homework.’’ There are three of these rows. In the second, you’re counting occurrences of the Grade column where the GradeType column has a value of ‘‘Homework.’’ In this case, one of the three. 'Homework' The result is: Count of Homework Rows 3 The asterisk in the parentheses means ‘‘all columns.’’ SQL retrieves all columns in those rows that are selected, and then it returns a count of the. an example of the DISTINCT keyword, used with the following SongTitles table: SongID Artist Album Title 1 The Beatles Abbey Road Come Together 2 The Beatles Abbey Road Sun King 3 The Beatles

Ngày đăng: 05/07/2014, 05:20

Xem thêm: The Language of SQL- P23 docx

TỪ KHÓA LIÊN QUAN

Mục lục

    Chapter 1 Relational Databases and SQL

    Microsoft SQL Server, Oracle, and MySQL

    Primary and Foreign Keys

    The Significance of SQL

    Chapter 2 Basic Data Retrieval

    Column Names with Embedded Spaces

    Chapter 3 Calculations and Aliases

    The Function of Functions

    Sorting in Ascending Order

    Sorting in Descending Order

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN