读书人

急求高手!SQL 同表中两条记录数据相加

发布时间: 2012-03-26 15:46:56 作者: rapoo

急求高手!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) 

读书人网 >SQL Server

热点推荐