i******7 发帖数: 421 | 1 SQL 2008
有这样一个table:
phonenum calltime rowID
1 2/4/2010 1
4 2/8/2010 2
2 6/18/2010 3
2 6/20/2010 5
3 8/20/2010 6
4 2/9/2010 7
4 2/11/2010
… … …
我现在要做一个report,就是显示每个phonenum每天收到多少个call,只需要显示前90天
.比如phone1的第一个call是2/4.从那天开始的往后90天,每天收到多少个call.phone4
的第一个call是2/8.那么分别是day1有一个call, day2有一个call,day4有一个call.
具体report的格式如下.
phonenum 1st day 2nd day 3rd day 4th day … 90th day
1 1
2 1 1
3 1
4 1 1 1
有没有大牛知道这个怎么做?
谢谢! | i******7 发帖数: 421 | 2 格式如下也可以:
day phone1 phone2 phone3
day1
day2
day3
…
day90
包子谢!!
【在 i******7 的大作中提到】 : SQL 2008 : 有这样一个table: : phonenum calltime rowID : 1 2/4/2010 1 : 4 2/8/2010 2 : 2 6/18/2010 3 : 2 6/20/2010 5 : 3 8/20/2010 6 : 4 2/9/2010 7 : 4 2/11/2010
| c*******e 发帖数: 8624 | 3 先做一个辅助table
select phonenum ,
min(calltime) as min_calltime
from your_table
group by 1
然后数的时候用case
select a.phonenum ,
count(case when a.calltime between b.min_calltime and b.min_calltime
+ 1
then a.phonenum else null end) as day1_cnt ,
...
from your_table a
join table_above b
on a.phonenum = b.phonenum
group by 1
order by 1 ;
【在 i******7 的大作中提到】 : SQL 2008 : 有这样一个table: : phonenum calltime rowID : 1 2/4/2010 1 : 4 2/8/2010 2 : 2 6/18/2010 3 : 2 6/20/2010 5 : 3 8/20/2010 6 : 4 2/9/2010 7 : 4 2/11/2010
| i******7 发帖数: 421 | 4 谢谢回复.包子送上。
但是这个a.calltime between b.min_calltime and b.min_calltime + 1貌似不work啊
..而且是90天,要写90个case when么?
【在 c*******e 的大作中提到】 : 先做一个辅助table : select phonenum , : min(calltime) as min_calltime : from your_table : group by 1 : 然后数的时候用case : select a.phonenum , : count(case when a.calltime between b.min_calltime and b.min_calltime : + 1 : then a.phonenum else null end) as day1_cnt ,
| B*****g 发帖数: 34098 | 5 这个是第一天的,sql里的...就是1到90。hoho
1
【在 i******7 的大作中提到】 : 谢谢回复.包子送上。 : 但是这个a.calltime between b.min_calltime and b.min_calltime + 1貌似不work啊 : ..而且是90天,要写90个case when么?
| g***l 发帖数: 18555 | 6 这个是最简单的
select phonenum,
DateDiff(dd, '12/31/2009',phonedate)
as DayNumber,
1 as PhoneCount
into PhoneCountTemp
from table
select phonenum,DayNumber, sum(phonecount) as PhoneCount
from PhoneCountTemp
group by phonenum, daynumber
order by phonenum, daynumber | c*******e 发帖数: 8624 | 7 你有91个column,当然要写90个count了
【在 i******7 的大作中提到】 : 谢谢回复.包子送上。 : 但是这个a.calltime between b.min_calltime and b.min_calltime + 1貌似不work啊 : ..而且是90天,要写90个case when么?
| i******7 发帖数: 421 | 8 谢谢.包子送上..
但貌似这个是从1/1开始算第一天,实际上有的电话是从2月1号才收到call.我想以第一
次收到call的时候算第一天,而不是按照日历算.
【在 g***l 的大作中提到】 : 这个是最简单的 : select phonenum, : DateDiff(dd, '12/31/2009',phonedate) : as DayNumber, : 1 as PhoneCount : into PhoneCountTemp : from table : select phonenum,DayNumber, sum(phonecount) as PhoneCount : from PhoneCountTemp : group by phonenum, daynumber
| i******7 发帖数: 421 | 9 哦.我可能没有说清楚.实际上这个calltime的格式是:mm/dd/yyyy hh:mm:ss的.所以觉
得那个+1可能不work.如果格式包括时间,该怎么写呢?
发信人: cheungche (你不乖), 信区: Database
标 题: Re: 请教一下这个report的query应该怎么样写?
发信站: BBS 未名空间站 (Mon Oct 18 18:44:55 2010, 美东)
先做一个辅助table
select phonenum ,
min(calltime) as min_calltime
from your_table
group by 1
然后数的时候用case
select a.phonenum ,
count(case when a.calltime between b.min_calltime and b.min_calltime
+ 1
then a.phonenum else null end) as day1_cnt ,
...
from your_table a
join table_above b
on a.phonenum = b.phonenum
group by 1
order by 1 ;
【在 c*******e 的大作中提到】 : 你有91个column,当然要写90个count了
| i******7 发帖数: 421 | 10 谢谢回复.包子送上。
那有啥别的写法么?
【在 B*****g 的大作中提到】 : 这个是第一天的,sql里的...就是1到90。hoho : : 1
| B*****g 发帖数: 34098 | 11 哪天你用oracle,我给你个解法
【在 i******7 的大作中提到】 : 谢谢回复.包子送上。 : 那有啥别的写法么?
| g***l 发帖数: 18555 | 12 我改了,如果把2/1/2010当第一天,就是DATEDIFF(DD,'1/31/2010',PHONEDATE) | c*******e 发帖数: 8624 | 13 你cast成日期不就可以了?
【在 i******7 的大作中提到】 : 哦.我可能没有说清楚.实际上这个calltime的格式是:mm/dd/yyyy hh:mm:ss的.所以觉 : 得那个+1可能不work.如果格式包括时间,该怎么写呢? : : 发信人: cheungche (你不乖), 信区: Database : 标 题: Re: 请教一下这个report的query应该怎么样写? : 发信站: BBS 未名空间站 (Mon Oct 18 18:44:55 2010, 美东) : 先做一个辅助table : select phonenum , : min(calltime) as min_calltime : from your_table
|
|