优化sql:select date_day, workshop, production, plan, rate from( select toDate(partition_date) as date_day, case when operation in('1ZM','1FM') then '电极' when operation = '1HJ1' then '装配' when operation = '1FZ' then '化成' end as workshop, sum(toFloat64(qty)) as production, '0' as plan, '0' as rate from ( SELECT action_code,partition_date, sfc,operation, item, qty,shop_order_bo,resrce, row_number() over (partition by operation ,sfc,action_code order by created_timestamp desc) as row_num FROM chuz_ods.ods_mes_activity_log_all where 1=1 and change_stamp IS NULL and action_code = 'COMPLETE' and partition_date >= cast(concat(toString(today()-1),' 07:00:00.000') as DateTime64) and partition_date <= cast(concat(toString(today()),' 07:00:00.000') as DateTime64) and operation in ('1ZM','1FM','1HJ1','1FZ') group by action_code,partition_date, sfc,operation, item, qty,shop_order_bo,resrce,created_timestamp ) where 1=1 and row_num =1 group by toDate(partition_date), case when operation in('1ZM','1FM') then '电极' when operation = '1HJ1' then '装配' when operation = '1FZ' then '化成' end as workshop ) where 1=1
时间: 2024-01-06 19:06:48 浏览: 111
以下是优化后的SQL查询语句:
```sql
SELECT
toDate(partition_date) AS date_day,
CASE
WHEN operation IN ('1ZM', '1FM') THEN '电极'
WHEN operation = '1HJ1' THEN '装配'
WHEN operation = '1FZ' THEN '化成'
END AS workshop,
SUM(toFloat64(qty)) AS production,
'0' AS plan,
'0' AS rate
FROM chuz_ods.ods_mes_activity_log_all
WHERE
change_stamp IS NULL
AND action_code = 'COMPLETE'
AND partition_date >= CAST(concat(toString(today() - 1), ' 07:00:00.000') AS DateTime64)
AND partition_date <= CAST(concat(toString(today()), ' 07:00:00.000') AS DateTime64)
AND operation IN ('1ZM', '1FM', '1HJ1', '1FZ')
AND (operation IN ('1ZM', '1FM') OR row_number() OVER (PARTITION BY operation, sfc, action_code ORDER BY created_timestamp DESC) = 1)
GROUP BY
date_day,
workshop;
```
优化后的查询将子查询移除,并在主查询中使用了条件筛选和窗口函数。这样可以减少不必要的子查询和额外的GROUP BY操作,提高查询性能。请根据你的实际需求进行适当的调整。
阅读全文