简体   繁体   中英

Is it valid to use val in where condition?

My query is

SELECT count(crm_clients.client_id) as count, (((sum(expectd_rev)/1000) +((COALESCE((sum(COALESCE(client_rating,0))/count(crm_client_assign.client_id)),0)*100)/5))/392.10000000)*100 as val
FROM (`crm_clients`)
JOIN `crm_project_assign` ON `crm_clients`.`client_id` = `crm_project_assign`.`client_id`
JOIN `crm_projects` ON `crm_project_assign`.`project_id` = `crm_projects`.`project_id`
LEFT JOIN `crm_client_assign` ON `crm_clients`.`client_id` = `crm_client_assign`.`client_id`
WHERE `ctype` =  2 and crm_clients.client_id in (select client_id from crm_project_assign)

group by crm_clients.client_id
order by val desc

is it possible to use val in where condition?any solution?

It is not allowable for two reasons. The first is that you can't use column aliases in the where clause. The more important reason (in my opinion) is that val is composed of aggregation functions. You cannot use aggregation functions in the where clause.

The solution is simple: use a having clause. This goes after the group by and you can just do:

having val > 10

or whatever you would like.

No, you can't use that alias name in the where condition.

It is not allowable to refer to a column alias in a WHERE clause, because the column value might not yet be determined when the WHERE clause is executed.

You need to write that all again in the where condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM