1、laccount
科目表(laccount)中科目代码(vc_code)的长度和等级是4226的关系,
即一级是4位字符(例:1102),
二级在一级基础上添加两位(例:110201,二级代码为01),
三级在二级基础上添加两位(例:11020101,三级代码为01),
四级在二级基础上添加六位(例:11020101000991,四级代码为000991)。
请写出sql,要求查出所有以1103开头的科目代码三级是“02”的所有科目。
select t.*
from laccount t
where substr(t.vc_code,1,4) = '1103'
and substr(t.vc_code,7,2) = '02';
2、
数据库表结构如下,请编写sql计算该表中对应数据的年化收益。年化收益公式:年化收益=收益/年初至当日天数*当年实际天数。
展示字段:日期,账户代码,收益,年化收益。
字段名 | 数据类型 | 描述 |
---|---|---|
d_date | date | 日期 |
vc_fundcode | varchar2(20) | 账户代码 |
f_income | number(21,4) | 收益 |
select t.日期,
t.账户代码,
t.收益
,trunc(t.日期,'yyyy') as y1
,t.日期 - trunc(t.日期,'yyyy') + 1 as days1 --年初至当日天数
,add_months( trunc(t.日期,'yyyy'),12) as y2
,add_months( trunc(t.日期,'yyyy'),12) - trunc(t.日期,'yyyy') days2 --当年实际天数
,t.收益 /(t.日期 - trunc(t.日期,'yyyy') + 1)
* (add_months( trunc(t.日期,'yyyy'),12) - trunc(t.日期,'yyyy') ) as 年化收益
from bbb t;
3、
如下图所示,请写出table1和table2两个表通过id做full join关联的sql脚本及对应的结果集,要求展示字段为id、code、name、setup_date、mature_date。
table1:
id | code | name |
---|---|---|
1 | a0001 | 账户1 |
2 | a0002 | 账户2 |
3 | a0003 | 账户3 |
4 | a0004 | 账户4 |
table2:
id | setup_date | mature_date |
---|---|---|
1 | 2021/1/7 | 2025/3/3 |
2 | 2020/1/8 | 2028/3/3 |
3 | 2019/1/9 | 2035/3/3 |
5 | 2018/1/10 | 2031/3/3 |
select cc.id
,ee.id
,nvl(cc.id,ee.id) id
,case when cc.id is null then ee.id
else cc.id
end id
,cc.code
,cc.name
,ee.setup_date
,ee.mature_date
from cc
full join ee
on cc.id=ee.id
4、
表a字段如下:
字段名 | 中文含义 | 枚举值 |
---|---|---|
d_date | 日期 | |
vc_type | 类型 | |
f_id | 编号 | |
f_value | 金额 |
表b字段如下:
字段名 | 中文含义 | 枚举值 |
---|---|---|
f_id | 编号 | |
vc_name | 名称 | a1、a2、b1、b2、c1 |
表a和表b通过f_id字段关联。请编写sql统计名称为a1、b2、c1按日期汇总的合计金额。
结果集字段: 日期 合计金额。
select d_date
,sum(f_value)
from tablea t1
inner join tableb t2
on t1.f_id = t2.f_id
where t2.vc_name in('a1','b2','c1')
group by t1.d_date;
5、
根据以下表格数据编写sql语句。
tdate(日期) | acc_cd(产品) | ins_cd(证券) | quantity(购买数量) | dirtymktvalue(购买金额) |
---|---|---|---|---|
2021-01-03 | a00002 | 1100233sh | 1000 | 12300 |
2021-01-19 | a00002 | 8300002sh | 57000 | 701100 |
2021-02-11 | l00033 | 8300002sh | 211000 | 2595300 |
2021-02-19 | l00033 | 8300002sh | 225000 | 2767500 |
2021-02-03 | a00002 | 8300002sh | 71000 | 873300 |
2021-02-11 | a00002 | 8300002sh | 85000 | 1045500 |
2021-01-07 | a00002 | 1100233sh | 15000 | 184500 |
2021-01-11 | a00002 | 1100233sh | 29000 | 356700 |
2021-01-15 | a00002 | 1100233sh | 43000 | 528900 |
2021-01-19 | l00033 | 8300002sh | 183000 | 2250900 |
2021-02-03 | l00033 | 8300002sh | 197000 | 2423100 |
2021-02-27 | l00033 | 8300002sh | 239000 | 2939700 |
2021-02-19 | a00002 | 8300002sh | 99000 | 1217700 |
2021-02-27 | a00002 | 8300002sh | 113000 | 1389900 |
2021-01-03 | l00033 | 1100233sh | 127000 | 1562100 |
2021-01-07 | l00033 | 1100233sh | 141000 | 1734300 |
2021-01-11 | l00033 | 1100233sh | 155000 | 1906500 |
2021-01-15 | l00033 | 1100233sh | 169000 | 2078700 |
(1)、计每个产品最大购买金额的证券,展示字段为:日期、产品、证券、购买数据、购买金额。
select t1.*
from (
select t.tdate,
t.acc_cd,
t.ins_cd,
t.quantity,
t.dirtymktvalue
,max(t.dirtymktvalue)over(partition by t.acc_cd) as max_dirtymktvalue
,rank()over(partition by t.acc_cd order by t.dirtymktvalue desc) as rn
from abb t
) t1
where t1.max_dirtymktvalue = t1.dirtymktvalue
--where t1.rn = 1;
(2)、按月份统计产品的购买数量和购买金额,展示内容为:月份,产品,购买数量、购买金额。
select substr(t.tdate,1,7) as ym
,t.acc_cd
,sum(t.quantity) as sum_quantity
,sum(t.dirtymktvalue) as sum_dirtymktvalue
from abb t
group by substr(t.tdate,1,7),t.acc_cd;