日期:2014-05-16 浏览次数:20993 次
create table TEST (USER_ID NUMBER,TYPE_ID NUMBER,PRICE NUMBER,CREATE_TIME DATE); insert into TEST values (1,1,199,date'2012-01-01'); insert into TEST values (2,2,299,date'2012-01-01'); insert into TEST values (3,2,299,date'2012-01-01'); insert into TEST values (4,1,199,date'2012-01-01'); insert into TEST values (1,3,399,date'2012-01-11'); insert into TEST values (1,4,499,date'2012-01-11'); insert into TEST values (3,3,399,date'2012-02-01'); insert into TEST values (1,5,599,date'2012-02-01'); insert into TEST values (4,2,299,date'2012-02-01'); insert into TEST values (4,3,399,date'2012-02-01'); insert into TEST values (4,4,499,date'2012-02-01'); insert into TEST values (5,3,399,date'2012-03-01'); insert into TEST values (1,4,499,date'2012-03-01'); insert into TEST values (1,3,399,date'2012-04-01'); insert into TEST values (2,4,499,date'2012-04-01'); --(1) 查出表中不同的用户总数 select count(distinct USER_ID) c_user from TEST; --(2) 查出本月销售总额 select sum(PRICE) s_price from TEST where to_char(sysdate,'yyyy-mm')=to_char(CREATE_TIME,'yyyy-mm') --(3) 查出每天的(不同)用户数,总购买次数,销售额 select CREATE_TIME,count(distinct USER_ID) c_user,count(TYPE_ID) c_type,sum(PRICE) s_price from TEST group by CREATE_TIME
------解决方案--------------------
--(3) 查出每天的(不同)用户数,总购买次数,销售额
select CREATE_TIME,count(distinct USER_ID) c_user,count(TYPE_ID) c_type,sum(PRICE) s_price
from TEST
group by CREATE_TIME
这个有问题,购买次数不能按 商品来算,应该按 购买时间来算。
------解决方案--------------------