年月日产品分组报表

SELECT sum(a.buycount) buycount,(SELECT c.name from tb_product_type_info c -- 按年
WHERE c.id=b.typeid) typename,year(a.createdate)as year from tb_order_detail a,tb_product_pk b
where a.productname=b.oldproduct GROUP BY b.typeid,year(a.createdate);

SELECT sum(a.buycount) buycount,(SELECT c.name from tb_product_type_info c -- 按月
WHERE c.id=b.typeid) typename,year(a.createdate)as year,month(a.createdate)as month from tb_order_detail a,tb_product_pk b
where a.productname=b.oldproduct GROUP BY b.typeid,year(a.createdate),month(a.createdate);

SELECT sum(a.buycount) buycount,(SELECT c.name from tb_product_type_info c -- 按周
WHERE c.id=b.typeid) typename,year(a.createdate)as year,week(createdate,1) as week from tb_order_detail a,tb_product_pk b
where a.productname=b.oldproduct GROUP BY b.typeid,year(a.createdate),week(a.createdate,1);

SELECT sum(a.buycount) buycount,(SELECT c.name from tb_product_type_info c -- 按季
WHERE c.id=b.typeid) typename,year(a.createdate)as year,quarter(createdate) as quarter from tb_order_detail a,tb_product_pk b
where a.productname=b.oldproduct GROUP BY b.typeid,year(a.createdate),quarter(a.createdate);

//--------------------------------0706--------------------------------

select
id,
orderno,
memberno,
username,
custname,
sex,
mobile,
address,
membertype,
membercate,
channeltype,
count(id) ordercount,
sum(payshop) orderpricetotal,
(sum(payshop)/ count(id))orderperprice,
sum(buycount) buycounttotal,
(avg(subday)/30) perfreque
from
(select
id,
orderno,
memberno,
username,
recivename AS custname,
sex,
ctmobile AS mobile,
sendaddress AS address,
membertype,
membercate,
channeltype,
payshop,
(select buycount from tb_crm_order_detail where orderno=a.orderno) buycount,
(to_days((select createdate from tb_crm_order_info where createdate>a.createdate and username=a.username order by createdate limit 0,1))-to_days(createdate)) subday
from tb_crm_order_info as a)b group by username;

//------------------------------------------------------------

select
username,
(avg(subday)/30) perfreque from(
select * from (select
username,
(to_days((select createdate from tb_crm_order_info where createdate>a.createdate and username=a.username order by createdate limit 0,1))-to_days(createdate)) subday
from tb_crm_order_info as a) as b where b.subday is not null)c group by username; -- 平均天数

 

select username, (avg(subday)/30) perfreque from( select * from (select username, (to_days((select createdate from tb_crm_order_info where createdate>a.createdate and username=a.username order by createdate limit 0,1))-to_days(createdate)) subday from tb_crm_order_info as a) as b where b.subday is not null)c group by username; -- 平均天数

select id, orderno, memberno, username, custname, sex, mobile, address, membertype, membercate, channeltype, count(id) ordercount, sum(payshop) orderpricetotal, (sum(payshop)/ count(id))orderperprice, (avg(subday)/30) perfreque from (select * from (select id, orderno, memberno, username, recivename AS custname, sex, ctmobile AS mobile, sendaddress AS address, membertype, membercate, channeltype, payshop, (to_days((select createdate from tb_crm_order_info where createdate>a.createdate and username=a.username order by createdate limit 0,1))-to_days(createdate)) subday from tb_crm_order_info as a) as b )c  group by username;

posted @ 2013-07-04 12:00  火腿骑士  阅读(182)  评论(0编辑  收藏  举报