一个SQL的问题
select b.model,sum(a.gsm_traffic) as gsmTraffic from ADS_FT_RE_GSM_IMEI a ,DW_DM_CU_IMEI b where a.imei = b.imei and gsm_traffic >= 1000 group by b.model order by gsmTraffic desc
上面这个SQL,有个条件 and gsm_traffic >= 1000,这个条件要加在gsm_traffic汇聚之后,这SQL如何改?
------解决方案--------------------加在汇聚后是不合理的,可以用Having子句,但必须是聚合内容才行。
select b.model,sum(a.gsm_traffic) as gsmTraffic
from ADS_FT_RE_GSM_IMEI a ,DW_DM_CU_IMEI b
where a.imei = b.imei and gsm_traffic >= 1000
group by b.model order by gsmTraffic desc
having sum(gsm_traffic) >= 1000
------解决方案--------------------select gsmTraffic from
(select b.model,sum(a.gsm_traffic) as gsmTraffic from ADS_FT_RE_GSM_IMEI a ,DW_DM_CU_IMEI b where a.imei = b.imei group by b.model order by gsmTraffic desc) a where gsm_traffic >= 1000
------解决方案--------------------select b.model,sum(a.gsm_traffic) as gsmTraffic
from ADS_FT_RE_GSM_IMEI a ,DW_DM_CU_IMEI b
where a.imei = b.imei /**and gsm_traffic >= 1000 **/
group by b.model having sum(gsm_traffic) >= 1000
order by gsmTraffic desc
group by 。。。having 。。。 是一组 中间不能放order by
------解决方案--------------------SQL code
select b.model,sum(a.gsm_traffic) as gsmTraffic
from ADS_FT_RE_GSM_IMEI a ,DW_DM_CU_IMEI b
where a.imei = b.imei
group by b.model
having sum(a.gsm_traffic)>=1000
order by gsmTraffic desc
------解决方案--------------------
3楼的问题估计是因为字段名不小心写错的问题,就是:
where gsm_traffic >= 1000
应该修改为:
where gsmTraffic >= 1000
我在2楼的是啥问题?贴出来看看。