|
|
|
@ -157,3 +157,55 @@ WITH SUMORDER AS
|
|
|
|
|
) B ON (A.PRODUCTID = B.PRODUCTID)
|
|
|
|
|
WHEN MATCHED THEN UPDATE SET A.TOTAL = B.TOTAL;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 2.2 日期时间
|
|
|
|
|
+ 数据库最大的年月往前推12个月
|
|
|
|
|
```sql
|
|
|
|
|
SELECT
|
|
|
|
|
TO_CHAR(
|
|
|
|
|
ADD_MONTHS(
|
|
|
|
|
TO_DATE( to_char( to_date( ( select max(DECLARE_PERIOD_YEAR_MONTH) from CRZ_NMG_WORKER_WAGE_BOND where project_id=600000000000000138 ), 'yyyy-MM' ), 'yyyy-MM' ), 'yyyy-MM' ),
|
|
|
|
|
ROWNUM - 12
|
|
|
|
|
),
|
|
|
|
|
'yyyy-MM'
|
|
|
|
|
) AS MONTHLIST
|
|
|
|
|
FROM
|
|
|
|
|
DUAL CONNECT BY ROWNUM <= 12
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
+ 指定月份的前N个月
|
|
|
|
|
```sql
|
|
|
|
|
-- 获取2020-06的后5个月(2020-11)
|
|
|
|
|
SELECT TO_CHAR(ADD_MONTHS(to_date('2020-06','yyyy-MM'),5),'YYYY-MM') FROM dual;
|
|
|
|
|
-- 获取2020-06的前3个月(2020-03)
|
|
|
|
|
SELECT TO_CHAR(ADD_MONTHS(to_date('2020-06','yyyy-MM'),-3),'YYYY-MM') FROM dual;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
+ 返回指定日期当月的最后一天
|
|
|
|
|
```sql
|
|
|
|
|
SELECT SYSDATE,last_day(SYSDATE) FROM dual;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 2.3 窗口函数
|
|
|
|
|
+ over()
|
|
|
|
|
```
|
|
|
|
|
# 累加
|
|
|
|
|
sum(COLLECT_BOND_AMOUNT) over ( ORDER BY DECLARE_PERIOD_YEAR_MONTH desc)
|
|
|
|
|
# 分组累加
|
|
|
|
|
sum( DESIGN_AMOUNT ) over ( partition BY PROJECT_ID order by DESIGN_AMOUNT desc)
|
|
|
|
|
# 在按部门划分的基础上,工资从高到低分级,级别RANK从1开始依次递增
|
|
|
|
|
RANK() OVER(PARTITION BY E.DEPTNO ORDER BY E.SAL DESC) RANK
|
|
|
|
|
# 记录后第N条
|
|
|
|
|
LEAD(E.SAL, 1, 0) OVER(PARTITION BY E.DEPTNO ORDER BY E.SAL) LEAD_SAL
|
|
|
|
|
# 记录前面第N行记录
|
|
|
|
|
LAG(E.SAL, 1, 0) OVER(PARTITION BY E.DEPTNO ORDER BY E.SAL) LAG_SAL
|
|
|
|
|
# 取行号
|
|
|
|
|
ROW_NUMBER() OVER(PARTITION BY E.DEPTNO ORDER BY E.SAL) ROW_NUM
|
|
|
|
|
# 统计某组中的总计值
|
|
|
|
|
SUM(E.SAL) OVER(PARTITION BY E.DEPTNO) SUM_SAL
|
|
|
|
|
# 统计某组中的平均值
|
|
|
|
|
AVG(E.SAL) OVER(PARTITION BY E.DEPTNO) AVG_SAL
|
|
|
|
|
# 按某列分组,并统计该组中记录数量
|
|
|
|
|
COUNT(E.SAL) OVER(PARTITION BY E.DEPTNO) COUNT_SAL
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|