select sum(t.p_totalnav) as scale,t.publish_date as date from (select fundcode,invest_manager,CAST(p_totalnav AS DECIMAL(22,4)) as p_totalnav,publish_date from app.v_jrcrm_hugo_manageasset)t where strpos(t.invest_manager,'钱亚风云')>0 group by 2 优化SQL
时间: 2024-03-29 09:34:43 浏览: 12
优化思路:
1. 使用EXISTS代替子查询:将子查询转化为EXISTS语句,可以提高查询效率。
2. 使用DECIMAL代替CAST:在定义表结构的时候,可以将p_totalnav字段的类型定义为DECIMAL,避免在查询时进行类型转换。
3. 使用索引:对invest_manager字段创建索引,可以加快查询速度。
优化后的SQL如下:
SELECT SUM(t.p_totalnav) AS scale, t.publish_date AS date
FROM app.v_jrcrm_hugo_manageasset t
WHERE EXISTS (SELECT 1 FROM app.v_jrcrm_hugo_manageasset m WHERE m.fundcode = t.fundcode AND m.invest_manager LIKE '%钱亚风云%')
AND t.publish_date BETWEEN 'start_date' AND 'end_date'
GROUP BY t.publish_date;
其中,start_date和end_date为查询时间范围。如果invest_manager字段已经创建了索引,可以进一步提高查询效率。
相关问题
select sum(t.p_totalnav) scale,t.publish_date date from app.v_jrcrm_hugo_manageasset t where strpos(t.invest_manager,'钱亚风云')>0 group by 2
这是一个查询语句,它的作用是从表 app.v_jrcrm_hugo_manageasset 中筛选出投资经理包含“钱亚风云”的记录,并按照 publish_date 分组,计算每组的 p_totalnav 总和,同时给每组取别名 scale 和 date。
需要注意的是,strpos()函数用于查找字符串中某个子串第一次出现的位置,如果找到了则返回该位置的索引,否则返回 0。在这个查询中,我们使用 strpos() 函数查找 invest_manager 字段中是否包含“钱亚风云”,如果包含则返回大于 0 的索引值,相当于一个布尔值,用于筛选出符合条件的记录。
select * from (select rownum as r, t.* from (select to_char(a.d_disclosedate, 'yyyy-MM-dd') as showdatetime, a.c_filetype, a.c_filename, a.c_filepath, a.c_month, a.c_year, a.d_modifydate, a.d_createdate, a.l_serialno, b.c_caption, b.c_keyvalue, wm_concat(c.c_fundcode) as c_fundcode, wm_concat(c.c_fundname) as c_fundname, b.c_memo from twordfilesmanage a, tdictionary b, tfundinfo c, twordfundrelation d where 1 = 1 and b.c_sysname = 'DATACNT' and b.l_keyno = 1301 and a.c_publish = '1' and a.c_filetype = trim(b.c_keyvalue) and c.c_fundcode = d.c_fundcode and a.l_serialno = d.l_serialno and a.c_filetype = '1' group by (a.d_disclosedate, a.c_filetype, a.c_filename, a.c_filepath, a.c_month, a.c_year, a.d_modifydate, a.d_createdate, a.l_serialno, b.c_caption, b.c_keyvalue, b.c_memo)) t where rownum <= 10) where r >= 1
这是一条 SQL 查询语句,目的是从数据库中查询符合条件的记录,并返回所有列的值。具体查询条件如下:
- 查询表 twordfilesmanage、tdictionary、tfundinfo 和 twordfundrelation
- 查询条件包括:
- b.c_sysname = 'DATACNT'
- b.l_keyno = 1301
- a.c_publish = '1'
- a.c_filetype = trim(b.c_keyvalue)
- c.c_fundcode = d.c_fundcode
- a.l_serialno = d.l_serialno
- a.c_filetype = '1'
- 查询结果按照 a.d_disclosedate、a.c_filetype、a.c_filename、a.c_filepath、a.c_month、a.c_year、a.d_modifydate、a.d_createdate、a.l_serialno、b.c_caption、b.c_keyvalue 和 b.c_memo 分组,并使用 wm_concat 函数对 c.c_fundcode 和 c.c_fundname 进行合并处理
- 最终返回 10 条符合条件的记录
该查询语句的具体实现可能会根据数据库的不同而有所不同。