ORACLE語句常用技巧收集

來源:互聯網
上載者:User
Simple CASE Example

For each customer in the sample oe.customers table, the following statement lists the credit limit as "Low" if it equals $100, "High" if it equals $5000, and "Medium" if it equals anything else.

SELECT cust_last_name,   CASE credit_limit WHEN 100 THEN 'Low'   WHEN 5000 THEN 'High'   ELSE 'Medium' END   FROM customers;CUST_LAST_NAME       CASECR-------------------- ------...Bogart               MediumNolte                MediumLoren                MediumGueney               Medium





Simple FunctionExample





CREATE FUNCTION f(cur SYS_REFCURSOR, mgr_hiredate DATE) RETURN NUMBER IS emp_hiredate DATE; before number :=0; after number:=0;begin loop fetch cur into emp_hiredate; exit when cur%NOTFOUND; if emp_hiredate > mgr_hiredate then after:=after+1; else before:=before+1; end if; end loop; close cur; if before > after then return 1; else return 0; end if;end;Simple Date Example

The following example converts the datetime value of one time zone to another time zone:

SELECT FROM_TZ(CAST(TO_DATE('1999-12-01 11:00:00',       'YYYY-MM-DD HH:MI:SS') AS TIMESTAMP), 'America/New_York')    AT TIME ZONE 'America/Los_Angeles' "West Coast Time"    FROM DUAL;West Coast Time------------------------------------------------01-DEC-99 08.00.00.000000 AM AMERICA/LOS_ANGELES
Simple GROUP BY clause, you can use either the upper or lower form of expression list:
SELECT department_id, MIN(salary), MAX(salary)   FROM employees   GROUP BY department_id, salary;SELECT department_id, MIN(salary), MAX(salary)   FROM employees   GROUP BY (department_id, salary);

In ROLLUP, CUBE, and GROUPING SETS clauses of GROUP BY clauses, you can combine individual expressions with sets of expressions in the same expression list. The following example shows several valid grouping sets expression lists in one SQL statement:

SELECT prod_category, prod_subcategory, country_id, cust_city, count(*)   FROM  products, sales, customers   WHERE sales.prod_id = products.prod_id    AND sales.cust_id=customers.cust_id    AND sales.time_id = '01-oct-00'   AND customers.cust_year_of_birth BETWEEN 1960 and 1970GROUP BY GROUPING SETS   (   (prod_category, prod_subcategory, country_id, cust_city),   (prod_category, prod_subcategory, country_id),   (prod_category, prod_subcategory),    country_id  );








相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.