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

急求高手!SQL 同表中两条记录数据相加,该怎么解决

2012-03-26 
急求高手!SQL 同表中两条记录数据相加select sum(day)as 日总数,sum(week)as 周总数 from t_zs where sj b

急求高手!SQL 同表中两条记录数据相加
select sum(day)as 日总数,sum(week)as 周总数 from t_zs where sj between '2012-02-08' and '2012-02-08'

日总数 周总数
 100 100 

select sum(day)as 日总数,sum(week)as 周总数 from t_zs where sj between '2012-02-08' and '2012-02-08' and cj=2

日总数 周总数
 50 50

select sum(day)as 日总数,sum(week)as 周总数 from t_zs where sj between '2012-02-08' and '2012-02-08' and cj=3

日总数 周总数
 50 50

我现在想查cj(车间)=2 and cj=3 的数据,也就是说要这样

select sum(day)as 日总数,sum(week)as 周总数 from t_zs where sj between '2012-02-08' and '2012-02-08' and cj=2 and cj=3

但这样是错误的没有结果,如果换成or更是不对,

问题是这样:一共有10个车间,也就是说cj有十个数(1,2,3,4,5,6,7,8,9,10)

这是个多选的,所以cj是不固定的,有可能是cj=2 and cj=4 ,又或者全部都选上

现在问题就是想如何在
select sum(day)as 日总数,sum(week)as 周总数 from t_zs where sj between '2012-02-08' and '2012-02-08'

这条语句上加上后面多选 cj的 条件,得出所选车间正确之和

[解决办法]
cj in (2,3)
[解决办法]

SQL code
select sum(day)as 日总数,sum(week)as 周总数 from t_zs where sj between '2012-02-08' and '2012-02-08' and cj in (2,3) 

热点排行