关于SQL语句的一个问题要查询11月的销售金额的总数(money_pay),销售笔数,VIP_code的销售金额总数,销售笔数
关于SQL语句的一个问题
要查询11月的销售金额的总数(money_pay),销售笔数,VIP_code的销售金额总数,销售笔数,非会员的销售金额总数,销售总数.
表名是sale 领导叫用一条语句查出来 请教下怎么写
[解决办法]
select sum(decode(vip_code, null, 0, money_pay)) as 非会员收入,
sum(decode(vip_code, null, 0, 1)) as 非会员人数,
sum(decode(vip_code, null, money_pay, 0)) as 会员收入,
sum(decode(vip_code, null, 1, 0)) as 会员人数,
sum(money_pay) as 总收入,
count(1) as 总人数
from sale t
where s_date >= '01-nov-11'
and s_date <= '30-nov-11'
[解决办法]
- SQL code
select a.rs 总人数,a.pay 总支付,b.rs 会员人数,b.pay 会员支付,c.rs 非会员,c.pay 非会员支付 from (select sum(money_pay)pay,sum(1)rs from sale t where s_date>='01-nov-11' and s_date<='30-nov-11')a,(select sum(money_pay)pay,sum(1)rs from sale t where s_date>='01-nov-11' and s_date<='30-nov-11' and t.vip_code is not null)b,(select sum(money_pay)pay,sum(1)rs from sale t where s_date>='01-nov-11' and s_date<='30-nov-11' and t.vip_code is null)c注:三个子句都是单条记录,所以可以放心连接,不用加任何条件,只要把三条记录合在一起就行了。另:加中文列名,是为了方便看,其实在实际中还是少用为好.如果还有什么问题,可以找我。
