Oracle utility SQL Query function has accumulated

1. Requirements: the query data, if the field is empty, the sql set displays a default value, otherwise displays the value of the check out.

 Function: nvl (param, default);

 Explanation: When using nvl function, the first parameter is the field to query, and the second parameter is the default display query field is empty, if not empty then check out the display value


2. Requirements: the query data, there is a field value is stored 1,2,3 this, to be displayed as a corresponding query in Chinese.

 Function decode (status, '1', '[ON]', '2', '[OFF]', '[OFF]');

 Explanation: decode function using an if statement is equivalent to determining the first parameter is a field to be queried, the second and the third is a combination (if block corresponding to a) shows (the value, the corresponding value), where there may be a plurality of such intermediate if block, else the last parameter corresponding to the action, if it does not satisfy the foregoing take a final value.


3. Demand: Use the like, if the value back like check out the results of the query

demo1: (select id from table)||'%'

demo2:  SELECT '[' || COUNT (decode (ZT, '. 1', '. 1')) || '/' || COUNT (ID) || ']' is the result from SB: [2/3] Such of

 Explain: sql statement used to splice two vertical bars || value, instead of the + sign.


4. Demand: The Id conditions on a query and the next data

 On a function: lag (a.id) over (partition by a.status order by a.yjsj desc)

 Next Function: lead (a.id) over (partition by a.status order by a.yjsj desc)

 示例SQL: select t.nextid,t.beforeid from (select a.id,lead(a.id) over(partition by a.status order by a.yjsj desc) nextid,lag(a.id) over(partition by a.status order by a.yjsj desc) beforeid from table a where a.status='0') t where t.id = ?;


5. Demand: The results of the two tables into one result set is returned, the corresponding field is not

union all the results may be the two tables into one result set returned, but to return the fields corresponding to

如: table1  id,name     table2   id,name,para1,para2

select id,name,para1,para2 from table2 union all select id,name,'','' from table1



Published 90 original articles · won praise 21 · views 470 000 +

Guess you like

Origin blog.csdn.net/yx13649017813/article/details/42024377