SELECT t0.legal_entity_id, t1.attribute_value, t1.attribute_value, t0.long_name, t0.short_name, t1.attribute_value, t1.attribute_value, t1.attribute_value, t1.attribute_value, t1.attribute_value, t0.parent_le_id, t1.attribute_value, t0.classification, t1.attribute_value, T3.last_name, T3.first_name, T3.phone, T3.mailing_address, t1.attribute_value, t3.zipcode, t0.country, t1.attribute_value, t0.le_status, t1.attribute_value, t0.inactive_date, CASE WHEN t0.short_name = '基金内部交易对手' OR t0.short_name = '恒丰_银行总行' THEN 'Y' ELSE 'N' END AS internal_org_flag , CASE WHEN t0.short_name = '基金内部交易对手' OR t0.short_name = '恒丰_银行总行' THEN 'Y' ELSE 'N' END AS is_internal_cpty_flag , CASE WHEN t2.role_name = 'CounterParty' THEN 'Y' ELSE 'N' END AS is_counterparty_flag , CASE WHEN t2.role_name = 'Issuer' THEN 'Y' ELSE 'N' END AS is_asset_issuer_flag , CASE WHEN t2.role_name = 'Agent' THEN 'Y' ELSE 'N' END AS is_custodian_flag , CASE t2.role_name = 'Guarantor' THEN 'Y' ELSE 'N' END AS is_guarantor_flag FROM legal_entity t0 left join le_attribute t1 on T1.legal_entity_id=T0.legal_entity_id left join legal_entity_role t2 on T2.legal_entity_id=T0.legal_entity_id left join le_contact T3 on T3.legal_entity_id=T0.legal_entity_id
时间: 2024-04-01 10:37:05 浏览: 27
这是一个 SQL 查询语句,可能是从某个系统或应用程序中提取数据。这个查询语句包含多个表的连接(使用 LEFT JOIN 关键字),并选择这些表中的一些列作为结果集。以下是对该查询语句中各个部分的解释:
1. SELECT 子句:选择要包含在结果集中的列。这些列来自于多个表,每个列都有一个别名(例如 t0.legal_entity_id 表示 legal_entity 表中的 legal_entity_id 列)。
2. FROM 子句:指定要查询的表以及它们之间的连接方式。这个查询中使用了 LEFT JOIN 关键字,表示左连接,即保留左表中所有行,而右表中没有匹配行的地方填充 NULL 值。
3. WHERE 子句:这个查询中没有 WHERE 子句,表示不对查询结果进行筛选,返回所有符合条件的行。
4. CASE 表达式:这个查询中使用了多个 CASE 表达式,用于根据某些条件返回不同的值。例如,internal_org_flag、is_internal_cpty_flag、is_counterparty_flag 等都是根据 t0.short_name 或 t2.role_name 的值来判断的。
总的来说,这个查询的目的是从多个表中提取一些数据,并对这些数据进行一些处理和转换,最终返回符合条件的行。
相关问题
语法分析:SELECT wt.transaction_id ,we.organization_id ,we.wip_entity_id ,decode(wt.line_id, NULL, we.wip_entity_name, '')
这是一个SQL查询语句,用于从一个或多个表中检索数据。具体来说,该查询语句包含以下列:
- wt.transaction_id:表示从wt表中选择transaction_id列。
- we.organization_id:表示从we表中选择organization_id列。
- we.wip_entity_id:表示从we表中选择wip_entity_id列。
- decode(wt.line_id, NULL, we.wip_entity_name, ''):该表达式使用DECODE函数,将wt表中的line_id列的值与NULL比较。如果它是NULL,则选择we表中的wip_entity_name列的值。否则,选择一个空字符串。
该查询语句将返回一个结果集,其中包含上述列的值。
优化sql:SELECT we.organization_id ,we.wip_entity_id ,case when wl.line_id is null then we.wip_entity_name else '' end wip_entity_name ,we.primary_item_id ,mtt.transaction_type_name ,mmt.transaction_date ,bd.department_code ,mmt.inventory_item_id ,mmt.subinventory_code ,mta.reference_account ,br.resource_code ,lu2.meaning as line_type_name ,mta.base_transaction_value ,mta.cost_element_id ,flv.meaning as cost_element ,wdj.class_code job_type_code ,ml.meaning job_type_name FROM (select * from gerp.mtl_material_transactions where substr(transaction_date,1,7) >= '2023-06' and transaction_source_type_id = 5) mmt inner join gerp.wip_entities we on mmt.organization_id = we.organization_id inner join gerp.mtl_transaction_accounts mta on mta.transaction_source_id = we.wip_entity_id and mta.transaction_id = mmt.transaction_id and mta.transaction_source_type_id = 5 inner join gerp.mtl_transaction_types mtt on mtt.transaction_type_id = mmt.transaction_type_id inner join mfg_lookups lu2 on lu2.lookup_code = mta.accounting_line_type and lu2.lookup_type = 'CST_ACCOUNTING_LINE_TYPE' inner join gerp.mtl_system_items_b msi on msi.inventory_item_id = mmt.inventory_item_id and msi.organization_id = mta.organization_id left join gerp.bom_departments bd on bd.department_id = mmt.department_id left join gerp.bom_resources br on br.resource_id = mta.resource_id left join gerp.wip_lines wl on wl.line_id = mmt.repetitive_line_id left join gerp.wip_discrete_jobs wdj on wdj.wip_entity_id = mta.transaction_source_id left join gerp.fnd_lookup_values_vl flv on cast(mta.cost_element_id as string) = flv.lookup_code and flv.lookup_type = 'CST_COST_CODE_TYPE' left join mfg_lookups ml on ml.lookup_code = wdj.job_type and ml.lookup_type = 'WIP_DISCRETE_JOB' 。其中mmt,we,mta,msi,wdj数据量很大
针对这个SQL语句,可以优化的地方有:
1. 使用JOIN语句时,可以根据数据量大小,将数据量大的表放在后面,这样可以避免在连接时出现性能问题。
2. 使用子查询时,可以在子查询中添加条件筛选,减少返回的数据量。
3. 可以对查询中的日期条件进行优化,例如使用日期范围查询时,可以使用BETWEEN关键字代替大于等于和小于等于的运算符。
4. 可以对大表进行分区或分片,以加快查询速度。
5. 可以对JOIN语句中的字段添加索引,以提高查询效率。
下面是对原SQL语句的优化:
```
SELECT
we.organization_id,
we.wip_entity_id,
CASE WHEN wl.line_id is null THEN we.wip_entity_name ELSE '' END wip_entity_name,
we.primary_item_id,
mtt.transaction_type_name,
mmt.transaction_date,
bd.department_code,
mmt.inventory_item_id,
mmt.subinventory_code,
mta.reference_account,
br.resource_code,
lu2.meaning as line_type_name,
mta.base_transaction_value,
mta.cost_element_id,
flv.meaning as cost_element,
wdj.class_code job_type_code,
ml.meaning job_type_name
FROM
gerp.wip_entities we
INNER JOIN (
SELECT
*
FROM
gerp.mtl_material_transactions
WHERE
transaction_date BETWEEN '2023-06-01' AND '2023-06-30'
AND transaction_source_type_id = 5
) mmt ON mmt.organization_id = we.organization_id
INNER JOIN gerp.mtl_transaction_accounts mta ON mta.transaction_source_id = we.wip_entity_id
AND mta.transaction_id = mmt.transaction_id
AND mta.transaction_source_type_id = 5
INNER JOIN gerp.mtl_transaction_types mtt ON mtt.transaction_type_id = mmt.transaction_type_id
INNER JOIN mfg_lookups lu2 ON lu2.lookup_code = mta.accounting_line_type AND lu2.lookup_type = 'CST_ACCOUNTING_LINE_TYPE'
INNER JOIN gerp.mtl_system_items_b msi ON msi.inventory_item_id = mmt.inventory_item_id
AND msi.organization_id = mta.organization_id
LEFT JOIN gerp.bom_departments bd ON bd.department_id = mmt.department_id
LEFT JOIN gerp.bom_resources br ON br.resource_id = mta.resource_id
LEFT JOIN gerp.wip_lines wl ON wl.line_id = mmt.repetitive_line_id
LEFT JOIN gerp.wip_discrete_jobs wdj ON wdj.wip_entity_id = mta.transaction_source_id
LEFT JOIN gerp.fnd_lookup_values_vl flv ON cast(mta.cost_element_id as string) = flv.lookup_code
AND flv.lookup_type = 'CST_COST_CODE_TYPE'
LEFT JOIN mfg_lookups ml ON ml.lookup_code = wdj.job_type AND ml.lookup_type = 'WIP_DISCRETE_JOB';
```
在优化后的SQL语句中,将子查询中的日期范围查询放在了WHERE语句中,将数据量较大的表放在了后面,左连接的表也放在了后面。同时,可以根据具体情况对需要添加索引的字段进行索引优化。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)