【oracle/語法】With語句的寫法


Sample1:

with temp AS (
  select * from emp where rownum<11)
select * from temp

Result:

SQL> with temp AS (
  2    select * from emp where rownum<11)
  3  select * from temp;

        ID NAME                                            AGE     SALARY CREATE_TIME
---------- ---------------------------------------- ---------- ---------- ---------------------------------------------------------------------------
       561 NDRKPHCF                                         40      43533 07-9月 -21 06.32.42.000000 下午
       562 VOMTMOBLCBVKIGVOZSN                              33      23063 07-9月 -21 06.32.42.000000 下午
       563 GKDDBKTFAUON                                     63      14078 07-9月 -21 06.32.42.000000 下午
       564 MHCEIQMT                                         64      11360 07-9月 -21 06.32.42.000000 下午
       565 IVKRXCHYROE                                      28      38106 07-9月 -21 06.32.42.000000 下午
       566 GROURMS                                          25      33055 07-9月 -21 06.32.42.000000 下午
       567 BEUEKLUL                                         63      23546 07-9月 -21 06.32.42.000000 下午
       568 QIITGBC                                          41      45708 07-9月 -21 06.32.42.000000 下午
       569 OTSCIZLDWMJZFV                                   32      28990 07-9月 -21 06.32.42.000000 下午
       570 KQZFWBK                                          64      45629 07-9月 -21 06.32.42.000000 下午

已選擇10行。

 

Sample2:

with cityOldestWomen as (
select id,name,gender,cityname,birthymd from (
select 
id,name,gender,cityname,to_char(birthday,'yyyy-MM-dd') as birthymd, 
rank() over (partition by cityname order by birthday) as seq
from customer
where gender='f')
where seq=1
order by id)
select * from cityOldestWomen

Result:

SQL> with cityOldestWomen as (
  2  select id,name,gender,cityname,birthymd from (
  3  select
  4  id,name,gender,cityname,to_char(birthday,'yyyy-MM-dd') as birthymd,
  5  rank() over (partition by cityname order by birthday) as seq
  6  from customer
  7  where gender='f')
  8  where seq=1
  9  order by id)
 10  select * from cityOldestWomen;

        ID NAME                                     G CITYNAME                                 BIRTHYMD
---------- ---------------------------------------- - ---------------------------------------- ----------
         1 XGZLORILKOGWELLJI                        f 山海關                                   1970-01-02
         2 JFMOXKKFDJPSNBNQS                        f 綏中                                     1970-01-03
         3 WKMIILRQAKY                              f 北戴河                                   1970-01-04
        23 MKSREQXPKJTWSK                           f 興城                                     1970-01-24
        30 ILXYETNXOKXSB                            f 津濱                                     1970-01-31

SQL>

END


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM