根据时间查询的问题!!!力求高手大神啊!!!!!!!!!!!!!!!

小尛憨 2013-02-21 01:21:48
table_1

ID,time,Msg三字段

time格式:'2012-12-21 11:56:00'

现在的要求是:
通过两个日期,例'2012-12-01','2012-12-31'
按每天排序并查询,从7~22点每个小时的count(ID),也就是记录总数

效果图:
12-1 | 12-2 | 12-3 | 12-4 | ... | 12-31
7 23 31 78 58 ... 12
8 102 ...
9 153
10 156
. .
. .
. .
22 87
...全文
697 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
szm341 2013-03-06
  • 打赏
  • 举报
回复
最近没事呵呵,那个首行null你可以用#13的代码看看数据分布,是哪里计算错误了
小尛憨 2013-03-06
  • 打赏
  • 举报
回复
引用 21 楼 szm341 的回复:
SQL code ? 12345678910111213141516171819202122232425 declare @sql nvarchar(max),@col nvarchar(max),@s_time varchar(10),@e_time varchar(10) set @s_time='2012-11-01'set @e_time='2012-12-01';w……
嘿嘿,谢谢你啊,真是不好意思老麻烦你啊
szm341 2013-03-06
  • 打赏
  • 举报
回复


declare @sql nvarchar(max),@col nvarchar(max),@s_time varchar(10),@e_time varchar(10)
set @s_time='2012-11-01'
set @e_time='2012-12-01'
;with cte as(
select dateadd(day,number,@s_time) time
from master..spt_values where type='P' and number<31
)select @sql=isnull(@sql+',','')+ quotename(convert(varchar,time,23))
,@col=isnull(@col+',isnull(','isnull(')+ quotename(convert(varchar,time,23))+',0) as '+quotename(convert(varchar,time,23)) 
from cte where time<@e_time
    
set @sql=';with cte as(
select convert(varchar,dateadd(day,number,@s_time),23) time
from master..spt_values where type=''P'' and number<31
),cte1 as
(select convert(varchar,DealStartTime,23) t,datename(hh,DealStartTime) h,count(1) c
from NormalDealInfo where DealStartTime>@s_time and DealStartTime<@e_time 
and CONVERT(varchar(8),DealStartTime,108) between ''07:00:00'' and ''23:00:00''
group by convert(varchar,DealStartTime,23),datename(hh,DealStartTime)
),cte2 as
(select a.time,b.h,b.c from cte a left join cte1 b on a.time=b.t where a.time<@e_time)
select h,'+@col+' from cte2 pivot (max(c) for time in ('+@sql+'))a'
select @sql
exec sp_executesql @sql,N'@s_time varchar(10),@e_time varchar(10)',@s_time,@e_time

小尛憨 2013-03-06
  • 打赏
  • 举报
回复
引用 19 楼 szm341 的回复:
去掉select @sql这句就不输出查询语句了,一般输出用来检查语法的 至于那个null得从数据上分析了,我这儿看语法没什么毛病呵呵
不好意思,能不能帮我把isnull(value,0)加上啊?在哪里加啊,我试了一下,不是语法错误,就是只有第一排会改成0,其他的还是NULL
szm341 2013-03-06
  • 打赏
  • 举报
回复
去掉select @sql这句就不输出查询语句了,一般输出用来检查语法的 至于那个null得从数据上分析了,我这儿看语法没什么毛病呵呵
小尛憨 2013-03-06
  • 打赏
  • 举报
回复
引用 17 楼 szm341 的回复:
呵呵,时间判断那个我忘记加了,昨天赶时间

那个问题是我为了迎合你上面写的列名条件,time for(列名)这里
列名我写的11-01这样子的,实际处理得到的是2012-11-01这样的,所以无法匹配

T。T
又有问题了,它返回的先是一句查询语句再返回一个表,怎么直接返回下面这个表啊?

还有,表里第一行NULL虽然没有影响,但是能不能去掉呢?查询怎么会多一条呢
szm341 2013-03-01
  • 打赏
  • 举报
回复
呵呵,时间判断那个我忘记加了,昨天赶时间 那个问题是我为了迎合你上面写的列名条件,time for(列名)这里 列名我写的11-01这样子的,实际处理得到的是2012-11-01这样的,所以无法匹配
小尛憨 2013-02-28
  • 打赏
  • 举报
回复
引用 10 楼 szm341 的回复:
嗯,我写错了,粘贴你的代码时没注意,for后面的列改成time再运行一下看看 SQL code ? 1234567891011121314151617181920212223 declare @sql nvarchar(max),@s_time varchar(10),@e_time varchar(10) set @s_time='2012-11-01'set……
对了,补充一下,我单独抽取里面查询语句查询的话是有数据的 select cast(datename(hh,DealStartTime) as int) h,convert(varchar,DealStartTime,23) t,count(*) c from NormalDealInfo where DealStartTime >@s_time and DealStartTime<@e_time and CONVERT(varchar(8),DealStartTime,108) between '07:00:00' and '22:00:00' group by cast(datename(hh,DealStartTime) as int),convert(varchar,DealStartTime,23) 在整个查询语句里时间是从开始31天,而在这里查询的时间会根据e_time和s_time查询的,前后肯定会有冲突的吧?
小尛憨 2013-02-28
  • 打赏
  • 举报
回复
引用 10 楼 szm341 的回复:
嗯,我写错了,粘贴你的代码时没注意,for后面的列改成time再运行一下看看



SQL code
?



1234567891011121314151617181920212223

declare @sql nvarchar(max),@s_time varchar(10),@e_time varchar(10) set @s_time='2012-11-01'set……


不好意思,那天跟老总临时出差去了
今早我从新研究了下
你这个脚本里选择日期是用
select convert(varchar,dateadd(day,number,@s_time),23) time
from master..spt_values where type='P' and number<31
这句代码控制,而不是用我传入的参数@s_time和@e_time控制的啊,固定从开始算起31天
另外

查询出来都是NULL啊,有数据里面,而且多了第一行不知道什么数据,然而22时的那行没有了
囧啊,因为有些代码不知道什么意思。所以另外我改了下我会改的地方,因为之前排序有问题嘛。。更新代码为:

declare @sql nvarchar(max),@s_time varchar(10),@e_time varchar(10)
set @s_time='2012-11-01'
set @e_time='2012-12-01'
;with cte as(
select dateadd(day,number,@s_time) time
from master..spt_values where type='P' and number<31
)
select @sql=isnull(@sql+',','')+ quotename(right(convert(varchar,time,23),5)) from cte where time<@e_time

set @sql=';with cte as(
select convert(varchar,dateadd(day,number,@s_time),23) time
from master..spt_values where type=''P'' and number<31
),cte1 as
(select cast(datename(hh,DealStartTime) as int) h,convert(varchar,DealStartTime,23) t,count(*) c
from NormalDealInfo where DealStartTime >@s_time and DealStartTime<@e_time
and CONVERT(varchar(8),DealStartTime,108) between ''07:00:00'' and ''22:00:00''
group by cast(datename(hh,DealStartTime) as int),convert(varchar,DealStartTime,23)
),cte2 as
(select a.time,b.h,b.c from cte a left join cte1 b on a.time=b.t where a.time<@e_time)
select * from cte2 pivot (max(c) for time in ('+@sql+'))a'
select @sql
exec sp_executesql @sql,N'@s_time varchar(10),@e_time varchar(10)',@s_time,@e_time
小尛憨 2013-02-28
  • 打赏
  • 举报
回复
引用 15 楼 szm341 的回复:
再试一下 SQL code ? 1234567891011121314151617181920212223 declare @sql nvarchar(max),@s_time varchar(10),@e_time varchar(10) set @s_time='2012-11-01'set @e_time='2012-12-01';with cte as( s……
有了有了!问题出在哪啊? 至于控制时间范围我加了个参数,也OK了!! 多谢大神,请受小女一拜!
szm341 2013-02-28
  • 打赏
  • 举报
回复
再试一下


declare @sql nvarchar(max),@s_time varchar(10),@e_time varchar(10)
set @s_time='2012-11-01'
set @e_time='2012-12-01'
;with cte as(
select dateadd(day,number,@s_time) time
from master..spt_values where type='P' and number<31
)select @sql=isnull(@sql+',','')+ quotename(convert(varchar,time,23)) from cte where time<@e_time
   
set @sql=';with cte as(
select convert(varchar,dateadd(day,number,@s_time),23) time
from master..spt_values where type=''P'' and number<31
),cte1 as
(select convert(varchar,DealStartTime,23) t,datename(hh,DealStartTime) h,count(1) c
from NormalDealInfo where DealStartTime >@s_time and DealStartTime<@e_time 
and CONVERT(varchar(8),DealStartTime,108) between ''07:00:00'' and ''23:00:00''
group by convert(varchar,DealStartTime,23),datename(hh,DealStartTime)
),cte2 as
(select a.time,b.h,b.c from cte a left join cte1 b on a.time=b.t where a.time<@e_time)
select * from cte2 pivot (max(c) for time in ('+@sql+'))a'
select @sql
exec sp_executesql @sql,N'@s_time varchar(10),@e_time varchar(10)',@s_time,@e_time

小尛憨 2013-02-28
  • 打赏
  • 举报
回复
引用 13 楼 szm341 的回复:
时间那个改成22:59:59,去掉行转列部分,先看一下是否有正确的数据? SQL code ? 12345678910111213141516171819202122 declare @sql nvarchar(max),@s_time varchar(10),@e_time varchar(10) set @s_time='2012-01-01'set @e_ti……
有数据,不过还是没有22点的,因为22点过后数据都比较少,一个月下来就几天又22点的数据
szm341 2013-02-28
  • 打赏
  • 举报
回复
时间那个改成22:59:59,去掉行转列部分,先看一下是否有正确的数据?


declare @sql nvarchar(max),@s_time varchar(10),@e_time varchar(10)
set @s_time='2012-01-01'
set @e_time='2012-02-01'
;with cte as(
select dateadd(day,number,@s_time) time
from master..spt_values where type='P' and number<31
)select @sql=isnull(@sql+',','')+ quotename(right(convert(varchar,time,23),5)) from cte where time<@e_time
  
set @sql=';with cte as(
select convert(varchar,dateadd(day,number,@s_time),23) time
from master..spt_values where type=''P'' and number<31
),cte1 as
(select convert(varchar,NormalDealInfo,23) t,datename(hh,NormalDealInfo) h,count(1) c
from NormalDealInfo where NormalDealInfo >@s_time and NormalDealInfo<@e_time 
and CONVERT(varchar(8),b.DealStartTime,108) between '07:00:00' and '22:59:59'
group by convert(varchar,NormalDealInfo,23),datename(hh,NormalDealInfo)
)select a.time,b.h,b.c from cte a left join cte1 b on a.time=b.t'
select @sql
exec sp_executesql @sql,N'@s_time varchar(10),@e_time varchar(10)',@s_time,@e_time


小尛憨 2013-02-22
  • 打赏
  • 举报
回复
引用 8 楼 szm341 的回复:
SQL code
?



1234567891011121314151617181920212223

declare @sql nvarchar(max),@s_time varchar(10),@e_time varchar(10) set @s_time='2012-01-01'set @e_time='2012-02-01';with cte as( select date……

有些地方写错了,我也不知道是不是你写错了,我改了,通过了,然后查询结果好像有点问题。。囧
改过后代码为:

declare @sql nvarchar(max),@s_time varchar(10),@e_time varchar(10)
set @s_time='2012-11-01'
set @e_time='2012-12-01'
;with cte as(
select dateadd(day,number,@s_time) time
from master..spt_values where type='P' and number<31
)select @sql=isnull(@sql+',','')+ quotename(right(convert(varchar,time,23),5)) from cte where time<@e_time

set @sql=';with cte as(
select convert(varchar,dateadd(day,number,@s_time),23) time
from master..spt_values where type=''P'' and number<31
),cte1 as
(select convert(varchar,DealStartTime,23) t,datename(hh,DealStartTime) h,count(1) c
from NormalDealInfo where DealStartTime >@s_time and DealStartTime<@e_time
and CONVERT(varchar(8),DealStartTime,108) between ''07:00:00'' and ''22:00:00''
group by convert(varchar,DealStartTime,23),datename(hh,DealStartTime)
),cte2 as
(select a.time,b.h,b.c from cte a left join cte1 b on a.time=b.t)
select * from cte2 pivot (max(c) for h in ('+@sql+'))a'
select @sql
exec sp_executesql @sql,N'@s_time varchar(10),@e_time varchar(10)',@s_time,@e_time


szm341 2013-02-22
  • 打赏
  • 举报
回复


declare @sql nvarchar(max),@s_time varchar(10),@e_time varchar(10)
set @s_time='2012-01-01'
set @e_time='2012-02-01'
;with cte as(
select dateadd(day,number,@s_time) time
from master..spt_values where type='P' and number<31
)select @sql=isnull(@sql+',','')+ quotename(right(convert(varchar,time,23),5)) from cte where time<@e_time
  
set @sql=';with cte as(
select convert(varchar,dateadd(day,number,@s_time),23) time
from master..spt_values where type=''P'' and number<31
),cte1 as
(select convert(varchar,NormalDealInfo,23) t,datename(hh,NormalDealInfo) h,count(1) c
from NormalDealInfo where NormalDealInfo >@s_time and NormalDealInfo<@e_time 
and CONVERT(varchar(8),b.DealStartTime,108) between '07:00:00' and '22:00:00'
group by convert(varchar,NormalDealInfo,23),datename(hh,NormalDealInfo)
),cte2 as
(select a.time,b.h,b.c from cte a left join cte1 b on a.time=b.t)
select * from cte2 pivot (max(c) for h in ('+@sql+'))a'
select @sql
exec sp_executesql @sql,N'@s_time varchar(10),@e_time varchar(10)',@s_time,@e_time

小尛憨 2013-02-22
  • 打赏
  • 举报
回复
引用 6 楼 szm341 的回复:
用你实际数据跑一下看看效果啊 用辅助表master..spt_values where type='P'生成一个月的日期 然后用left join关联,动态构建旋转函数列
没试出来,太深奥了,我不是数据库专业的,很多地方看不懂,就连我的表名在什么地方换上去都不知道啊 我是单表查询,就是想按这个格式查询出一个月的数据,字段名和列名没有也没关系 我单独的查询一天的我是写出来了: Create Procedure HourSortByDay @WhenDay DateTime as declare @t table ( Hour int) declare @i int set @i =7 while @i<23 begin insert @t values(@i) set @i=@i+1 end select count(*) as 总单 from @t t left join NormalDealInfo b on t.Hour=datepart(hour,b.DealEndTime) and convert(varchar(10),b.DealStartTime,120) = @WhenDay and CONVERT(varchar(8),b.DealStartTime,108)between '07:00:00' and '22:00:00' group by t.Hour 是一个存储过程 NormalDealInfo是表名,这里指有一列数据,就是@WhenDay那一天每个小时的数据,但是如果说要把一个月都一起查出来就不会了。。。
szm341 2013-02-22
  • 打赏
  • 举报
回复
用你实际数据跑一下看看效果啊 用辅助表master..spt_values where type='P'生成一个月的日期 然后用left join关联,动态构建旋转函数列
小尛憨 2013-02-22
  • 打赏
  • 举报
回复
引用 4 楼 szm341 的回复:
修改一下呵呵 SQL code ? 123456789101112131415161718192021 declare @sql nvarchar(max),@s_time varchar(10),@e_time varchar(10) set @s_time='2012-01-01'set @e_time='2012-02-01';with cte as( s……
我愣是没看懂
szm341 2013-02-22
  • 打赏
  • 举报
回复
嗯,我写错了,粘贴你的代码时没注意,for后面的列改成time再运行一下看看


declare @sql nvarchar(max),@s_time varchar(10),@e_time varchar(10)
set @s_time='2012-11-01'
set @e_time='2012-12-01'
;with cte as(
select dateadd(day,number,@s_time) time
from master..spt_values where type='P' and number<31
)select @sql=isnull(@sql+',','')+ quotename(right(convert(varchar,time,23),5)) from cte where time<@e_time
   
set @sql=';with cte as(
select convert(varchar,dateadd(day,number,@s_time),23) time
from master..spt_values where type=''P'' and number<31
),cte1 as
(select convert(varchar,DealStartTime,23) t,datename(hh,DealStartTime) h,count(1) c
from NormalDealInfo where DealStartTime >@s_time and DealStartTime<@e_time 
and CONVERT(varchar(8),DealStartTime,108) between ''07:00:00'' and ''22:00:00''
group by convert(varchar,DealStartTime,23),datename(hh,DealStartTime)
),cte2 as
(select a.time,b.h,b.c from cte a left join cte1 b on a.time=b.t where a.time<@e_time)
select * from cte2 pivot (max(c) for time in ('+@sql+'))a'
select @sql
exec sp_executesql @sql,N'@s_time varchar(10),@e_time varchar(10)',@s_time,@e_time

szm341 2013-02-21
  • 打赏
  • 举报
回复
修改一下呵呵


declare @sql nvarchar(max),@s_time varchar(10),@e_time varchar(10)
set @s_time='2012-01-01'
set @e_time='2012-02-01'
;with cte as(
select dateadd(day,number,@s_time) time
from master..spt_values where type='P' and number<31
)select @sql=isnull(@sql+',','')+ quotename(right(convert(varchar,time,23),5)) from cte where time<@e_time
 
set @sql=';with cte as(
select convert(varchar,dateadd(day,number,@s_time),23) time
from master..spt_values where type=''P'' and number<31
),cte1 as
(select convert(varchar,time,23) t,datename(hh,time) h,count(1) c
from tb where time>@s_time and time<@e_time group by convert(varchar,time,23),datename(hh,time)
),cte2 as
(select a.time,b.h,b.c from cte a left join cte1 b on a.time=b.t)
select * from cte2 pivot (max(c) for h in ('+@sql+'))a'
select @sql
exec sp_executesql @sql,N'@s_time varchar(10),@e_time varchar(10)',@s_time,@e_time

加载更多回复(3)

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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