Oracle basic part 2 (pseudo columns/tables, single functions, null value processing, row-column conversion, analytic functions, set operations)

1 Pseudo column, pseudo table

1.1 Pseudo columns

rowid : rowid is a pseudo column used to uniquely mark rows in a table. It is the internal address of the row data in the physical table. It contains two addresses. One is the address of the data file stored in the block containing the row in the data table, and the other is the row that can directly locate the data row itself in the data block. address in .
The rowid of each record is unique except that it may not be unique within the same cluster. It can be understood that rowid is unique

rownum : rownum is the number that the Oracle system sequentially assigns to the rows returned from the query. The first row returned is assigned 1, and the second row returned is assigned 2. This field can be used to limit the total number of rows returned by the query. And rownum cannot be prefixed with the name of any table

1.2 Pseudo table

dual : dual is indeed a table, a table with only one field and one row of records. Its fields and records are meaningless. Usually we call it 'pseudo table'. dual means that it comes with the system and is a system table. Its table structure cannot be deleted or modified.
Insert image description here

2 single function

2.1 Commonly used string functions

2.1.1 length() Query the length of the specified character

Syntax: length(string)
Explanation: Calculate the length of characters occupied by string

select length('ABCD') from dual

The result is 4

2.1.2 substr() is used to intercept strings

Syntax: substr(string string, int a[, int b]);
Explanation: string needs to be intercepted, a intercepts the starting position of the string (note: when a is equal to 0 or 1, interception starts from the first position) , b The length of the string to be intercepted (omitted to intercept to the end)

select substr('ABCD',2,2) from dual

The result is BC

2.1.3 concat() for string concatenation

Syntax: Concat (expression 1, expression 2)
Explanation: Use the expression 1 value and the expression 2 value for splicing display.

select concat('A','B') from dual 

The result is AB

There are also other splicing methods

select concat(concat('A','B'),'C') from dual  -- concat只能拼接两个字符串,需要拼接多个需要嵌套

select 'A' || 'B' || 'C' from dual  -- 可以使用 || 进行字符串的拼接

2.2 Commonly used numerical functions

2.2.1 round() rounding

Syntax: ROUND (number[,decimals])
Explanation: number is the value to be intercepted, and decimals specifies the number of digits after the decimal point that need to be retained. Optional, omit it to truncate all decimal parts and round. If it is a negative number, it represents the number of digits to the left starting from the decimal point. The corresponding integer number is filled with 0 and the decimal is removed. It should be noted that, unlike the trunc function, the intercepted numbers must be rounded.

select round(100.456,2) from dual  -- 100.46

2.2.2 trunc(for number) digital interception

Syntax: TRUNC (number[,decimals])
Explanation: number is the value to be intercepted; decimals specifies the number of digits after the decimal point to be retained. It is optional. If it is ignored, all decimal parts will be truncated.
Note : Data is not rounded when intercepting

select trunc(100.456,2) from dual  -- 100.45

2.2.2 mod() modulus

Syntax: mod(m,n)
Explanation: (1) MOD returns the remainder of m divided by n. If n is 0, returns m; (2) This function takes any numeric data type or any non-numeric data type as a parameter, Can be implicitly converted to a numeric data type.

select mod(10,3) from dual  -- 1

2.3 Common date functions

2.3.1 sysdate current date and time

Syntax: sysdate
Explanation: Return the current date and time

select sysdate from dual  -- 2023-04-11 22:02:30

2.3.2 add_months() add months function

Syntax: add_months(times,months)
Explanation: Used to calculate the time value after adding months to time times. If the value of months is a negative number, it is the time value between this time point (this time-months months)

select add_months(sysdate,2) from dual  -- 2023-06-11 22:06:04

2.3.3 LAST_DAY() The last day of the month

Syntax: last_day(time)
Analysis: Returns the last day of the month where the specified date is located

select last_day(sysdate) from dual -- 2023-04-30 22:08:30

2.3.4 TRUNC (for dates) date interception

Syntax: TRUNC(date[,fmt])
Explanation: date is a date value; fmt date format; the date will be intercepted according to the specified date format; ignore it and intercept it from the latest date

select trunc(sysdate,'mi') from dual -- 按分钟截取(把秒截掉,显示当前日期的分钟)
select trunc(sysdate,'hh') from dual -- 按小时截取(把分钟截掉,显示当前日期的小时)
select trunc(sysdate) from dual -- 按日截取(把时间截掉)
select trunc(sysdate,'mm') from dual -- 按月截取(把日截掉,显示当月第一天)
select trunc(sysdate,'yyyy') from dual -- 按年截取(把月截掉,显示当年第一天)

2.4 Other functions

2.4.1 nvl() null value function

Syntax: NVL (expression 1, expression 2)
Explanation: If expression 1 is null, NVL returns the value of expression 2, otherwise it returns the value of expression 1. The purpose of this function is to convert a null value into an actual value. The value of its expression can be numeric, character or date. But the data types of expression 1 and expression 2 must be the same type.

SELECT NVL(NULL, 0) FROM DUAL;  -- 0

2.4.2 decode() conditional value

Syntax 1: decode (expression, value, result1, result2)
Explanation: If expression=value, output result1, otherwise output result2
Syntax 1: decode (expression, value1, result1, value2, result2, value3, result3..., default)
Explanation : If expression=value1, output result1, expression=value2, output reslut2, expression=value3, output result3. If expression is not equal to all listed values, the output is default.

select decode(100,1,2,400,200,500) from dual -- 500

3 row-column conversion

3.1 Using PIVOT

Syntax 1: PIVOT (arbitrary aggregate function FOR column name IN (type))
Explanation: [Aggregation function] aggregates fields that need to be converted into column values; [column name] is the field that needs to be converted into column identifiers, [type] ] is the required result display. You can specify an alias in [Type]; you can also specify a subquery in IN.

SELECT * FROM (
  SELECT 
       A16.INTEREST_RATE_CD
       ,A16.DATA_DT
       ,A16.TERM
       ,A16.INTEREST_RATE
  FROM FACT_FTP260_BSC_A16 A16
)
PIVOT(
        SUM(INTEREST_RATE)
        FOR TERM
        IN ('1D' AS D1   ,'7D' AS D7   ,'14D' AS D14
           ,'1M' AS M1   ,'2M' AS M2   ,'6M' AS M6
           ,'9M' AS M9   ,'1Y' AS Y1    ,'2Y' AS Y2
           ,'3Y' AS Y3   ,'5Y' AS Y5   ,'7Y' AS Y7
           ,'10Y' AS Y10 ,'15Y' AS Y15 )
    )

3.2 Using sum and DECODE functions

select (select name from t_area where id = areaid) 区域,
       sum(case when month='01' then money else 0 end)一月,
       sum(case when month='02' then money else 0 end)二月,
       sum(case when month='03' then money else 0 end)三月,
       sum(case when month='04' then money else 0 end)四月,
       sum(case when month='05' then money else 0 end)五月,
       sum(case when month='06' then money else 0 end)六月,
       sum(case when month='07' then money else 0 end)七月,
       sum(case when month='08' then money else 0 end)八月,
       sum(case when month='09' then money else 0 end)九月,
       sum(case when month='10' then money else 0 end)十月,
       sum(case when month='11' then money else 0 end)十一月,
       sum(case when month='12' then money else 0 end)十二月

from t_account
where year = '2012'
group by areaid

3.2 Use CASE WHEN and GROUP BY

Note : This method is the most commonly used, and price comparison is easy to understand.

SELECT
    A16.INTEREST_RATE_CD
    ,SUM(CASE TERM WHEN '1D' THEN A16.INTEREST_RATE ELSE 0 END) AS D1
    ,SUM(CASE TERM WHEN '7D' THEN A16.INTEREST_RATE ELSE 0 END) AS D7
    ,SUM(CASE TERM WHEN '14D' THEN A16.INTEREST_RATE ELSE 0 END) AS D14
    ,SUM(CASE TERM WHEN '1M' THEN A16.INTEREST_RATE ELSE 0 END) AS M1
    ,SUM(CASE TERM WHEN '2M' THEN A16.INTEREST_RATE ELSE 0 END) AS M2
    ,SUM(CASE TERM WHEN '3M' THEN A16.INTEREST_RATE ELSE 0 END) AS M3
    ,SUM(CASE TERM WHEN '6M' THEN A16.INTEREST_RATE ELSE 0 END) AS M6
    ,SUM(CASE TERM WHEN '9M' THEN A16.INTEREST_RATE ELSE 0 END) AS M9
    ,SUM(CASE TERM WHEN '1Y' THEN A16.INTEREST_RATE ELSE 0 END) AS Y1
    ,SUM(CASE TERM WHEN '2Y' THEN A16.INTEREST_RATE ELSE 0 END) AS Y2
    ,SUM(CASE TERM WHEN '3Y' THEN A16.INTEREST_RATE ELSE 0 END) AS Y3
    ,SUM(CASE TERM WHEN '5Y' THEN A16.INTEREST_RATE ELSE 0 END) AS Y5
    ,SUM(CASE TERM WHEN '7Y' THEN A16.INTEREST_RATE ELSE 0 END) AS Y7
    ,SUM(CASE TERM WHEN '10Y' THEN A16.INTEREST_RATE ELSE 0 END) AS Y10
    ,SUM(CASE TERM WHEN '15Y' THEN A16.INTEREST_RATE ELSE 0 END) AS Y15
FROM FACT_FTP260_BSC_A16 A16
GROUP BY 
     A16.INTEREST_RATE_CD

4 Analytical functions

Can be used for ranking (1: Jump with the same value, ranking with the same serial number; 2. Continuous with the same value, same ranking with the same serial number; 3. Continuous serial numbers, regardless of whether the values ​​are the same)

4.1 rank() jumps if the value is the same, the rank is the same, and the sequence number is the same.

select rank() over(order by usenum desc) 序号,t.* from t_account t

Insert image description here

4.2 dense_rank() The values ​​are the same, the rankings are the same, and the serial numbers are consecutive.

select dense_rank() over(order by usenum desc) 序号,t.* from t_account t

Insert image description here

4.3 row_number() over() serial numbers are consecutive, regardless of whether the values ​​are the same

select row_number() over(order by usenum desc) 序号,t.* from t_account t

Insert image description here

5. Set operations

5.1 union all union (including duplicate records)

select * from t_owners where id>5
union all
select * from t_owners where id<8

Insert image description here

5.2 union (excluding duplicate records)

select * from t_owners where id>5
union
select * from t_owners where id<8

Insert image description here

5.3 intersect (repeating parts of two sets)

select * from t_owners where id>5
intersect
select * from t_owners where id<8

Insert image description here

5.4 minus difference set

5.4.1 Example 1

select * from t_owners where id>5
minus
select * from t_owners where id<8

Insert image description here

5.4.2 Paging for subtraction operations

select rownum, t.* from t_account t where rownum <=20
minus
select rownum, t.* from t_account t where rownum <=10

Insert image description here

Guess you like

Origin blog.csdn.net/slb190623/article/details/130092148