日期:2014-05-18 浏览次数:20646 次
--问题一 declare @t table(tine nchar(10),cid int ) insert into @t select '20070906 ',1 insert into @t select '20070908 ',2 insert into @t select '20070909 ',3 insert into @t select '20071007 ',4 insert into @t select '20071007 ',5 select left(tine,6) as '月份 ',sum(cid) as '合计 ' from @t group by left(tine,6) /* 月份 合计 ------ ----------- 200709 6 200710 9 (2 行受影响) */
------解决方案--------------------
--问题二 declare @t table(tine datetime,cid int ,flag int) insert into @t select '2007-8-16 16:48:27',1,1 insert into @t select '2007-8-16 16:48:28',2,1 insert into @t select '2007-8-16 16:58:32',2,0 insert into @t select '2007-8-16 16:59:31',1,0 insert into @t select '2007-8-16 16:48:28',3,1 insert into @t select '2007-8-16 16:51:28',3,0 select min(tine) as '开始时间',max(tine) as '结束时间', cid as '按钮号',max(tine)-min(tine) as '时间长度' from @t group by cid --你要的效果 /* 开始时间 结束时间 按钮号 时间长度 ----------------------- ----------------------- ----------- ----------------------- 2007-08-16 16:48:27.000 2007-08-16 16:59:31.000 1 1900-01-01 00:11:04.000 2007-08-16 16:48:28.000 2007-08-16 16:58:32.000 2 1900-01-01 00:10:04.000 2007-08-16 16:48:28.000 2007-08-16 16:51:28.000 3 1900-01-01 00:03:00.000 (3 行受影响) */