日期:2014-05-17  浏览次数:21064 次

sql求取变更次数方法求助
现在有表
ba_purchase 
字段如下:
id,purchase_code 套餐编号,purchase_lowest_cost 套餐金额,date(每天都有记录),sp_code 企业编号
现在要计算,任意一段时间内,企业套餐变更的次数, 变更的条件就是 pruchase_code不一样;
变更的企业个数, 条件是在任意一段时间内,只要该企业有变更就计算;
求各位大侠帮忙啊!

------解决方案--------------------
SQL code

select count(1) 
from ba_purchase
where date>=? and data<=?
group by sp_code
having count(purchase_code)>1

------解决方案--------------------
select count(distinct purchase_code)
from ba_purchase
 where sp_code = 企业编号
and date between 开始日期 and 截止日期;
 
select count(*)
from (select *
From ba_purchase
where date between 开始日期 and 截止日期
group by sp_code, purchase_code
having count(*) > 1);