SQL学习备忘

1.按照拼音首字母的正序或倒序排序

1 SELECT

2     CREATOR_REALNAME

3 FROM

4     tableName

5 ORDER BY NLSSORT(CREATOR_REALNAME, 'NLS_SORT = SCHINESE_PINYIN_M') desc
拼音排序

参考:nls_sort和nlssort 排序功能介绍

 

2.按照创建时间降序排序:即按照创建时间从大道小排序,(时间从数轴上看,最新时间在右,就越大,老时间在左,就越小);所以按照创建时间降序排序的结果就是最新的数据在前,老数据在后;

ORDER BY CREATE_TIME DESC,MODEL_NAME ASC

 

3.CASE 语句的使用:

select p.wb_id as 工作包ID, p.wb_name as 工作包名称,m.model_id as 项目ID, m.model_name as 项目名称,

       case m.model_flag when '0' then '基础WBS' when '1' then '项目WBS' end as 所属WBS

  from km_process p, km_model_info m

 where p.model_id=m.model_id

   and p.wb_id='20130911000348'

 

4.三层结构+查询的条数,分装的Oracle的分页查询

 1 SELECT *

 2   FROM (SELECT COUNT(1) CNT

 3           FROM (SELECT case r.rela_table_tag

 4                          when 'process_piture' then

 5                           '图片文件'

 6                          when 'process_visio' then

 7                           'Visio文件'

 8                        end as rela_table_tag,

 9                        r.rela_table_code rela_table_code,

10                        f.file_id file_id,

11                        f.file_path file_path,

12                        f.original_file_name original_file_name,

13                        f.ext_name ext_name,

14                        f.model_id modelId,

15                        f.phase_name phase_name

16                   FROM KM_PROCESS_PICTURE_RELA r, KM_FILE_INFO f

17                  WHERE r.file_id = f.file_id

18                    AND r.rela_table_code = '20141014944972'

19                  ORDER BY f.create_time desc)),

20        (SELECT rela_table_tag,

21                rela_table_code,

22                file_id,

23                file_path,

24                original_file_name,

25                ext_name,

26                modelId,

27                phase_name

28           FROM (SELECT rela_table_tag,

29                        rela_table_code,

30                        file_id,

31                        file_path,

32                        original_file_name,

33                        ext_name,

34                        modelId,

35                        phase_name,

36                        ROWNUM AS MY_ROWNUM

37                   FROM (SELECT case r.rela_table_tag

38                                  when 'process_piture' then

39                                   '图片文件'

40                                  when 'process_visio' then

41                                   'Visio文件'

42                                end as rela_table_tag,

43                                r.rela_table_code rela_table_code,

44                                f.file_id file_id,

45                                f.file_path file_path,

46                                f.original_file_name original_file_name,

47                                f.ext_name ext_name,

48                                f.model_id modelId,

49                                f.phase_name phase_name

50                           FROM KM_PROCESS_PICTURE_RELA r, KM_FILE_INFO f

51                          WHERE r.file_id = f.file_id

52                            AND r.rela_table_code = '20141014944972'

53                          ORDER BY f.create_time desc))

54          WHERE MY_ROWNUM <= 10

55            AND MY_ROWNUM > 0)
Oracle分页查询

 

5.decode函数的使用

 1    SELECT  p.PHASE_ID,

 2             p.PHASE_NAME, 

 3             p.PHASE_CODE, 

 4                 p.MODEL_ID,

 5                 p.SOURCE_DATA_ID, 

 6                 decode(h.parent_id,null ,'-1', h.parent_id) PARENT_ID

 7             FROM  RDM_MODEL_PHASE p, erm_basic_dataitem_view h

 8          WHERE  p.DELETE_FLAG = '0'

 9            AND  p.SOURCE_DATA_ID = h.PHASE_ID

10            AND  p.MODEL_ID = ?
decode

 

6.Right,Left

 1 -- 39   

 2 select i.phase_id,

 3        decode(j.source_data_id,null,'-1',j.source_data_id) parent_id,

 4        i.phase_name,

 5        i.source_data_id

 6   from RDM_MODEL_PHASE i left join RDM_MODEL_PHASE j on i.parent_id=j.phase_id

 7  where i.delete_flag = '0'

 8    and i.model_id = '141352764572700067'

 9    

10 -- 11   

11 select i.phase_id,

12        decode(j.source_data_id,null,'-1',j.source_data_id) parent_id,

13        i.phase_name,

14        i.source_data_id

15   from RDM_MODEL_PHASE i right join RDM_MODEL_PHASE j on i.parent_id=j.phase_id

16  where i.delete_flag = '0'

17    and i.model_id = '141352764572700067' 

18    

19      --  39  11  28  

20 --原始 39           

21 select i.phase_id,i.parent_id, i.phase_name,i.source_data_id

22   from RDM_MODEL_PHASE i 

23  where i.delete_flag = '0'

24    and i.model_id = '141352764572700067' 
Right,Left左右关联查询

 

7.sql查找最小缺失值与重用被删除的键

8.Coalesce函数

9.oracle中的exists 和not exists 用法

你可能感兴趣的:(sql)