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

根据字段查询表时,将另一字段部分数据加-号,怎么处理呢

2012-04-04 
根据字段查询表时,将另一字段部分数据加-号,怎么办呢?point表POINTTYPE314221411231我想查询时,把type2

根据字段查询表时,将另一字段部分数据加-号,怎么办呢?
point表

POINT TYPE  
 3 1
 4 2
 2 1
 4 1
 1 2
 3 1

我想查询时,把type=2 的字段对应的point都加个负号。我想对point求和sum(point)
显示这样的结果
POINT TYPE
3 1
-4 2
2 1
4 1
-1 2
3 1
要怎么写呢?如有解答,不胜感激!

[解决办法]

SQL code
--> 测试数据:[point]if object_id('[point]') is not null drop table [point]create table [point]([POINT] int,[TYPE] int)insert [point]select 3,1 union allselect 4,2 union allselect 2,1 union allselect 4,1 union allselect 1,2 union allselect 3,1select case when [type]=2 then -[POINT] else [POINT] end as [POINT],[TYPE] from [point]/*POINT    TYPE3    1-4    22    14    1-1    23    1*/--求和:select [TYPE],sum(case when [type]=2 then -[POINT] else [POINT] end) as [POINT] from [point] group by [TYPE]  /* TYPE    POINT1    122    -5 */
[解决办法]
SQL code
--求和:select sum(case when [type]=2 then -[POINT] else [POINT] end) as [POINT] from [point]  /*POINT7*/ 

热点排行