这段sql里面怎么把date_day取最小的一条: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 09:06:33 浏览: 136
sql.rar_exec select_select_sql select from exec_普通sql大全
你可以使用子查询和窗口函数来获取date_day取最小的一条记录。在子查询中,你可以使用row_number()函数对数据进行排序,并使用partition by对操作类型、sfc和action_code进行分组。然后,在外部查询中,选择row_num等于1的记录,这样就能获取到date_day取最小的一条记录。
以下是修改后的SQL语句:
```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,
row_number() OVER (PARTITION BY operation, sfc, action_code ORDER BY partition_date) AS row_num
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')
GROUP BY
action_code,
partition_date,
sfc,
operation,
item,
qty,
shop_order_bo,
resrce
)
WHERE
row_num = 1;
```
这样,你将会得到date_day取最小的一条记录。注意,我修改了一些查询条件的位置,以便更好地优化查询性能。请确保根据你的实际需求进行适当的调整。
阅读全文