请教一下,如何写做下面较复杂的统计汇总SQL语句?

opqhjb_2002 2007-04-28 02:21:13
我有一表Table1 字段名为: A B C D E
字段值为: t 1 0 0 1
h 0 1 0 1
t 0 0 1 0
h 1 0 0 1
h 1 1 1 0
... ... ... ... ...

我想要的结果为: A字段的记录条数 B字段值为1的条数 C字段值为0的条数
t 2 1 2
h 3 2 1
... ... ... ...
用一条SQL语句统计.
请高手帮帮忙.小弟对SQL语言掌握不好.谢谢了!
...全文
267 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zrz2008 2007-04-28
  • 打赏
  • 举报
回复
一种简单可行的办法,我已经测试过了,保证没有问题

select a='t',b=1,c=0,d=0,e=1 into #table
insert into #table values('h',0,1,0,1)
insert into #table values('t',0,0,1,0)
insert into #table values('h',1,0,0,0)
insert into #table values('h',1,1,1,1)

select a,count(a) a_count,sum(b) b_1,count(c)-sum(c) c_0 from #table group by a

drop table #table
a a_count b_1 c_0
----------------------------
t 2 1 2
h 3 2 1

(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 2 行)
opqhjb_2002 2007-04-28
  • 打赏
  • 举报
回复
viptiger(六嘎)
你的回答没有加条件,我试了不行.
rookie_one(流氓会武术,谁都挡不住)
没有达到我想要的结果.
我的结果是对 记录值为 : t和h的做统计, 我所要的是:
t的名称(不重复),t的所有记录条数, t的B字段值为1的记录条数,t的C字段值为0记录条数
h名称(不重复),h所有记录条数, hB字段值为1的记录条数,hC字段值为0记录条数
fcuandy 2007-04-28
  • 打赏
  • 举报
回复
请搜索 "行转列"
ojuju10 2007-04-28
  • 打赏
  • 举报
回复
create table #(a varchar(5),b int,c int ,d int,e int)
insert into # select 't' , 1, 0, 0, 1
insert into # select 'h', 0, 1, 0, 1
insert into # select 't', 0, 0, 1, 0
insert into # select 'h', 1, 0, 0, 1
insert into # select 'h', 1, 1, 1, 0

select a, count(a) A字段的记录条数, sum(case when b=1 then 1 else 0 end) B字段值为1的条数 ,sum(case when c=0 then 1 else 0 end) C字段值为0的条数 from #
group by a
order by a desc

a A字段的记录条数 B字段值为1的条数 C字段值为0的条数
----- ----------- ----------- -----------
t 2 1 2
h 3 2 1
ojuju10 2007-04-28
  • 打赏
  • 举报
回复
create table #(a varchar(5),b int,c int ,d int,e int)
insert into # select 't' , 1, 0, 0, 1
insert into # select 'h', 0, 1, 0, 1
insert into # select 't', 0, 0, 1, 0
insert into # select 'h', 1, 0, 0, 1
insert into # select 'h', 1, 1, 1, 0

select a, count(a), sum(case when b=1 then 1 else 0 end) ,sum(case when c=1 then 1 else 0 end) from #
group by a
hrb2008 2007-04-28
  • 打赏
  • 举报
回复
select tba.a,
A字段的记录条数=(select count* from Table1 where a=tba.a),
B字段值为1的条数=(select count* from Table1 where a=tba.a and b=1),
C字段值为0的条数=(select count* from Table1 where a=tba.a and c=0)
from Table1 as tba group by tba.a
rookie_one 2007-04-28
  • 打赏
  • 举报
回复
create table #temp
(A varchar(50),
B varchar(50),
C varchar(50)
)
insert into #temp
select 't','1','0' union all select 'h','0','1' union all select 't','0','0' union all select 'h','1','0' union all select 'h','1','1'
select * from #temp

select t.a,
(select count(*) from #temp where a=t.a) A字段的记录条数,
(select count(*) from #temp where a=t.a and b=1) B字段值为1的条数,
(select count(*) from #temp where a=t.a and c=0) C字段值为0的条数
from #temp t
------------
t 2 1 2
h 3 2 1
t 2 1 2
h 3 2 1
h 3 2 1
viptiger 2007-04-28
  • 打赏
  • 举报
回复
declare @Table1 table(A char(1),B int,C int,D int,E int)
insert @Table1
select 't',1,0,0,1
union all
select 'h',0,1,0,1
union all
select 't',0,0,1,0
union all
select 'h',1,0,0,1
union all
select 'h',1,1,1,0

select count(*) a,sum(b) b,sum(c-1)*-1 c from @Table1
group by a

-----------------------------------------------


(所影响的行数为 5 行)

a b c
----------- ----------- -----------
3 2 1
2 1 2

(所影响的行数为 2 行)
hrb2008 2007-04-28
  • 打赏
  • 举报
回复

select tba.a,
A字段的记录条数=(select count* from Table1 where a=tba.a),
B字段值为1的条数=(select count* from Table1 where a=tba.a and b=1),
C字段值为0的条数=(select count* from Table1 where a=tba.a and c=0)
from Table1 as tba

22,210

社区成员

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

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