首先建立乙個示例的檢視:
此檢視模擬訪問款檢視sql> create
or replace view test_sum(id ,amt,trx)
2as3select
1,100,'pr'
from dual union all
4select
2,100,'pr'
from dual union all
5select
3,50,'py'
from dual union all
6select
4,100,'pr'
from dual union all
7select
5,200,'py'
from dual union all
8select
6,100,'py'
from dual;
view created.
sql> select * from test_sum;
id amt tr
---------- ---------- --
1100 pr
2100 pr
350 py
4100 pr
5200 py
6100 py
6 rows selected.
sql>
id 是唯一列
amt 表示每次事務處理(取款/存款)涉及的型別
trx 定義事務處理的型別,取款是py,存款是pr
需求:計算每次存、取款後的金額,如果trx是pr,則加上amt所代表的金額,否則減去。
第一步:首先把取款的值變為負數
第二步,使用sum分析函式sql> select id,
2case when trx = 'pr'
then
'存款'
else
'取款'
end 訪問型別,
3 amt 金額,
4case when trx = 'pr'
then amt else -amt end 變更後的值
5from test_sum
6order
by id;
id 訪問型別 金額 變更後的值
---------- -------- ---------- ----------
1 存款 100
1002 存款 100
1003 取款 50 -50
4 存款 100
1005 取款 200 -200
6 取款 100 -100
6 rows selected
sql>
到此,更改累計和的值的步驟就已經做完了。sql> select id,
2case when trx = 'pr'
then
'存款'
else
'取款'
end 訪問型別,
3 amt 金額,
4 sum(case when trx = 'pr'
then amt else -amt end) over(order
by id) as 餘額
5from test_sum
6order
by id;
id 訪問型別 金額 餘額
---------- -------- ---------- ----------
1 存款 100
1002 存款 100
2003 取款 50
1504 存款 100
2505 取款 200
506 取款 100 -50
6 rows selected
sql>
SQL小練之查詢和更改
1.找出姓名倒數第三位是a且不屬於20號部門的員工資訊 sql server select from emp where ename like a and deptno 20 這裡用到了萬用字元進行模糊查詢,like 這個關鍵字就是用於字串模糊查詢的 a 中,用下劃線佔位符,a 則標識字元a處於倒數...
更改 input type 的值
需要實現的效果 乙個輸入框,當輸入框未獲得焦點的時候,value 值為 密碼 當輸入框失去焦點的時候,輸入內容顯示為 我們很直接會想到下面的js showpwd focus function 發現並沒有實現預期效果,出現 uncaught exception type property can t ...
隨筆 SQL查詢之group by和求最值的使用
直接開始 首先是表資料 要求 查詢出學生的各個科目和其成績,科目的成績以最高分為最終結果,並按照uid正序排列 第一步 查詢學生,科目及其成績 select uid,uname,ucourse,uscores from users 第二步 用group by對uname和ucourse進行分組,並對...