with user_worker as (select Title,ID from [biUser]) select user_worker.Title as worker, (COUNT(CASE WHEN (CASE WHEN bl.Checker=0 THEN '' WHEN bl.Checker>0 and bl.Audited=-1 THEN 'NG' WHEN bl.Checker>0 and bl.Audited=1 THEN 'OK' END) = 'NG' THEN 1 END) / NULLIF(COUNT(CASE WHEN (CASE WHEN bl.Checker=0 THEN '' WHEN bl.Checker>0 and bl.Audited=-1 THEN 'NG' WHEN bl.Checker>0 and bl.Audited=1 THEN 'OK' END) = 'OK' THEN 1 END), 0)+ COUNT(CASE WHEN (CASE WHEN bl.Checker=0 THEN '' WHEN bl.Checker>0 and bl.Audited=-1 THEN 'NG' WHEN bl.Checker>0 and bl.Audited=1 THEN 'OK' END) IS NULL THEN 1 END)) AS ResultRatio from [bdWorkOrderList] bl left join [bdWorkOrder] bd on bl.OrderID=bd.ID left join [reportMJWXJL] rwx on bl.OrderID=rwx.OrderID left join [reportMJQHLHJL] rqh on bl.OrderID=rqh.OrderID left join user_worker on user_worker.ID=bl.Worker where bd.IsDeleted=0 and bl.StepName like 'Maker%' and bl.StepName in('Maker修模','maker切换料号','Maker模具保养') and DATEPART(MONTH, bl.StartTime) = DATEPART(MONTH, GETDATE()) order by user_worker.Title 这段代码出现如下报错:Column 'user_worker.Title' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
时间: 2024-04-19 14:24:10 浏览: 155
cnm.zip_CNM_matlab CNM_workera98_www.2014cnm_zip
这个错误是因为在你的查询中,选择列表中的列 'user_worker.Title' 未在聚合函数或 GROUP BY 子句中包含,而你的查询中使用了聚合函数。
根据你的需求,你可以将查询修改如下:
```sql
WITH user_worker AS (
SELECT Title, ID
FROM [biUser]
)
SELECT
user_worker.Title AS worker,
(COUNT(CASE WHEN (CASE WHEN bl.Checker = 0 THEN ''
WHEN bl.Checker > 0 AND bl.Audited = -1 THEN 'NG'
WHEN bl.Checker > 0 AND bl.Audited = 1 THEN 'OK' END) = 'NG' THEN 1 END)
/ NULLIF(COUNT(CASE WHEN (CASE WHEN bl.Checker = 0 THEN ''
WHEN bl.Checker > 0 AND bl.Audited = -1 THEN 'NG'
WHEN bl.Checker > 0 AND bl.Audited = 1 THEN 'OK' END) = 'OK' THEN 1 END), 0)
+ COUNT(CASE WHEN (CASE WHEN bl.Checker = 0 THEN ''
WHEN bl.Checker > 0 AND bl.Audited = -1 THEN 'NG'
WHEN bl.Checker > 0 AND bl.Audited = 1 THEN 'OK' END) IS NULL THEN 1 END)) AS ResultRatio
FROM [bdWorkOrderList] bl
LEFT JOIN [bdWorkOrder] bd ON bl.OrderID = bd.ID
LEFT JOIN [reportMJWXJL] rwx ON bl.OrderID = rwx.OrderID
LEFT JOIN [reportMJQHLHJL] rqh ON bl.OrderID = rqh.OrderID
LEFT JOIN user_worker ON user_worker.ID = bl.Worker
WHERE bd.IsDeleted = 0
AND bl.StepName LIKE 'Maker%'
AND bl.StepName IN ('Maker修模', 'maker切换料号', 'Maker模具保养')
AND DATEPART(MONTH, bl.StartTime) = DATEPART(MONTH, GETDATE())
GROUP BY user_worker.Title
ORDER BY user_worker.Title
```
在上述修改中,将聚合函数 `COUNT` 和条件语句都放在了 `GROUP BY` 子句中,并且将查询结果按照 `user_worker.Title` 进行分组。
请注意,根据你的具体需求,你可能还需要对其他列进行适当的聚合或添加到 `GROUP BY` 子句中。请根据实际情况进行相应的调整。
阅读全文