Các hàm cơ sở dữ liệu

Phần này nói về các hàm dùng với dữ liệu được tổ chức thành các hàng tương ứng với các bản ghi.

Biểu tượng Ghi chú

Phân loại Cơ sở dữ liệu có thể bị nhầm lẫn với một cơ sở dữ liệu được tích hợp trong LibreOffice. Tuy nhiên, không có sự liên hệ nào giữa một cơ sở dữ liệu trong LibreOffice và phân loại Cơ sở dữ liệu của LibreOffice Calc cả.


Dữ liệu ví dụ:

Các dữ liệu sau sẽ được dùng làm ví dụ để mô tả các chức năng của hàm:

Trong phạm vi A1:E10 ta sẽ liệt kê các em nhỏ được mời đi dự tiệc sinh nhật của Joe. Các thông tin sẽ được sắp xếp như sau: cột A là tên của em nhỏ, B là lớp, sau đó là tuổi, khoảng cách tới trường (tính bằng mét) và cân nặng (tính bằng kg).

A

B

C

D

E

1

Name

Grade

Age

Distance to School

Weight

2

Andy

3

9

150

40

3

Betty

4

10

1000

42

4

Charles

3

10

300

51

5

Daniel

5

11

1200

48

6

Eva

2

8

650

33

7

Grade

2

7

300

42

8

Greta

1

7

200

36

9

Harry

3

9

1200

44

10

Irene

2

8

1000

42

11

12

13

Name

Grade

Age

Distance to School

Weight

14

>600

15

16

DCOUNT

5


Công thức ở ô B16 là « =DCOUNT(A1:E10;0;A13:E14) »

Các tham số cho hàm của CSDL:

Các mục sau sẽ được dùng làm tham số cho tất cả các hàm CSDL:

Cơ sở dữ liệu là phạm vi ô được đặt làm cơ sở dữ liệu.

DatabaseField specifies the column where the function operates on after the search criteria of the first parameter is applied and the data rows are selected. It is not related to the search criteria itself. For the DatabaseField parameter you can enter a reference to a header cell or a number to specify the column within the Database area, starting with 1. To reference a column by means of the literal column header name, place quotation marks around the header name.

SearchCriteria là phạm vi các ô chứa tiêu chí tìm kiếm. Nếu bạn viết một vài tiêu chí tìm kiếm trong 1 hàng, chúng sẽ được hợp lại bằng toán tử AND (và). Nếu bạn viết các tiêu chí trong các hàng khác nhau, chúng sẽ được hợp lại bằng toán tử OR (hoặc). Các ô không có gì sẽ bị bỏ qua.

Choose - LibreOffice Calc - Calculate to define how LibreOffice Calc acts when searching for identical entries.

See also the Wiki page about Conditional Counting and Summation.

DAVERAGE

Hàm DAVERAGE trả về trị trung bình của giá trị của tất cả các ô (trường) nằm trong tất cả các hàng (bản ghi cơ sở dữ liệu) thỏa các tiêu chuẩn tìm kiếm đã xác định.

The search supports regular expressions. You can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must precede every character with a \ character. You can switch the automatic evaluation of regular expression on and off in - LibreOffice Calc - Calculate.

Cú pháp

DAVERAGE(Database; DatabaseField; SearchCriteria)

For the DatabaseField parameter you can enter a reference to a header cell or a number to specify the column within the Database area, starting with 1. To reference a column by means of the literal column header name, place quotation marks around the header name.

Ví dụ

Để tìm cân nặng trung bình của tất cả các đứa trẻ cùng tuổi trong mẫu thí dụ trên (xin hãy cuộn lên) thì nhập công thức này vào ô B16:

=DAVERAGE(A1:E10;"Weight";A13:E14)

Trong hàng 14, dưới « Age » (Tuổi), nhập 7, 8, 9, v.v., cái này sau cái kia. Kết quả là cân nặng trung bình của tất cả các đứa trẻ cùng tuổi.

DCOUNT

DCOUNT đếm số dòng (hay số bản ghi) trong CSDL thoả mãn điều kiện tìm kiếm và chứa các giá trị số.

The search supports regular expressions. You can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must precede every character with a \ character. You can switch the automatic evaluation of regular expression on and off in - LibreOffice Calc - Calculate.

Cú pháp

DCOUNT(Database; DatabaseField; SearchCriteria)

If the DatabaseField argument is omitted, DCOUNT returns the count of all records that satisfy Criteria. For the DatabaseField parameter you can enter a reference to a header cell or a number to specify the column within the Database area, starting with 1. To reference a column by means of the literal column header name, place quotation marks around the header name.

Ví dụ

Trong thí dụ trên (xin hãy cuộn lên), muốn biết mấy đứa trẻ cần phải đi trường học xa hơn 600 mét. Kết quả sẽ được cất giữ trong ô B16. Đặt con trỏ vào ô B16. Nhập công thức =DCOUNT(A1:E10;0;A13:E14) vào ô B16. Trợ lý Hàm có thể giúp bạn nhập phạm vi dữ liệu.

Database là phạm vi dữ liệu cần ước lượng, bao gồm các phần đầu: trong trường hợp này « A1:E10 ». DatabaseField ghi rõ cột cho các tiêu chuẩn tìm kiếm: trong trường hợp này, toàn bộ cơ sở dữ liệu. SearchCriteria là phạm vi vào đó bạn có thể nhập các tham số tìm kiếm: trong trường hợp này « A13:E14 ».

Để tìm xem bao nhiêu đứa trẻ trong lớp 2 lớn hơn 7 tuổi, xoá mục nhập « >600 » trong ô D14 và nhập 2 vào ô B14 dưới Grade (Lớp), và nhập >7 vào ô C14 ở bên phải. Kết quả là 2. Có hai đứa trẻ trong lớp 2 lớn hơn 7 tuổi. Vì cả hai tiêu chuẩn đều nằm trong cùng một hàng, chúng được kết nối với nhau bằng toán tử AND (và).

DCOUNTA

Hàm DCOUNTA đếm số hàng (bản ghi) trong một cơ sở dữ liệu thỏa các điều kiện tìm kiếm đã xác định và chứa giá trị kiểu số hoặc chữ và số.

The search supports regular expressions. You can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must precede every character with a \ character. You can switch the automatic evaluation of regular expression on and off in - LibreOffice Calc - Calculate.

Cú pháp

DCOUNTA(Database; DatabaseField; SearchCriteria)

If the DatabaseField argument is omitted, DCOUNTA returns the count of all records that satisfy Criteria. For the DatabaseField parameter you can enter a reference to a header cell or a number to specify the column within the Database area, starting with 1. To reference a column by means of the literal column header name, place quotation marks around the header name.

Ví dụ

Trong mẫu thí dụ trên (xin hãy cuộn lên), bạn có thể tìm kiếm số đứa trẻ có tên bắt đầu với chữ E hoặc xếp sau E trong bảng chữ cái. Chỉnh sửa công thức trong ô B16 để hiển thị =DCOUNTA(A1:E10;"Tuổi";A13:E14). Xoá các tiêu chuẩn tìm kiếm cũ, và nhập >=E dưới "Tên" trong trường A14. Kết quả là 5. Bây giờ nếu bạn xoá tất cả các giá trị số cho Greta trong hàng 8, kết quả thay đổi thành 4. Hàng 8 không còn được đếm lại vì nó không chứa giá trị. Tên "Greta" là chữ, không phải một giá trị số. Lưu ý: tham số "DatabaseField" (trường cơ sở dữ liệu) cần phải trỏ tới một cột chứa giá trị.

DGET

Hàm DGET trả về nội dung của ô đã tham chiếu trong một cơ sở dữ liệu mà tương ứng với tiêu chuẩn tìm kiếm đã ghi rõ. Trong trường hợp gặp lỗi, hàm trả về hoặc « #VALUE! » (không tìm thấy hàng), hoặc « Err502 » (tìm thấy nhiều hơn một ô).

The search supports regular expressions. You can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must precede every character with a \ character. You can switch the automatic evaluation of regular expression on and off in - LibreOffice Calc - Calculate.

Cú pháp

DGET(Database; DatabaseField; SearchCriteria)

For the DatabaseField parameter you can enter a reference to a header cell or a number to specify the column within the Database area, starting with 1. To reference a column by means of the literal column header name, place quotation marks around the header name.

Ví dụ

Trong mẫu thí dụ trên (xin hãy cuộn lên), muốn xác định lớp học của đứa trẻ có tên trong ô A14. Công thức được nhập vào ô B16 và khác một ít với các mẫu thí dụ trước vì chỉ một cột (một trường cơ sở dữ liệu) có thể được nhập cho tham số DatabaseField. Nhập công thức này:

=DGET(A1:E10;"Grade";A13:E14)

Nhập tên Frank vào ô A14 thì bạn thấy kết quả 2, vì Frank học lớp 2. Nhâp « Age » (Tuổi) thay cho « Grade » (Lớp) thì bạn thấy Frank có mấy tuổi.

Hoặc nhập giá trị 11 chỉ vào ô C14, và xoá các mục nhập khác trong hàng này. Chỉnh sửa công thức trong ô B16 như sau :

=DGET(A1:E10;"Name";A13:E14)

Thay cho lớp, tên được truy vận. Phép giải xuất hiện ngay: Daniel là đứa trẻ duy nhất có 11 tuổi.

DMAX

Hàm DMAX trả về giá trị lớn nhất của một ô (trường) trong một cơ sở dữ liệu (mọi bản ghi) thỏa các tiêu chuẩn tìm kiếm đã xác định.

The search supports regular expressions. You can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must precede every character with a \ character. You can switch the automatic evaluation of regular expression on and off in - LibreOffice Calc - Calculate.

Cú pháp

DMAX(Database; DatabaseField; SearchCriteria)

For the DatabaseField parameter you can enter a reference to a header cell or a number to specify the column within the Database area, starting with 1. To reference a column by means of the literal column header name, place quotation marks around the header name.

Ví dụ

Đối với mẫu thí dụ trên, để tìm biết cân nặng của đứa trẻ nặng nhất trong mỗi lớp (xin hãy cuộn lên), nhập công thức này vào ô B16:

=DMAX(A1:E10;"Weight";A13:E14)

Dưới Grade (Lớp), nhập 1, 2, 3, v.v., cái này sau cái kia. Một khi nhập một số lớp thì chương trình hiển thị cân nặng của đứa trẻ nặng nhất.

DMIN

Hàm DMIN trả về nội dung tối thiểu của một ô (trường) trong một cơ sở dữ liệu mà tương ứng với tiêu chuẩn tìm kiếm đã ghi rõ.

The search supports regular expressions. You can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must precede every character with a \ character. You can switch the automatic evaluation of regular expression on and off in - LibreOffice Calc - Calculate.

Cú pháp

DMIN(Database; DatabaseField; SearchCriteria)

For the DatabaseField parameter you can enter a reference to a header cell or a number to specify the column within the Database area, starting with 1. To reference a column by means of the literal column header name, place quotation marks around the header name.

Ví dụ

Để tìm khoảng cách trường hợp mà ngắn nhất cho các đứa trẻ trong mỗi lớp trong mẫu thí dụ trên (xin hãy cuộn lên), nhập công thức này vào ô B16:

=DMIN(A1:E10;"Distance to School";A13:E14)

Trong hàng 14, dưới « Grade » (Lớp), nhập 1, 2, 3, v.v., cái này sau cái kia. Kết quả là khoảng cách trường học mà ngắn nhất cho mỗi lớp.

DPRODUCT

Hàm DPRODUCT thì nhân tất cả các ô của một phạm vi dữ liệu trong đó nội dung ô tương ứng với tiêu chuẩn tìm kiếm.

The search supports regular expressions. You can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must precede every character with a \ character. You can switch the automatic evaluation of regular expression on and off in - LibreOffice Calc - Calculate.

Cú pháp

DPRODUCT(Database; DatabaseField; SearchCriteria)

For the DatabaseField parameter you can enter a reference to a header cell or a number to specify the column within the Database area, starting with 1. To reference a column by means of the literal column header name, place quotation marks around the header name.

Ví dụ

Đối với mẫu thí dụ tiệc sinh nhật trên (xin hãy cuộn lên), không thể áp dụng hàm này một cách có ích.

DSTDEV

Hàm DSTDEV tính độ lệch chuẩn của một dân số dựa vào một mẫu, dùng những số trong một cột cơ sở dữ liệu tương ứng với các điều kiện đã cho. Các bản ghi được xem như là một mẫu dữ liệu. Nghĩa là những đứa trẻ trong ví dụ đại diện cho một mặt cắt của tất cả các đứa trẻ. Lưu ý rằng không thể thu được một kết quả đại diện từ một mẫu nhỏ hơn một nghìn.

The search supports regular expressions. You can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must precede every character with a \ character. You can switch the automatic evaluation of regular expression on and off in - LibreOffice Calc - Calculate.

Cú pháp

DSTDEV(Database; DatabaseField; SearchCriteria)

For the DatabaseField parameter you can enter a reference to a header cell or a number to specify the column within the Database area, starting with 1. To reference a column by means of the literal column header name, place quotation marks around the header name.

Ví dụ

Để tìm độ lệch chuẩn của cân nặng của tất cả các đứa trẻ cùng tuổi trong mẫu thí dụ (xin hãy cuộn lên), nhập công thức này vào ô B16:

=DSTDEV(A1:E10;"Weight";A13:E14)

Trong hàng 14, dưới « Age » (Tuổi), nhập 7, 8, 9, v.v., cái này sau cái kia. Kết quả là độ lệch chuẩn của cân nặng của tất cả các đứa trẻ cùng tuổi này.

DSTDEVP

Hàm DSTDEVP tính độ lệch chuẩn của một dân số dựa vào tất cả các ô của một phạm vi dữ liệu thỏa các tiêu chuẩn tìm kiếm đã cho. Các bản ghi trong ví dụ được xử lý như là toàn bộ dân số.

The search supports regular expressions. You can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must precede every character with a \ character. You can switch the automatic evaluation of regular expression on and off in - LibreOffice Calc - Calculate.

Cú pháp

DSTDEVP(Database; DatabaseField; SearchCriteria)

For the DatabaseField parameter you can enter a reference to a header cell or a number to specify the column within the Database area, starting with 1. To reference a column by means of the literal column header name, place quotation marks around the header name.

Ví dụ

Để tìm độ lệch chuẩn của cân nặng cho tất cả các đứa trẻ cùng tuổi ở tiệc sinh nhật của Joe (xin hãy cuộn lên), nhập công thức này vào ô B16:

=DSTDEVP(A1:E10;"Weight";A13:E14)

Trong hàng 14, dưới « Age » (Tuổi), nhập 7, 8, 9,, v.v., cái này sau cái kia. Kết quả là độ lệch chuẩn của cân nặng cho tất cả các trẻ con cùng tuổi cho ta đã kiểm tra cân nặng.

DSUM

Hàm DSUM trả về tổng của tất cả các ô trong một trường cơ sở dữ liệu nào đó trong tất cả các hàng (bản ghi) thỏa các tiêu chuẩn tìm kiếm đã cho.

The search supports regular expressions. You can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must precede every character with a \ character. You can switch the automatic evaluation of regular expression on and off in - LibreOffice Calc - Calculate.

Cú pháp

DSUM(Database; DatabaseField; SearchCriteria)

For the DatabaseField parameter you can enter a reference to a header cell or a number to specify the column within the Database area, starting with 1. To reference a column by means of the literal column header name, place quotation marks around the header name.

Ví dụ

Để tìm biết chiều dài của tổ hợp các khoảng cách đi trường hợp của tất các đứa trẻ ở tiệc sinh nhật của Joe (xin hãy cuộn lên) thì nhập công thức này vào ô B16:

=DSUM(A1:E10;"Distance to School";A13:E14)

Nhập 2 vào hàng 14 dưới « Grade ». Vậy chương trình hiển thị tổng (1950) các khoảng cách đi trường học của tất cả các đứa trẻ học lớp 2.

DVAR

Hàm DVAR trả về phương sai của tất cả các ô của một trường cơ sở dữ liệu trong tất cả các bản ghi thỏa các tiêu chuẩn tìm kiếm đã cho. Những bản ghi trong ví dụ được xử lý như là một mẫu dữ liệu. Không thể thu được một kết quả đại diện từ một dân số nhỏ hơn một nghìn.

The search supports regular expressions. You can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must precede every character with a \ character. You can switch the automatic evaluation of regular expression on and off in - LibreOffice Calc - Calculate.

Cú pháp

DVAR(Database; DatabaseField; SearchCriteria)

For the DatabaseField parameter you can enter a reference to a header cell or a number to specify the column within the Database area, starting with 1. To reference a column by means of the literal column header name, place quotation marks around the header name.

Ví dụ

Để tìm biết độ lệch của cân nặng của tất cả các đứa trẻ cùng tuổi trong mẫu thí dụ trên (xin hãy cuộn lên), nhập công thức này vào ô B16:

=DVAR(A1:E10;"Weight";A13:E14)

Trong hàng 14, dưới « Age » (Tuổi), nhập 7, 8, 9, v.v., cái này sau cái kia. Bạn sẽ thấy kết quả là độ lệch của các giá trị cân nặng cho tất cả các đứa trẻ cùng tuổi này.

DVARP

Hàm DVARP tính phương sai của tất cả các giá trị ô trong một trường cơ sở dữ liệu trong tất cả các bản ghi thỏa các tiêu chuẩn tìm kiếm đã cho. Những bản ghi trong ví dụ được xử lý như là toàn bộ dân số.

The search supports regular expressions. You can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must precede every character with a \ character. You can switch the automatic evaluation of regular expression on and off in - LibreOffice Calc - Calculate.

Cú pháp

DVARP(Database; DatabaseField; SearchCriteria)

For the DatabaseField parameter you can enter a reference to a header cell or a number to specify the column within the Database area, starting with 1. To reference a column by means of the literal column header name, place quotation marks around the header name.

Ví dụ

Để tìm phương sai của cân nặng của tất cả các đứa trẻ cùng tuổi ở tiệc sinh nhật của Joe (xin hãy cuộn lên), nhập công thức này vào ô B16:

=DVARP(A1:E10;"Weight";A13:E14)

Trong hàng 14, dưới « Age » (Tuổi), nhập 7, 8, 9,, v.v., cái này sau cái kia. Kết quả là phương sai của các giá trị cân nặng cho tất cả các đứa trẻ cùng tuổi này mà đến tiệc sinh nhật của Joe.