你好,按照你问题是需要按year分组,并且组内按照计算code开头为1的quantity之和以及code开头为2的quantity之和的数据。首先分析,既然需要按照year分组,那么就需要用到group by 那么 就可以写出如下:select year , xx, xx from 表名 group by year如何计算组内code开头为1的quantity之和以及code开头为2的quantity之和的数据,那么需要用到sum,并且分别刷选出code开头为1和code开头为2的数据即可。完整SQL如下:select year, sum (case when quantitiy like '%1' then quantitiy else 0 end as ) as 开头为1的quantity之和为一列, sum (case when quantitiy like '%2' then quantitiy else 0 end as ) as 开头为2的quantity之和为一列 from 表名 group by year参考技术Aselect sum(quantity)from table where code like “1%” select sum(quantity)from table where code like “2%”
现在用sql语句查出来字段里包含某个字符串的所有记录怎么查
一般用substring函数截取出特定字段,如 --用户姓名第二个字是'大'字的 select * from Users where SUBSTRING(UserName,2,1)='大' 第二种方法就是直接like模糊查询,看具体情况使用 select * from Users where UserName like '%大%'参考技术Aselect * from 表 where 字段 like '%字符串%'参考技术Bcharindex() != 0