首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求个sql.该如何处理

2012-04-21 
求个sql...??A表spidkhid zdrq sl23253182012-02-15 00:00:00.000300023253182011-03-10 17:05:33.0006002

求个sql...??
A表
spid khid zdrq sl
23253182012-02-15 00:00:00.0003000
23253182011-03-10 17:05:33.000600
23253182011-03-10 17:05:33.00011400
23253182012-01-16 00:00:00.0003000
B表
id spid khid xyksrq xyjsrq
1 23253182011-01-01 00:00:00.0002011-12-31 00:00:00.000
2 23253182012-01-01 00:00:00.0002012-12-31 00:00:00.000
求结果(合计a表在b表中日期范围内的合计数量)
id spid khid hjsl
1 2325 318 12000
2 2325 318 6000

[解决办法]

SQL code
select b.id,b.spid,b.khid ,sum( sl) from a,b where a.spid=b.spid and a.khid =b.khid and a.zdrq  between xyksrq and xyjsrqgroup by b.id,b.spid,b.khid
[解决办法]
SQL code
create table A( spid int, khid int, zdrg datetime, s1 int)create table B( id int, spid int, khid int, xyksrq datetime, xyjsrq datetime)insert into A select 2325, 318, '2012-02-15 00:00:00.000', 3000 union allselect 2325, 318, '2011-03-10 17:05:33.000', 600 union allselect 2325, 318, '2011-03-10 17:05:33.000', 11400 union allselect 2325, 318 ,'2012-01-16 00:00:00.000', 3000insert into Bselect 1,2325,318,'2011-01-01 00:00:00.000', '2011-12-31 00:00:00.000' union allselect 2,2325,318, '2012-01-01 00:00:00.000', '2012-12-31 00:00:00.000'select *,hjsl=(select SUM(s1) from A where zdrg between t1.xyksrq and t1.xyjsrq) from B t1 

热点排行