You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.4 KiB

# 一、元数据
## 1.1 存储过程
+ 查看所有存储过程
```sql
select distinct name From user_source where type = 'PROCEDURE'
```
+ 查看存储过程语句
```sql
SELECT text
FROM user_source
WHERE NAME = 'EXECUTE_JOB_FINANCE_SURVEY'
ORDER BY line
```
## 1.2 表
+ 查看所有表
```sql
# 查询当前用户
select * from user_tab_comments where table_type='TABLE';
# 查询所有用户,当前用户必须有sysdba权限
select * from all_tab_comments
```
+ 查看表所有字段
```sql
SELECT
COLUMN_NAME,
DATA_TYPE
FROM
user_tab_columns
WHERE
table_name = 'DW_INFRASTRUCTURE_SURVEY'
ORDER BY
COLUMN_ID ASC;
```
+ 查看具体字段
```sql
SELECT
COLUMN_NAME,
DATA_TYPE,
DATA_LENGTH,
NULLABLE,
DEFAULT_LENGTH,
DATA_DEFAULT
FROM
user_tab_columns
WHERE
table_name = 'DW_INFRASTRUCTURE_SURVEY'
AND COLUMN_NAME = 'STATISTICAL_TIME'
ORDER BY
COLUMN_ID ASC;
```
+ 查看字段及注释
```sql
SELECT
b.COLUMN_NAME,
a.COMMENTS,
case when DATA_PRECISION is null and DATA_SCALE=0 and DATA_TYPE='NUMBER' then 'NUMBER(38,0)' when
DATA_PRECISION is not null and DATA_SCALE>0 then 'NUMBER('||DATA_PRECISION||','||DATA_SCALE||')' when
DATA_TYPE<>'DATE' then DATA_TYPE||'('||DATA_LENGTH||')' else DATA_TYPE end as DATA_TYPE_,
b.NULLABLE,
b.DATA_LENGTH,
b.DEFAULT_LENGTH,
b.DATA_DEFAULT,
DATA_PRECISION,
DATA_SCALE,
b.COLUMN_ID,
b.DATA_TYPE
FROM
user_col_comments a
LEFT JOIN user_tab_columns b ON a.table_name = b.table_name
AND a.COLUMN_NAME = b.COLUMN_NAME
WHERE
a.table_name = 'OA_VEHICLE_OIL_CHARGE'
ORDER BY
b.COLUMN_ID ASC
```
+ 修改表备注
```
# 表备注
Comment on table DW_RE_PROGRESS_DETAIL is '项目进度-进度明细';
# 字段备注
comment on column Student.id is '学生id';
comment on column Student.name is '学生姓名';
```
## 1.3 视图
+ 查询当前用户所有视图
```sql
select * from user_tab_comments where table_type='VIEW'
```
+ 视图创建语句
```sql
select text from all_views where view_name='DW_SAFETY_RISK_LARG_QS_VW'
```
# 二、常用函数
## 2.1 行转列、列转行
+ pivot
```sql
--行转列
select *
from SalesList pivot(
max(salesNum) for shangPin in ( --shangPin 即要转成列的字段
'上衣' as 上衣, --max(salesNum) 此处必须为聚合函数,
'裤子' as 裤子, --in () 对要转成列的每一个值指定一个列名
'袜子' as 袜子,
'帽子' as 帽子
)
)
where 1 = 1; --这里可以写查询条件没有可以直接不要where
```
```sql
with hsb_stat as (select count(1) as num,'已完成重大污染物识别与评价项目' as item from SU_CHECK_ENVWATER_IMPL where STAT_ITEM_ID=110 and FINISH_STATUS=1 and PROJECT_ID=#{project_id}
union
select count(1) as num,'已完成环水保检测项目' as item from SU_CHECK_ENVWATER_IMPL where STAT_ITEM_ID=111 and FINISH_STATUS=1 and PROJECT_ID=#{project_id}
union
select count(1) as num,'已完成“三同时”落实情况项目' as item from SU_CHECK_ENVWATER_IMPL where STAT_ITEM_ID=112 and FINISH_STATUS=1 and PROJECT_ID=#{project_id}
union
select count(1) as num,'已完成环水保验收项目' as item from SU_CHECK_ENVWATER_IMPL where STAT_ITEM_ID=113 and FINISH_STATUS=1 and PROJECT_ID=#{project_id}
union
select nvl(sum(CHECK_COUNT),0) as num,'检查次数' as item from SU_CHECK_ENVWATER_IMPL where PROJECT_ID=#{project_id}
union
select nvl(sum(CHECK_ISSUE_COUNT),0) as num,'发现问题数' as item from SU_CHECK_ENVWATER_IMPL where PROJECT_ID=#{project_id}
union
select nvl(sum(RECTIFY_ISSUE_COUNT),0) as num,'整改完成' as item from SU_CHECK_ENVWATER_IMPL where PROJECT_ID=#{project_id})
-- 重点
select * from hsb_stat pivot(max(num) for item in (
'已完成“三同时”落实情况项目' as santongshi,
'已完成环水保检测项目' as hsjc
'已完成环水保验收项目' as hsys,
'已完成重大污染物识别与评价项目' as zdwrw,
'检查次数' as jccs,
'整改完成' as zgwc,
'发现问题数' as fxwts
))
```
+ merge into 合并
```sql
MERGE INTO t1
USING (SELECT id,name FROM t2) t2
ON ( t1.id=t2.id)
WHEN MATCHED THEN
UPDATE SET T1.name= t2.name
WHEN NOT MATCHED THEN
INSERT (id,name) VALUES (t2.id,t2.name);
# 结合with
MERGE INTO #TEMP1 A
USING (
WITH SUMORDER AS
(SELECT PRODUCTID, SUM(AMOUNT) TOTAL
FROM ORDER GROUP BY PRODUCTID)
SELECT * FROM SUMORDER
) B ON (A.PRODUCTID = B.PRODUCTID)
WHEN MATCHED THEN UPDATE SET A.TOTAL = B.TOTAL;
```