SQL work commonly used functions (Oracle/PLSQL)

SQL work commonly used functions (Oracle/PLSQL)

--整体代码示例
SELECT * FROM(
SELECT airline,
       substr(date,1,4),
       case when date between '2018-01-01' and  '2018-08-31'  then date
         else to_char(to_date(date,'yyyy-mm-dd')+364,'yyyy-mm-dd') end 日期,
       decode(新航班号,null,flight_no,新航班号) 航班号,
       sum(ticket_income_cki + account_fuel) / nullif(sum(order_seat * standard_price), 0) 折扣,
       sum(order_seat * segment_space) / nullif(sum(supply_seat * segment_space), 0) 客座, 
FROM   database0 t
WHERE  (dep_date between '2018-01-01' and '2018-08-31'
        or dep_date between '2017-01-01' and '2017-08-31')
       and length(trim(translate(flight_no,'0123456789',' '))) is null  
GROUP BY airline,
         substr(flight_date,1,4),
         case when date between '2018-01-01' and  '2018-08-31'  then date
              else to_char(to_date(date,'yyyy-mm-dd')+364,'yyyy-mm-dd') end,
                                                           --注意这里没有'日期'
         decode(新航班号,null,flight_no,新航班号))
         PIVOT(sum(折扣) 折扣, sum(客座) 客座 forin ('2017', '2018'))
  1. substr(variable, start_num, num_chars)

    substr is equivalent to the MID function in excel, and the statements of substr must be grouped in group by (when aggregate functions such as sum are used)

    substr(date,1,4)--获取年份,date格式为20181105
    substr(flight_no,-1) --相当于excel的RIGHT函数,截取最右边的n个字符
    
  2. decode(variable, value_A, ‘A’, value_B, ‘B’, ‘Other’)

    Recoding function, when variable=a, change to b (the function is weak, it can only be used to modify when it is equal to a certain value, if you want to recode when it is in a certain range, use case when), the decoded statement must be grouped in group by

    decode(sale,1000,'D',2000,'C',3000,'B',4000,'A',’Other’) --将不同的价格重编码为不同等级,其他改为'Other'
    decode(value,1000,'D',2000,'C',3000,'B',4000,'A', value) --只修改几个值,其他保留为value本身不修改
    
  3. case…when…end

    The case when statement is generally used to recode to generate new variables, and the case when statement must be grouped in group by

    case when date between '2018-01-01' and  '2018-08-31'  then '今年'
         when date between '2017-01-01' and  '2017-08-31'  then '去年'
         else '前年' end 年份 --将日期重编码生成年份变量
    
    case when date between '2018-01-01' and  '2018-08-31'  then date
         else to_char(to_date(date,'yyyy-mm-dd')+364,'yyyy-mm-dd') end 日期
         --将去年的日期按星期对应改为今年的日期,今年的日期保持不变
    
  4. length/trim/translate

    length/trim/translate are all functions for processing strings

    length() -- calculate the length

    trim() -- remove spaces

    translate -- translate a character to a character

    length(trim(translate(flight_no,'0123456789',' '))) is null
    --flight_no字段有的含有字母,有的为纯数字,此语句将含有字母的行剔除
    
  5. nullif(expression1 , expression2)

    nullif is used to judge whether expression1 and expression2 are equal, return expression when equal, otherwise return null

    sum(order_seat * segment_space) / nullif(sum(supply_seat * segment_space), 0) 客座 
    --判断除数是否为0,为0时替换为null,避免报错
    
  6. to_char(to_date())

    Conversion of date and string formats

    to_char(to_date(date,'yyyy-mm-dd')+364,'yyyy-mm-dd')
    --此处的date原字段类型为vachar2,先用to_date函数转为日期格式,即可用+364来调整日期,再用to_char函数转回字符串类型,保持字段类型不变
    
  7. pivot

    pivot is a row-to-column function

    SELECT * FROM (...)
        PIVOT(sum(折扣) 折扣, sum(客座) 客座 forin ('2017', '2018'))
    --将变量年转置为列,原来的列将会由'客座','折扣'变为'2017_客座','2017_折扣','2018_客座','2018_折扣'.
    --所有用到聚集函数的变量都要写到pivot函数里,可重命名变量
    

Guess you like

Origin blog.csdn.net/lzykevin/article/details/83758792