以功能怎么现实
有以下格式:
人名行号列1列2 列3
张三11223123
张三2223224
张三3323325
张三4423426
张三53323527
李四13423628
李四22223729
李四33323830
李四44423931
李四53824032
王五13924133
王五222224234
王五333324335
王五444424436
王五54312337
想只用一个select 语句 把 每人的第2、3、4行中第1列的数据取出来相加 可以现实吗?
最后的结果 就是 张三,9(2+3+4)
李四,99(22+33+44)
王五,999(222+333+444)
[解决办法]
- SQL code
create table t1(人名 varchar(10),行号 int,列1 int,列2 int,列3 int)insert t1select '张三', 1, 12, 231, 23 union allselect '张三', 2, 2, 232, 24 union allselect '张三', 3, 3, 233, 25 union allselect '张三', 4, 4, 234, 26 union allselect '张三', 5, 33, 235, 27 union allselect '李四', 1, 34, 236, 28 union allselect '李四', 2, 22, 237, 29 union allselect '李四', 3, 33, 238, 30 union allselect '李四', 4, 44, 239, 31 union allselect '李四', 5, 38, 240, 32 union allselect '王五', 1, 39, 241, 33 union allselect '王五', 2, 222,242, 34 union allselect '王五', 3, 333,243, 35 union allselect '王五', 4, 444,244, 36 union allselect '王五', 5, 43, 123, 37goselect 人名,列=sum(列1) from t1where 行号 in (2,3,4)group by 人名/*人名 列---- ---李四 99王五 999张三 9*/godrop table t1