Name PartyBranch TypeNo a 第一支部 正式党员
b 第一支部 积极分子
c 第一支部 正式党员
d 第一支部 正式党员
e 第一支部 正式党员
f 第二支部 正式党员
g 第二支部 正式党员
h 第二支部 积极分子
i 第二支部 正式党员 PartyBranch是党支部的意思
TypeNo是党员类型
GloryNo是否优秀
现在需要统计 各支部人数 和 各支部正式党员数
要怎么写sql语句,请各位大虾帮忙~~~~
b 第一支部 积极分子
c 第一支部 正式党员
d 第一支部 正式党员
e 第一支部 正式党员
f 第二支部 正式党员
g 第二支部 正式党员
h 第二支部 积极分子
i 第二支部 正式党员 PartyBranch是党支部的意思
TypeNo是党员类型
GloryNo是否优秀
现在需要统计 各支部人数 和 各支部正式党员数
要怎么写sql语句,请各位大虾帮忙~~~~
PartyBranch,
count(*) as 支部人数,
sum(case TypeNo when '正式党员' then 1 else 0 end) as 正式党员数
from
tname
group by
PartyBranch
declare @tempsql varchar(8000)
set @tempsql = ''
select @tempsql = @tempsql + '[' + zymc + '] = (select count(*) from table1 b where b.name = a.name and b.zymc = ''' + zymc + '''),' from table1 group by zymc
set @tempsql = left(@tempsql,len(@tempsql)-1)
print @tempsql
-------------------------
set @tempsql = 'select a.name ,' + @tempsql
+ ' from table1 a'
+ ' group by a.name'print @tempsqlexec(@tempsql)修改下应该就可以解决你的问题
--> 测试数据:@table
declare @table table([Name] varchar(1),[PartyBranch] varchar(8),[TypeNo] varchar(8))
insert @table
select 'a','第一支部','正式党员' union all
select 'b','第一支部','积极分子' union all
select 'c','第一支部','正式党员' union all
select 'd','第一支部','正式党员' union all
select 'e','第一支部','正式党员' union all
select 'f','第二支部','正式党员' union all
select 'g','第二支部','正式党员' union all
select 'h','第二支部','积极分子' union all
select 'i','第二支部','正式党员'select PartyBranch,count(1) as 人数,
sum(case TypeNo when '正式党员' then 1 else 0 end) as 正式党员数
from @table
group by PartyBranch
--结果
----------------------------------
第二支部 4 3
第一支部 5 4
PartyBranch nvarchar(10),
TypeNo nvarchar(10))select * from caidanielinsert caidaniel select 'a','第一支部','正式党员'
union all
select 'b','第一支部','积极分子'
union all
select 'c','第一支部','正式党员'
union all
select 'd','第一支部','正式党员'
union all
select 'e','第一支部','正式党员'
union all
select 'f','第二支部','正式党员'
union all
select 'g','第二支部','正式党员'
union all
select 'h','第二支部','积极分子'
union all
select 'i','第二支部','正式党员'Select Partybranch,count(*) from caidaniel group by Partybranch
Select Partybranch,count(*) from caidaniel
where typeno='正式党员'
group by Partybranch
declare @table table([Name] varchar(1),[PartyBranch] varchar(8),[TypeNo] varchar(8))
insert @table
select 'a','第一支部','正式党员' union all
select 'b','第一支部','积极分子' union all
select 'c','第一支部','正式党员' union all
select 'd','第一支部','正式党员' union all
select 'e','第一支部',null union all
select 'f','第二支部', null union all
select 'g','第二支部','正式党员' union all
select 'h','第二支部','积极分子' union all
select 'i','第二支部','正式党员';select PartyBranch 支部, COUNT(1) 人数, SUM(case when TypeNo = '正式党员' then 1 else 0 end)
正式党员数 from @table group by PartyBranch order by PartyBranch desc--结果
支部 人数 正式党员数
-------- ----------- -----------
第一支部 5 3
第二支部 4 2
if exists (select 1 from sysobjects where name = 'tb' and xtype = 'u')
drop table tb
create table tb([Name] varchar(1),[PartyBranch] varchar(8),[TypeNo] varchar(8))
insert into tb
select 'a','第一支部','正式党员' union all
select 'b','第一支部','积极分子' union all
select 'c','第一支部','正式党员' union all
select 'd','第一支部','正式党员' union all
select 'e','第一支部','正式党员' union all
select 'f','第二支部', '正式党员' union all
select 'g','第二支部','正式党员' union all
select 'h','第二支部','积极分子' union all
select 'i','第二支部','正式党员';declare @sql nvarchar(2000)
select @sql = ISNULL(@sql +', ','select PartyBranch 支部, COUNT(1) 人数, ')
+'SUM(case when TypeNo = '''+TypeNo+''' then 1 else 0 end) '+TypeNo+' '
from (select distinct TypeNo from tb) texec (@sql + ' from tb group by PartyBranch')--结果
(9 row(s) affected)
支部 人数 积极分子 正式党员
-------- ----------- ----------- -----------
第二支部 4 1 3
第一支部 5 1 4(2 row(s) affected)
GO
Create table tb([Name] varchar(1),[PartyBranch] varchar(8),[TypeNo] varchar(8))
GO
insert tb
select 'a','第一支部','正式党员' union all
select 'b','第一支部','积极分子' union all
select 'c','第一支部','正式党员' union all
select 'd','第一支部','正式党员' union all
select 'e','第一支部','正式党员' union all
select 'f','第二支部','正式党员' union all
select 'g','第二支部','正式党员' union all
select 'h','第二支部','积极分子' union all
select 'i','第二支部','正式党员'
GO
select
PartyBranch,
count(TypeNo) as 人数,
sum(case TypeNo when '正式党员' then 1 else 0 end )as 正式党员
from tb
group by PartyBranch
order by PartyBranch desc