当前位置: 代码迷 >> Sql Server >> 请问一个SQL语句(,马上就结)
  详细解决方案

请问一个SQL语句(,马上就结)

热度:16   发布时间:2016-04-27 20:15:00.0
请教一个SQL语句(在线等,马上就结)
表结构如下
id       pid     N1       N2
1           1         10       20
1           2         10       10
1           3         10       10
1           ...     ...     ...
2           1         5         5
2           2         5         5
2           3         5         5
2           ...     ...     ...  
现在想得到如下查询结果,查询条件是查询pid从1到3
id         N1       N2
1           30       40
2           15       15
这个语句该怎么写


------解决方案--------------------
select id,sum(N1) as N1,sum(N2) as N2
from 表
where pid between 1 and 3
group by id
------解决方案--------------------
select pid,sum(N1) as N1,sum(N2) as N2 from table1 group by pid
------解决方案--------------------
--TRY
SELECT id,sum(N1),SUM(N2) FROM tablename where pid in ( '1 ', '2 ', '3 ') group by id
  相关解决方案