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

Oracle XSQL- P11 doc

20 254 0

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 20
Dung lượng 199,99 KB

Nội dung

Table 8.22 Date-Format Examples SQL RESULT SELECT to_char(current_timestamp, 2002-05-19 'YYYY-MM-DD HH24:MI:SS.FF') 01:01:07.000001 AS date_str FROM dual SELECT to_char year: 2002 (sysdate,'"year": YYYY') AS date_str FROM dual SELECT to_char SUNDAY, MAY 19, 2002 AD (sysdate,'DAY, MONTH DD, YYYY AD') AS date_str FROM dual NOTE You can change the default date format by editing the init.ora file or by using the ALTER SESSION statement to change it for a particular SQL session. Altering the session is generally impractical for XSQL and can’t be done from the xsql:query action. If you change the date format in the init.ora file, then the default date format will be changed for everyone on your database. This may be ill-advised if your database has other applications. Date-Format Elements An SQL date format mask is made up of elements that are replaced by the appropriate value from the date in the output. Table 8.22 lists some examples of date formats. Table 8.23 lists all the elements that you can use in date formats. Table 8.23 Date-Format Elements ELEMENT DESCRIPTION -/,.;: These punctuation marks can appear anywhere in the format mask. They will be included verbatim in the output. "text" Quoted text can be included anywhere in the format mask. It will be included verbatim in the output. AD, A.D., BC, or B.C. Outputs if the date is A.D. or B.C. AM, A.M., PM,orP.M. Outputs if the time is A.M. or P.M. CC Century number. SCC Century number with B.C. dates represented by a negative number. 180 Chapter 8 Table 8.23 (Continued) ELEMENT DESCRIPTION D Numerical day of the week. Sunday is 1 and Saturday is 7. DAY Full name of the day (e.g., Sunday). DD Day of the month. DDD Day of the year. DY Abbreviated day (e.g., Sun). E Abbreviated era name (only valid for Japanese Imperial, Republic of China Official, and Thai Buddha calendars). EE Full era name (only valid for Japanese Imperial, Republic of China Official, and Thai Buddha calendars). FF Fractional seconds. HH, HH12 Hour of the day on a 12-hour clock. HH24 Hour of the day on a 24-hour clock. IW Week of the year. I One-digit year. IY Two-digit year. IYY Three-digit year. IYYY Four-digit year. J Julian day or days since the last day of the year 4713 B.C. Day is January 1, 4712 B.C. MI Minute. MM Month number. 1 is January and 12 is December. MON Three-letter month abbreviation. MONTH Full month name (e.g., January) Q Quarter of the year (e.g., quarter 1 is January to March). RM Month number is given in Roman numerals. RR Last two digits of the year, when used with TO_CHAR. RRRR Last four digits of the year, when used with TO_CHAR. SS TSecond. (continues) Oracle SQL 181 Table 8.23 Date-Format Elements (Continued) ELEMENT DESCRIPTION SSSSS Seconds since midnight. TZD Daylight saving time information. TZH Timezone hour. TZM Timezone minute. TZR Timezone region information. WW Week of the year. W Week of the month, with week one starting on the first of the month. YEAR Year spelled out in words. SYEAR Like YEAR, with a negative sign in front if it is a B.C. year. X Local radix character. Y One-digit year. YY Two-digit year. YYY Three-digit year. YYYY Four-digit year. SYYYY Like YYYY, with a negative sign in front if it is a B.C. year. SQL Functions Oracle has a vast array of built-in functions that you can use in your SQL statements. You’ve already seen examples of some of the functions, such as to_date and to_char. Here, you’ll see all the functions broken down by type. Covered first are aggregate functions. These functions, such as max, take a data set and aggregate it in to one value. The numeric functions allow you to do math in your SQL; the character functions, to manipulate and get information on strings; the date functions, to provide operations on dates. There is also a set of conversion functions, as well as— inevitably—the miscellaneous functions. WARNING As discussed previously, the default element name for functions will usually be an illegal XSQL name. When using with select statements, you must alias your function names to legal XML element names or XSQL will choke on your query. 182 Chapter 8 These functions are Oracle expressions and can be used anywhere an Oracle expres- sion can be used. For instance, you can use them in the elements clause or the where clause of a select statement or in the values clauses of insert and update statements. Using Aggregate Functions Aggregate functions summarize data. However, since they return only one value, they aren’t strictly compatible with other elements in a select statement. The lack of com- patibility has a couple of implications. The first implication is solved by the GROUP BY and HAVING clauses, which allow you to group the elements before you apply aggre- gate functions. Aggregate functions also allow you to use the DISTINCT keyword. From this keyword, you can access the actual functions themselves. Before beginning, it’s good to know how the aggregate functions deal with NULL values. NULL values are ignored. GROUP BY Clauses Group by clauses make your aggregate functions more valuable. With a single state- ment, you can get separate aggregates across your data. To get a breakdown of job- based salary, you would use the GROUP BY function as follows: SELECT job,max(sal) AS max_sal FROM emp GROUP BY job Table 8.24 lists the resulting output. You can use GROUP BY in conjunction with joins and have multiple group by expressions, as follows: SELECT job,dname,max(sal) AS max_sal FROM emp,dept WHERE emp.deptno=dept.deptno GROUP BY job,dname Table 8.25 lists the results. Table 8.24 Group By Job JOB max_sal ANALYST 3000 CLERK 1300 MANAGER 2975 PRESIDENT 5000 SALESMAN 1600 Oracle SQL 183 Table 8.25 Group By Job and Department Name JOB DEPARTMENT NAME max_sal ANALYST RESEARCH 3000 CLERK ACCOUNTING 1300 CLERK RESEARCH 1100 CLERK SALES 950 MANAGER ACCOUNTING 2450 MANAGER RESEARCH 2975 MANAGER SALES 2850 PRESIDENT ACCOUNTING 5000 SALESMAN SALES 1600 The GROUP BY clause can be used in conjunction with the HAVING clause to restrict the end output. It works a lot like the where clause, except that it works to exclude the rows based on the value of the aggregate. Here’s an example in which you get only the job and department name pairs where the max_sal is greater than 2,500: SELECT job,dname,max(sal) FROM emp,dept WHERE emp.deptno=dept.deptno GROUP BY job,dname HAVING max(sal)>2500 Aggregate Functions and DISTINCT Keyword Earlier, you learned that the DISTINCT keyword returns only the distinct values for a column. What if you want an aggregate function to work only on distinct values? You can do that by simply specifying DISTINCT when calling the function. Here is an example that gives you the count of distinct jobs in the emp table: SELECT count(distinct job) AS distinct_job, count(job) AS job FROM emp Table 8.26 lists the results. Table 8.26 Distinct Values and Aggregates Distinct_job JOB 514 184 Chapter 8 Avg The AVG function averages all the values in its domain of rows. If a GROUP BY clause is used, the function will be performed once for each value or unique sets of values specified by the GROUP BY clause. AVG works on numeric values only. The following are some examples of the AVG function. SELECT avg(sal) AS avg_sal FROM emp SELECT DISTINCT avg(DISTINCT sal) AS avg_distinct_sal FROM emp Table 8.27 lists the results. Table 8.27 avg(sal) and avg(distinct sal) Results avg_sal avg_distinct_sal 2073.21429 2064.58333 SELECT job, avg(sal) AS avg_sal FROM emp GROUP BY job Table 8.28 lists the results. Table 8.28 Grouped All and Distinct Results JOB AVG_SAL ANALYST 3000 CLERK 1037.5 MANAGER 2758.33333 PRESIDENT 5000 SALESMAN 1400 UPDATE dummy_emp SET sal=(SELECT avg(sal) FROM emp) WHERE dummy_emp.ename=’ADAMS’; Oracle SQL 185 Count The count function counts all the specified values in its domain of rows. If a GROUP BY clause is used, the function will be performed once for each value or unique sets of values specified by the GROUP BY clause. Count works on any data type. The following are some examples and their results. SELECT count(ename) AS count_ename FROM emp Table 8.29 lists the results. Table 8.29 COUNT(ename) Results count_ename 14 SELECT deptno, count(DISTINCT job) AS count_job FROM emp GROUP BY deptno Table 8.30 lists the results. Table 8.30 Grouped Results DEPTNO count_job 10 3 20 3 30 3 Max The max function averages all the values in its domain of rows. If a GROUP BY clause is used, the function will be performed once for each value or unique sets of values specified by the GROUP BY clause. Max works on numeric values only. The following are some examples. SELECT max(sal) AS max_sal FROM emp Table 8.31 lists the results. 186 Chapter 8 Table 8.31 max(sal) Results max_sal 5000 SELECT job, max(sal) AS max_sal FROM emp GROUP BY job Table 8.32 lists the results. Table 8.32 Grouped All and Distinct Results JOB AVG_SAL ANALYST 3000 CLERK 1300 MANAGER 2975 PRESIDENT 5000 SALESMAN 1600 UPDATE dummy_emp SET sal=(SELECT max(sal) FROM emp) WHERE dummy_emp.ename=’ADAMS’; Min The min function averages all the values in its domain of rows. If a GROUP BY clause is used, the function will be performed once for each value or unique sets of values specified by the GROUP BY clause. Min works on numeric values only. The following are some examples and their results. SELECT min(sal) AS min_sal FROM emp Table 8.33 lists the results. Oracle SQL 187 Table 8.33 min(sal) Results min_sal 800 SELECT job, max(sal) AS max_sal FROM emp GROUP BY job Table 8.34 lists the results. Table 8.34 Grouped All and Distinct Results JOB AVG_SAL ANALYST 3000 CLERK 800 MANAGER 2450 PRESIDENT 5000 SALESMAN 1250 UPDATE dummy_emp SET sal=(SELECT min(sal) FROM emp) WHERE dummy_emp.ename=’ADAMS’; Stddev The stddev function averages all the values in its domain of rows. If a GROUP BY clause is used, the function will be performed once for each value or unique sets of val- ues specified by the GROUP BY clause. Stddev works on numeric values only. The following are some examples. SELECT stddev(sal) AS stddev_sal FROM emp Table 8.35 lists the results. 188 Chapter 8 Table 8.35 stddev(sal) Results stddev_sal 1182.50322 SELECT stddev(DISTINCT sal) AS stddev_sal FROM emp Table 8.36 lists the results. Table 8.36 max(sal) Results stddev_sal 1229.95096 SELECT job, stddev(sal) AS stddev_sal FROM emp GROUP BY job Table 8.37 lists the results. Table 8.37 Grouped All and Distinct Results JOB AVG_SAL ANALYST 0 CLERK 213.600094 MANAGER 274.241378 PRESIDENT 0 SALESMAN 177.951304 UPDATE dummy_emp SET sal=(SELECT stddev(sal) FROM emp) WHERE dummy_emp.ename=’ADAMS’; Oracle SQL 189 [...]... Results stddev_sal 24775 SELECT job, sum(sal) AS sum_sal FROM emp GROUP BY job Table 8.40 lists the results Table 8.40 Grouped All and Distinct Results JOB SUM_SAL ANALYST 6000 CLERK 4150 MANAGER 8275 Oracle SQL Table 8.40 Grouped All and Distinct Results (Continued) JOB SUM_SAL PRESIDENT 5000 SALESMAN 560 UPDATE dummy_emp SET sal=(SELECT sum(sal) FROM emp) WHERE dummy_emp.ename=’ADAMS’; Variance The... absolute value of a number Returns are NULL if given a null value SELECT abs(10) AS positive,abs(-10) AS negative FROM dual Table 8.44 lists the results Table 8.44 Results of Abs positive negative 10 10 Oracle SQL Bin_to_num Bin_to_num converts a bit vector to a number SELECT bin_to_num(1,1,1,1) bin_to_num(1,0,0,1) bin_to_num(0,1,0,1) bin_to_num(0,0,0,1) FROM dual AS AS AS AS fifteen, nine, five, one... intermixed in the parameter list, but dates use the default date mask It’s best to convert dates to strings first with the to_char function SELECT least(deptno,21) FROM dept Table 8.49 lists the results Oracle SQL Table 8.49 Least Results LEAST_NUM 10 20 21 21 Mod Mod returns the modulo of two numbers—the remainder after the first number is divided by the second SELECT mod(4,3) AS r_4_mod_3, mod(8,3)... number is positive, zero, or negative SELECT sign(60) AS positive, sign(0) AS zero, sign(-77.77) AS negative FROM dual Table 8.53 lists the results Table 8.53 Sign Results positive zero negative 1 0 -1 Oracle SQL Sqrt Sqrt provides the square root of a number Negative numbers aren’t allowed: SELECT sqrt(100) AS square_root FROM dual Table 8.54 lists the results Table 8.54 Sqrt Results square_root 10... The value returned is based on the character set of the database and is only an ascii value if a 7-bit ascii value is in use SELECT ascii(‘a’) AS lower_a, ascii(‘A’) AS upper_a, ascii(‘z’) AS lower_z, Oracle SQL ascii(‘Z’) AS upper_z, ascii(‘0’) AS zero, ascii(‘9’) AS nine FROM DUAL Table 8.58 lists the results Table 8.58 LOWER_A 97 ASCII Results UPPER_A LOWER_Z UPPER_Z ZERO NINE 65 122 90 48 57 Bitand . names or XSQL will choke on your query. 182 Chapter 8 These functions are Oracle expressions and can be used anywhere an Oracle expres- sion can be used. For instance, you can use them in the elements. Four-digit year. SYYYY Like YYYY, with a negative sign in front if it is a B.C. year. SQL Functions Oracle has a vast array of built-in functions that you can use in your SQL statements. You’ve already. with TO_CHAR. RRRR Last four digits of the year, when used with TO_CHAR. SS TSecond. (continues) Oracle SQL 181 Table 8.23 Date-Format Elements (Continued) ELEMENT DESCRIPTION SSSSS Seconds since

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

TỪ KHÓA LIÊN QUAN

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

TÀI LIỆU LIÊN QUAN