如何求一个月内的每天某个字段的记录不在之前时间记录中的数量

liuguojiank 2012-08-28 04:03:26
如题,如何求一个月内的每天某个字段的记录不在之前时间记录中的数量,假设有一个客户表:
create table customer
(
mobile varchar(20),
dealtime datetime,
)

需要求出dealtime每天的记录不在之前的时间记录中的数量,假设要求8月10号的,就是要求出8月10号所拨打的电话号码中不在8月1号之前的记录

select '2012-08-10' as 'date',COUNT(1) from customer where convert(char(10),dealtime,121) = '2012-08-10' and mobile not in (select mobile from customer b where convert(char(10), b.dealtime,121) < '2012-08-10')

这样写也是可以的,但是要每天每天的拼起来,有没办法循环出来,要求的结果是

dealtime count
2012-08-10 94
2012-08-11 88
2012-08-13 101


这样的格式的,请大家指教
...全文
77 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
coleling 2012-08-30
  • 打赏
  • 举报
回复

create table customer
(
mobile varchar(20),
dealtime datetime,
)

insert into customer values('123','2012-01-01 00:01:00')
insert into customer values('456','2012-01-01 00:02:00')
insert into customer values('123','2012-01-02 00:01:00')
insert into customer values('456','2012-01-02 00:02:00')
insert into customer values('789','2012-01-02 00:03:00')

with t as(select CONVERT(date,dealtime) dealdate,mobile from customer)
select o.dealdate,COUNT(*) count
from t o where o.mobile not in (select i.mobile from t i where i.dealdate < o.dealdate)
group by o.dealdate
order by o.dealdate

dealdate count
---------- -----------
2012-01-01 2
2012-01-02 1

(2 row(s) affected)


语句应该没什么问题,效率就不敢说了...
spiritofdragon 2012-08-28
  • 打赏
  • 举报
回复
--这联查看看对不。但速度肯定相当慢...
with t1 as (
select distinct convert(char(10), dealtime,121) dealtime from customer order by convert(char(10), dealtime,121)
)
select t1.dealtime
,count(*) [count]
from t1
join customer c on t1.dealtime=convert(char(10), c.dealtime,121)
where
c.mobile not in (select distinct mobile from customer where convert(char(10), dealtime,121)<t1.dealtime)
group by t1.dealtime
order by t1.dealtime
liuguojiank 2012-08-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
引用 1 楼 的回复:
select convert(char(10), dealtime,121) dealtime,count(*) [count]
from customer
group by convert(char(10), dealtime,121)
order by convert(char(10), dealtime,121)
--这样简单理解,对么?
楼上正解
[/Quote]

理解错了,不是这样的,看3楼
liuguojiank 2012-08-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
select convert(char(10), dealtime,121) dealtime,count(*) [count]
from customer
group by convert(char(10), dealtime,121)
order by convert(char(10), dealtime,121)
--这样简单理解,对么?
[/Quote]

这样只是求出每天有多少数据量而已,不是求出每天与那天之前数据不重复的数量,比如我在1日拨打了两个号码,分别是123和456,我2日拨打了三个号码,分别是123,456,789,那我出来的结果是

1日 2
2日 1

因为2日拨打的号码里面1日已经拨过了,所以是重复数据,不能算在结果里面,只有789这个号码是非重复数据,要算在结果里面


q465897859 2012-08-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
select convert(char(10), dealtime,121) dealtime,count(*) [count]
from customer
group by convert(char(10), dealtime,121)
order by convert(char(10), dealtime,121)
--这样简单理解,对么?
[/Quote]楼上正解
spiritofdragon 2012-08-28
  • 打赏
  • 举报
回复
select convert(char(10), dealtime,121) dealtime,count(*) [count]
from customer
group by convert(char(10), dealtime,121)
order by convert(char(10), dealtime,121)
--这样简单理解,对么?

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧