需要帮助处理SQL查询
Query :
Select customer_id,
if (call_details ='International' , 'International Calls', 'National Calls'),
sum(minutes)
from call_minutes
where date between '$from' and '$to'
group by call_details
我的结果显示如下
请告诉我为什么国家电话没有分组。我想找到国家电话和国际电话的总和。
发布于 2018-05-24 03:19:08
为此您不需要子查询,因为MySQL允许您在group by
中使用列别名(并非所有数据库都这样做)。所以:
Select customer_id,
(case when call_details = 'International'
then 'International Calls'
else 'National Calls'
end) as call_group,
sum(minutes)
from call_minutes
where date between '$from' and '$to'
group by customer_id, call_group;
注意:这假设每个客户都需要单独的行。如果没有,则同时从customer_id
和group by
中删除group by
。
Select (case when call_details = 'International'
then 'International Calls'
else 'National Calls'
end) as call_group,
sum(minutes)
from call_minutes
where date between '$from' and '$to'
group by call_group;
如果需要每个组中的客户ids列表,则始终可以添加group_concat(customer_id)
。
发布于 2018-05-23 22:43:13
在SQL下面使用:
select customer_id,
call_details,
sum(minutes) as minutes
from(
Select customer_id,
if (call_details ='International' , 'International Calls', 'National Calls') as call_details,
minutes
from call_minutes
where date between '$from' and '$to') x
group by customer_id,call_details
https://stackoverflow.com/questions/50498436
复制相似问题