create table T1(id int,name varchar(4),kind bit,start_time varchar(5),end_time varchar(5))<br>insert T1 select 1,'早',0,'07:01','10:00'<br>union all select 2,'中',0,'10:01','15:00'<br>union all select 3,'晚',0,'15:01','22:00'<br>union all select 4,'夜',1,'22:01','07:00' <br><br>create table T2(id int identity(1,1),dt datetime,amount money)<br>insert t2 select '2007-05-02 10:34:10',7.5<br>union all select '2007-05-02 10:50:10',100.00<br>union all select '2007-05-03 03:10:10',11.1<br>union all select '2007-05-03 10:50:10',100.00<br>union all select '2007-05-03 23:50:10',100.00<br>union all select '2007-05-03 15:12:11',5.5<br>union all select '2007-05-04 19:10:10',3.5<br>union all select '2007-05-05 07:10:10',10<br>union all select '2007-05-05 08:10:10',12<br>union all select '2007-05-05 09:10:10',13.6<br>union all select '2007-05-04 04:50:10',100.00<br><br>select * from T1<br>select * from T2<br><br>select identity(int, 1, 1) id, convert(varchar(10), dt, 120) date, count(*) ct, sum(amount) je<br>into #t1<br>from t2 <br>where dt >= convert(datetime, (convert(varchar(10), dt, 120) + ' 07:00:00')) and <br>dt < convert(datetime, (convert(varchar(10), dt, 120) + ' 10:00:00'))<br>group by convert(varchar(10), dt, 120)<br><br>select identity(int, 1, 1) id, convert(varchar(10), dt, 120) date, count(*) ct, sum(amount) je<br>into #t2<br>from t2 <br>where dt >= convert(datetime, (convert(varchar(10), dt, 120) + ' 10:00:00')) and <br>dt < convert(datetime, (convert(varchar(10), dt, 120) + ' 15:00:00'))<br>group by convert(varchar(10), dt, 120)<br><br>select identity(int, 1, 1) id, convert(varchar(10), dt, 120) date, count(*) ct, sum(amount) je<br>into #t3<br>from t2 <br>where dt >= convert(datetime, (convert(varchar(10), dt, 120) + ' 15:00:00')) and <br>dt < convert(datetime, (convert(varchar(10), dt, 120) + ' 22:00:00'))<br>group by convert(varchar(10), dt, 120)<br><br><br>select identity(int, 1, 1) id, date, sum(ct) ct, sum(je) je <br>into #t4<br>from (<br> select convert(varchar(10), dt, 120) date, count(*) ct, sum(amount) je<br> from t2 <br> where dt >= convert(datetime, (convert(varchar(10), dt, 120) + ' 22:00:00')) and <br> dt < convert(datetime, (convert(varchar(10), dt, 120) + ' 23:59:59'))<br> group by convert(varchar(10), dt, 120)<br>union all<br> select convert(varchar(10), convert(datetime, date) - 1, 120) date, ct, je from<br> (select convert(varchar(10), dt, 120) date, count(*) ct, sum(amount) je<br> from t2 <br> where dt >= convert(datetime, (convert(varchar(10), dt, 120) + ' 00:00:00')) and <br> dt < convert(datetime, (convert(varchar(10), dt, 120) + ' 07:00:00'))<br> group by convert(varchar(10), dt, 120)) a) b <br>group by date<br><br><br>Create table Tmp(date varchar(10), zc int, zj numeric(18, 9), <br> hc int, hj numeric(18, 9), <br> wc int, wj numeric(18, 9), <br> yc int, yj numeric(18, 9))<br><br>insert into tmp(date) select '2007-05-02'<br>union all select '2007-05-03'<br>union all select '2007-05-04'<br>union all select '2007-05-05'<br><br>select * from tmp<br><br>update tmp<br>set zc = ct, zj = je<br>from #t1 <br>where #t1.date = tmp.date<br><br>update tmp<br>set hc = ct, hj = je<br>from #t2 <br>where #t2.date = tmp.date<br><br>update tmp<br>set wc = ct, wj = je<br>from #t3 <br>where #t3.date = tmp.date<br><br>update tmp<br>set yc = ct, yj = je<br>from #t4 <br>where #t4.date = tmp.date<br><br>select * from tmp