问题描述:
现一网站有n个注册用户、n个接口,n个合作网站作为接口用户。其中这些注册用户由各个合作网站带来,也就是说用户通过某个合作网站在本网站注册。数据库表:
如图,授权表给接`口授权合作网站,一个接口可授,权多个合作网站。
目的: 用一句sql统计查询出某月份中每天某接口的所有授权的合作网站(每接口所授权的合作网站都不相同)所带来的注册用户数量,查询条件为接口名称和月份。要求结果的行和列显示如下:
(假设)12月 1 2 3 4 5 31 (可能为28、30、31)
合作网站1
合作网站2
(选择不同的接口会列该接口所授权的合作网站)
现一网站有n个注册用户、n个接口,n个合作网站作为接口用户。其中这些注册用户由各个合作网站带来,也就是说用户通过某个合作网站在本网站注册。数据库表:
如图,授权表给接`口授权合作网站,一个接口可授,权多个合作网站。
目的: 用一句sql统计查询出某月份中每天某接口的所有授权的合作网站(每接口所授权的合作网站都不相同)所带来的注册用户数量,查询条件为接口名称和月份。要求结果的行和列显示如下:
(假设)12月 1 2 3 4 5 31 (可能为28、30、31)
合作网站1
合作网站2
(选择不同的接口会列该接口所授权的合作网站)
select max(decode(t2.rn,'01',t1.user_count,0)) as 'day 1',
max(decode(t2.rn,'02',t1.user_count,0)) as 'day 2',
......
max(decode(t2.rn,'28',t1.user_count,0) as 'day 28',
max(case when '29' > to_char(last_day(to_date('enter_month','yyyymm')),'dd')
then '本月无该日期'
else decode(t2.rn,'29',t1.user_count,0)
end) as 'day 29',
max(case when '30' > to_char(last_day(to_date('enter_month','yyyymm')),'dd')
then '本月无该日期'
else decode(t2.rn,'30',t1.user_count,0)
end) as 'day 30',
max(case when '31' > to_char(last_day(to_date('enter_month','yyyymm')),'dd')
then '本月无该日期'
else decode(t2.rn,'31',t1.user_count,0)
end) as 'day 31'
from (
select cu.wsid,
it.wsname,
to_char(cu.redate,'dd') as user_day,
count(1) as user_count
from customers cu,
interwebsites iw,
interwebsites it
where cu.interid = iw.interid
and cu.wsid = it.wsid
and iw.intername = 'interface_name'
and to_char(cu.redate,'yyyymm') = 'enter_month'
group by cu.wsid,it.wsname,to_char(cu.redate,'dd')
)t1,
(
select lpad(rownum,2,'0') rn
from all_objects a
where rownum <= to_char(last_day(to_date('enter_month','yyyymm')),'dd')
)t2
where t2.rn = t1.user_day(+)
group by t1.wsid,t1.wsname;
不好意思,忘记各表的字段说明了 ..customers (注册用户表) :
-- redate (注册时间)
-- wsid (网站序号)
-- interid (接口序号)cooWebsites (合作网站表) :
-- wsid (网站序号)
-- wsname (网站名称)interwebsites (网站接口表)
-- interid (接口序号)
-- intername (接口名称)
FYI:http://lavasoft.blog.51cto.com/62575/7482