跪求跨天查询的合理解决方案(150分)

  • 主题发起人 主题发起人 kukow
  • 开始时间 开始时间
K

kukow

Unregistered / Unconfirmed
GUEST, unregistred user!
我们经常用到日期查询,要写个复杂的查询有时很是复杂,现在遇到一个跨天查询的例子!如下:<br>use tempdb<br>go<br>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' &nbsp; (kind 为1表示跨天)<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-03 03:10:10',11.1<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><br><br>我要查询出以下结果:<br>日期 &nbsp; &nbsp;次数(早) &nbsp; &nbsp; 金额(早) &nbsp; 次数(中) &nbsp; &nbsp; 金额(中) 次数(晚) &nbsp; &nbsp; 金额(晚) 次数(夜) &nbsp; &nbsp; 金额(夜) &nbsp; 合计次数 &nbsp;合计金额<br><br><br>如何写SQL语句<br>分不够可以再加...
 
use tempdb <br>go <br>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' &nbsp; (kind 为1表示跨天) <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-03 03:10:10',11.1 <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><br><br>我要查询出以下结果: <br>日期 &nbsp; &nbsp;次数(早) &nbsp; &nbsp; 金额(早) &nbsp; 次数(中) &nbsp; &nbsp; 金额(中) 次数(晚) &nbsp; &nbsp; 金额(晚) 次数(夜) &nbsp; &nbsp; 金额(夜) &nbsp; 合计次数 &nbsp;合计金额 <br>2007-05-02 &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;7.5 &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 11.1 &nbsp; &nbsp; &nbsp; &nbsp;2 &nbsp; &nbsp; &nbsp; 18.6 <br>2007-05-03 &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5.5 &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; &nbsp; 5.5 <br>2007-05-04 &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3.5 &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; &nbsp; 3.5 <br>我要得到以上结果,如何写查询语句....
 
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 &nbsp;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 &gt;= &nbsp;convert(datetime, (convert(varchar(10), dt, 120) + ' 07:00:00')) and <br>dt &lt; convert(datetime, (convert(varchar(10), dt, 120) + ' 10:00:00'))<br>group by convert(varchar(10), dt, 120)<br><br>select &nbsp;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 &gt;= &nbsp;convert(datetime, (convert(varchar(10), dt, 120) + ' 10:00:00')) and <br>dt &lt; convert(datetime, (convert(varchar(10), dt, 120) + ' 15:00:00'))<br>group by convert(varchar(10), dt, 120)<br><br>select &nbsp;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 &gt;= &nbsp;convert(datetime, (convert(varchar(10), dt, 120) + ' 15:00:00')) and <br>dt &lt; 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>&nbsp; select &nbsp;convert(varchar(10), dt, 120) date, count(*) ct, sum(amount) je<br>&nbsp; from t2 <br>&nbsp; where dt &gt;= &nbsp;convert(datetime, (convert(varchar(10), dt, 120) + ' 22:00:00')) and <br>&nbsp; &nbsp; &nbsp; &nbsp; dt &lt; convert(datetime, (convert(varchar(10), dt, 120) + ' 23:59:59'))<br>&nbsp; group by convert(varchar(10), dt, 120)<br>union all<br>&nbsp; select convert(varchar(10), convert(datetime, date) - 1, 120) date, ct, je from<br>&nbsp; (select &nbsp;convert(varchar(10), dt, 120) date, count(*) ct, sum(amount) je<br>&nbsp; &nbsp;from t2 <br>&nbsp; &nbsp;where dt &gt;= &nbsp;convert(datetime, (convert(varchar(10), dt, 120) + ' 00:00:00')) and <br>&nbsp; &nbsp; &nbsp; &nbsp; dt &lt; convert(datetime, (convert(varchar(10), dt, 120) + ' 07:00:00'))<br>&nbsp; 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>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hc int, hj numeric(18, 9), <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wc int, wj numeric(18, 9), <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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
 
1 早 0 07:01 10:00<br>2 中 0 10:01 15:00<br>3 晚 0 15:01 22:00<br>4 夜 1 22:01 07:00<br><br>1 2007-05-02 10:34:10.000 7.5000<br>2 2007-05-02 10:50:10.000 100.0000<br>3 2007-05-03 03:10:10.000 11.1000<br>4 2007-05-03 10:50:10.000 100.0000<br>5 2007-05-03 23:50:10.000 100.0000<br>6 2007-05-03 15:12:11.000 5.5000<br>7 2007-05-04 19:10:10.000 3.5000<br>8 2007-05-05 07:10:10.000 10.0000<br>9 2007-05-05 08:10:10.000 12.0000<br>10 2007-05-05 09:10:10.000 13.6000<br>11 2007-05-04 04:50:10.000 100.0000<br><br><br>2007-05-02 NULL NULL NULL NULL NULL NULL NULL NULL<br>2007-05-03 NULL NULL NULL NULL NULL NULL NULL NULL<br>2007-05-04 NULL NULL NULL NULL NULL NULL NULL NULL<br>2007-05-05 NULL NULL NULL NULL NULL NULL NULL NULL<br><br><br>2007-05-02 NULL NULL 2 107.500000000 NULL NULL 1 11.100000000<br>2007-05-03 NULL NULL 1 100.000000000 1 5.500000000 2 200.000000000<br>2007-05-04 NULL NULL NULL NULL 1 3.500000000 NULL NULL<br>2007-05-05 3 35.600000000 NULL NULL NULL NULL NULL NULL<br>//----------
 
use tempdb <br>go <br>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' &nbsp; --(kind 为1表示跨天) <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-03 03:10:10',11.1 <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><br><br><br><br><br>select * <br>&nbsp; &nbsp; ,合计次数 = [次数(早)]+[次数(中)]+[次数(晚)]+[次数(夜)]<br>&nbsp; &nbsp; ,合计金额 = [金额(早)]+[金额(中)]+[金额(晚)]+[金额(夜)]<br>from (<br>&nbsp; &nbsp; select date<br>&nbsp; &nbsp; &nbsp; &nbsp; ,sum(case when name='早' then 1 else 0 end) as [次数(早)]<br>&nbsp; &nbsp; &nbsp; &nbsp; ,sum(case when name='早' then amount else 0 end) as [金额(早)]<br>&nbsp; &nbsp; &nbsp; &nbsp; ,sum(case when name='中' then 1 else 0 end) as [次数(中)] <br>&nbsp; &nbsp; &nbsp; &nbsp; ,sum(case when name='中' then amount else 0 end) as [金额(中)] <br>&nbsp; &nbsp; &nbsp; &nbsp; ,sum(case when name='晚' then 1 else 0 end) as [次数(晚)] <br>&nbsp; &nbsp; &nbsp; &nbsp; ,sum(case when name='晚' then amount else 0 end) as [金额(晚)] <br>&nbsp; &nbsp; &nbsp; &nbsp; ,sum(case when name='夜' then 1 else 0 end) as [次数(夜)] &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; ,sum(case when name='夜' then amount else 0 end) as [金额(夜)] <br>&nbsp; &nbsp; from ( &nbsp; &nbsp;select convert(varchar(10),case &nbsp;when t1.kind=1 and convert(varchar(5),t2.dt,8)&lt;=t1.end_time then dateadd(day,-1,t2.dt) else t2.dt end,120) date<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ,amount,name<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from t1 ,t2 <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where 1=<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case t1.kind when 0 then (case when convert(varchar(5),t2.dt,8) between t1.start_time and t1.end_time then 1 else 0 end) &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else (case when convert(varchar(5),t2.dt,8) &gt;= t1.start_time or convert(varchar(5),t2.dt,8)&lt;=t1.end_time then 1 else 0 end) &nbsp;end<br>&nbsp; &nbsp; &nbsp; &nbsp; ) a<br>&nbsp; &nbsp; group by date<br>) m<br><br><br>问题自己解决了,大富翁现在人气越来越谈了.结贴了...
 
后退
顶部