需要帮助我的SQL查询
ActiveSheet.Range("$A$1:$C$29663").AutoFilter Field:=1, Criteria1:= _
Worksheets("Sheet1").Range("B3").Value, Operator:=xlAnd, Criteria2:=Worksheets("Sheet1").Range("B4").Value
我的结果显示如下
请告诉我为什么国家电话没有分组。我想找到国内电话和国际电话的总和
答案 0 :(得分:1)
在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
答案 1 :(得分:0)
您不需要子查询,因为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
和select
:
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;
如果您想要每个组中的客户ID列表,您可以随时添加group_concat(customer_id)
。