### SQL select count0 from select from select CTL_ID as dataValue CTL_NAME as dataText CTL_CODE as ctlCode catalog as type as pid as fontColor as drugFlag as operType SORT_ID as me
The given SQL query selects the count of rows from a nested subquery. The subquery retrieves data from multiple tables and applies various conditions to filter the results. The final result is sorted based on the SORT_ID column in ascending order.
It is difficult to beautify the SQL query without knowing the specific database schema and the purpose of the query. However, here are a few suggestions to improve the readability of the query:
- Use meaningful aliases for table names and column names instead of single letters or abbreviations. This will make the query more understandable.
- Split the query into multiple lines and indent the subqueries for better readability.
- Add comments to explain the purpose of each subquery or condition.
- Consider breaking down the query into smaller subqueries or views to improve maintainability and understandability.
Here is an example of a slightly modified version of the query with improved formatting:
SELECT COUNT(0)
FROM (
SELECT *
FROM (
-- First subquery
SELECT
CTL_ID AS "dataValue",
CTL_NAME AS "dataText",
CTL_CODE AS "ctlCode",
'catalog' AS "type",
? AS "pid",
'' AS "fontColor",
'' AS "drugFlag",
'' AS "operType",
SORT_ID,
'' AS "medicalCode"
FROM EMR.EMR_CATALOG
WHERE PARENT_CTL_ID = ?
UNION ALL
-- Second subquery
SELECT
t.*,
1000 AS SORT_ID
FROM (
SELECT
s.ITEM_ID AS "dataValue",
s.ITEM_NAME AS "dataText",
'' AS "ctlCode",
'dict' AS "type",
? AS "pid",
'' AS "fontColor",
'' AS "drugFlag",
'' AS "operType",
'' AS "medicalCode"
FROM SYSMN.SYS_SERVICE_ITEM_DICT s
LEFT JOIN sysmn.SYS_DICT_ALIAS alias ON alias.VALUE_ID = s.ITEM_ID
WHERE s.ITEM_CLASS_ID = ?
AND s.STOP_FLAG = '0'
AND s.DEL_FLAG = '0'
-- Add more conditions or join conditions if required
-- Add comments to explain the purpose of each condition
) t
UNION ALL
-- Third subquery
SELECT
a.BASICDATAVALUE_ID AS "dataValue",
NVL(a.SHOW_TEXT, a.DATA_VALUE_REMARK) AS "dataText",
CASE WHEN a.BASICDATA_ID = 'fdfbdb65567249f69c7524b8b8b0632a' THEN '9786be42ac31444bafe8bc6749cbe8a7' ELSE a.PY_CODE END AS "ctlCode",
'dict' AS "type",
? AS "pid",
a.FONT_COLOR AS "fontColor",
(SELECT CN_MEDCODE charge.clinic_vs_price
FROM charge.price_item d
WHERE e.price_item_id = d.price_item_id
AND e.clinic_item_id = a.BASICDATAVALUE_ID) AS "medicalCode",
CASE
WHEN EXISTS (SELECT 1 FROM SYSMN.SYS_TRANSFUSION_DICT WHERE BLOOD_ID = a.BASICDATAVALUE_ID) THEN 'blood'
WHEN EXISTS (SELECT 1 FROM SYSMN.SYS_HIS_DRUG_DICT WHERE DRUG_ID = a.BASICDATAVALUE_ID) THEN 'drug'
ELSE ''
END AS "drugFlag",
DECODE(c.DATA_VALUE, '1', '0', '2', '0', '3', '1', '4', '1', '') AS "operType"
FROM SYSMN.MAV_EMR_CORE_DICT a
LEFT JOIN sysmn.SYS_OPER_DICT b ON a.BASICDATAVALUE_ID = b.OPER_ID
LEFT JOIN SYSMN.MAV_EMR_CORE_DICT c ON b.OPER_NATURE = c.BASICDATAVALUE_ID
WHERE a.BASICDATA_ID = ?
-- Add more conditions or join conditions if required
-- Add comments to explain the purpose of each condition
) tmp_count
ORDER BY SORT_ID ASC
) tmp_total
Note: This is just a suggestion, and the actual formatting may vary based on the specific requirements and conventions of your organization
AI 优化版
原文地址:https://www.cveoy.top/t/topic/iutK 著作权归作者所有。请勿转载和采集!