开发者

MySQL - Group By multiple conditions

开发者 https://www.devze.com 2023-01-29 04:42 出处:网络
Im trying to group cerain info together but the way type A and type B data looks is very different. what im trying to figure out is how to create a condition 开发者_运维百科inside a group by statemen

Im trying to group cerain info together but the way type A and type B data looks is very different.

what im trying to figure out is how to create a condition 开发者_运维百科inside a group by statement.

ie


GROUP BY

click.employee_id, click.mode, asset.location, click.paper_size, asset.name

OR

click.mode, asset.location, click.paper_size, asset.name

HAVING click.employee_id >= 10000000


If the employee_id is greater than 100000 it should only group by the 2nd condition, and not with the click.employee_id in mind.

Thanx in advance


How about using a CASE statement in the GROUP BY

Something like

GROUP BY

CASE WHEN click.employee_id > 100000 THEN 1 ELSE click.employee_id END , 
click.mode, 
asset.location, 
click.paper_size, 
asset.name


I agree with @astander. However, one caveat...

Before SQL actually starts processing a query, it actually runs it two times. Once just to get a final output structure of the query to know the final column widths and data types. If the first record encounted by the engine is an employee ID LESS THAN the 100,000 range, the CASE will return a 1 and might use a smaller data column type than the employee ID column. So, to fix, you might want to do

then 00000001 else click.employee_id ...

At least I've seen this happen before and just a heads-up on it.

0

精彩评论

暂无评论...
验证码 换一张
取 消