太懒了。。。上网求答案。。。

phh1989 2009-09-14 03:08:38
--序号-------学号----------状态--------
1 1 1
2 1 1
3 1 0
4 1 0
5 1 0
6 2 1
7 2 0
8 2 0
9 1 1
10 2 0
11 1 1
12 1 1
13 1 0
14 2 1
想要获的效果
-----学号----------次数-----------
1 5
2 2
说明一下,次数是1出现的次数
求解谢谢~
...全文
130 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihan6415151528 2009-09-14
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 phh1989 的回复:]
看不懂14楼的。。。。
[/Quote]

...你就是14啊!
lihan6415151528 2009-09-14
  • 打赏
  • 举报
回复

select 学号,次数=sum(case 状态 when 1 else 0 end)
from tb
group by 学号
phh1989 2009-09-14
  • 打赏
  • 举报
回复
看不懂14楼的。。。。
soft_wsx 2009-09-14
  • 打赏
  • 举报
回复
select 学号,COUNT(1) 次数 from tb where 状态=1 group by 学号 
SELECT 学号,sum(case when 状态=1 then 1 else 0 end) from tb group by 学号
/*
1 5
2 2
*/
petunsecn 2009-09-14
  • 打赏
  • 举报
回复
select StuID, count(*) as Frequency from Table where State=1 group by StuID
xiequan2 2009-09-14
  • 打赏
  • 举报
回复
[Quote=引用楼主 phh1989 的回复:]
--序号-------学号----------状态--------
    1        1            1
    2        1            1
    3        1            0
    4        1            0
    5        1            0
    6        2            1
    7        2            0
    8        2            0
    9        1            1
  10        2            0
  11        1            1
  12        1            1
  13        1            0
  14        2            1
想要获的效果
-----学号----------次数-----------
      1            5
      2            2
说明一下,次数是1出现的次数
求解谢谢~
[/Quote]

if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([序号] int,[学号] int,[状态] int)
insert [tb]
select 1,1,1 union all
select 2,1,1 union all
select 3,1,0 union all
select 4,1,0 union all
select 5,1,0 union all
select 6,2,1 union all
select 7,2,0 union all
select 8,2,0 union all
select 9,1,1 union all
select 10,2,0 union all
select 11,1,1 union all
select 12,1,1 union all
select 13,1,0 union all
select 14,2,1
--------------开始查询--------------------------
select
学号,
次数=(select count([状态]) from tb where 学号=t.学号 and [状态]=1)
from
tb t
group by
学号
/*
学号 次数
----------- -----------
1 5
2 2

(2 行受影响)


*
/
guguda2008 2009-09-14
  • 打赏
  • 举报
回复
少壮不努力,老大程序员
zjybushiren88888 2009-09-14
  • 打赏
  • 举报
回复
select 学号,次数=sum(case 状态  when 1 then 1 else 0 end)
from tb
group by 学号
xuejiecn 2009-09-14
  • 打赏
  • 举报
回复

select
学号, 次数=sum(case 状态 when 1 else 0 end)
from tb group by 学号 order by 学号

ws_hgo 2009-09-14
  • 打赏
  • 举报
回复
declare @TB table([序号] int,[学号] int,[状态] int)
insert @TB
select 1,1,1 union all
select 2,1,1 union all
select 3,1,0 union all
select 4,1,0 union all
select 5,1,0 union all
select 6,2,1 union all
select 7,2,0 union all
select 8,2,0 union all
select 9,1,1 union all
select 10,2,0 union all
select 11,1,1 union all
select 12,1,1 union all
select 13,1,0 union all
select 14,2,1

select [学号],sum([状态]) [状态] from @TB group by [学号]

学号 状态
----------- -----------
1 5
2 2

(2 行受影响)
ws_hgo 2009-09-14
  • 打赏
  • 举报
回复
select 学号,sum(次数) 次数 from tb group by 学号
ws_hgo 2009-09-14
  • 打赏
  • 举报
回复
select 学号,sum(次数) 次数
from
tb
虫洞 2009-09-14
  • 打赏
  • 举报
回复

select 学号,次数=sum(case 状态 when 1 else 0 end)
from tb
group by 学号
--小F-- 2009-09-14
  • 打赏
  • 举报
回复
---不好意思  1,2楼都有点问题
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-09-14 15:11:12
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([序号] int,[学号] int,[状态] int)
insert [tb]
select 1,1,1 union all
select 2,1,1 union all
select 3,1,0 union all
select 4,1,0 union all
select 5,1,0 union all
select 6,2,1 union all
select 7,2,0 union all
select 8,2,0 union all
select 9,1,1 union all
select 10,2,0 union all
select 11,1,1 union all
select 12,1,1 union all
select 13,1,0 union all
select 14,2,1
--------------开始查询--------------------------
select
学号,
次数=sum(case 状态 when 1 then 1 else 0 end)
from
tb
group by
学号
----------------结果----------------------------
/* 学号 次数
----------- -----------
1 5
2 2

(2 行受影响)
*/
--小F-- 2009-09-14
  • 打赏
  • 举报
回复
select 
学号,
次数=sum(case 状态 when 1 else 0 end)
from
tb
group by
学号
--小F-- 2009-09-14
  • 打赏
  • 举报
回复
select 
学号,
次数=sum(case 状态 when 1 else 0 end)
from
tb

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧