统计连续字段的个数

问题要求
declare @t table(no int)
insert into @t
select 2
union all select 2
union all select 2
union all select 1
union all select 2
union all select 4
union all select 4
union all select 1
union all select 1
想要的答案是 计算出连续的一样no的个数
count_no
3
1
1
2
2

方法:

declare @t table(no int)
insert into @t
select 2
union all select 2
union all select 2
union all select 1
union all select 2
union all select 4
union all select 4
union all select 1
union all select 1

declare @tb table(no1 int)

declare @a varchar(8000),@b int,@c varchar(2)
set @b=1
set @a=”
set @c=”
select @a=@a+’,'+cast(no as varchar(20)) from @t
while(len(@a)>0)
begin
if (@c=”)
begin
set @c=substring(@a,2,1)
set @a=stuff(@a,1,2,”)
end
else
begin
if(@c=substring(@a,2,1))
begin
set @a=stuff(@a,1,2,”)
set @b=@b+1
end
else
begin
set @c=”
insert into @tb select @b
set @b=1
end
end
end
insert into @tb select @b
select * from @tb

no1
———–
3
1
1
2
2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Java 8中,可以使用流(stream)和集合框架(Collection framework)来统计某个字段个数。 假设我们有一个包含多个对象的集合,每个对象都有一个表示某个字段的属性。我们可以使用流的filter()方法来筛选出符合条件的对象,然后使用count()方法来统计符合条件的对象的个数。 例如,假设我们有一个Person类,其中有一个表示性别的属性gender。我们可以统计所有性别为男性的人数,代码如下: List<Person> personList = new ArrayList<>(); // 假设有多个Person对象添加到personList中 long malesCount = personList.stream() .filter(person -> person.getGender().equals("男性")) .count(); 上述代码首先将personList转换为流,然后使用filter()方法来筛选出gender属性为"男性"的对象,最后使用count()方法来统计符合条件的对象的个数,并将结果赋值给变量malesCount。 需要注意的是,代码中的filter()方法需要传入一个Lambda表达式,该表达式用来定义筛选条件。在上述例子中,我们使用Lambda表达式来比较Person对象中的gender属性是否等于"男性"。 最后,我们可以使用malesCount变量来获取统计结果,它表示性别为男性的Person对象的个数。 ### 回答2: 在Java8中,我们可以使用流(Stream)和lambda表达式来方便地统计某个字段个数。 首先,我们需要先创建一个包含对象的集合或数组。假设我们有一个包含多个学生对象的List集合,每个学生对象都有一个字段表示性别,我们要统计男生的个数。 我们可以使用stream()方法将List集合转换成一个流,然后使用filter()方法过滤出性别为男的学生对象,最后使用count()方法统计符合条件的学生个数。 示例代码如下: ``` import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<Student> students = Arrays.asList( new Student("Tom", 18, "男"), new Student("Alice", 20, "女"), new Student("Bob", 19, "男"), new Student("David", 22, "男"), new Student("Linda", 21, "女") ); long maleCount = students.stream() .filter(student -> student.getGender().equals("男")) .count(); System.out.println("男生的个数为:" + maleCount); } } class Student { private String name; private int age; private String gender; public Student(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public String getGender() { return gender; } } ``` 以上代码首先创建了一个包含5个学生对象的List集合,然后使用stream()方法将其转换成一个流。接着使用filter()方法过滤出性别为男的学生对象,最后使用count()方法统计符合条件的学生个数,并将结果打印输出。 运行结果为: ``` 男生的个数为:3 ``` 这样我们就成功统计了指定字段个数,使用流和lambda表达式可以使代码更简洁和易读。 ### 回答3: 在Java 8中,可以使用Stream API来统计某个字段个数。首先,需要将数据转换为一个Stream对象,然后使用filter方法筛选符合条件的元素,最后使用count方法统计符合条件的元素个数。 假设有一个Student类,其中包含name和age两个字段。我们希望统计年龄为18岁的学生人数。下面是一个示例代码: ```java import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { // 创建一个包含学生信息的列表 List<Student> students = new ArrayList<>(); students.add(new Student("Alice", 18)); students.add(new Student("Bob", 20)); students.add(new Student("Charlie", 18)); students.add(new Student("David", 22)); students.add(new Student("Eve", 18)); // 统计年龄为18岁的学生人数 long count = students.stream() .filter(student -> student.getAge() == 18) .count(); System.out.println("年龄为18岁的学生人数为: " + count); } static class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public int getAge() { return age; } } } ``` 运行代码,输出结果为: 年龄为18岁的学生人数为: 3 通过Stream的filter方法筛选出年龄为18岁的学生,再使用count方法统计符合条件的学生人数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值