34,590
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE [msgToShi] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[hAdmin] [int] NOT NULL ,
[hTg] [bit] NOT NULL CONSTRAINT [DF_msgToShi_hTg] DEFAULT (0),
[hBslb] [int] NOT NULL CONSTRAINT [DF_msgToShi_hBslb] DEFAULT (0),
[hYd] [bit] NOT NULL CONSTRAINT [DF_msgToShi_hYd] DEFAULT (0),
CONSTRAINT [PK_msgToShi] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
) ON [PRIMARY]
GO
ID hAdmin hTg hBslb hYd
----------- ----------- ---- ----------- ----
1 2 0 1 0
2 2 0 0 0
3 2 1 1 0
3 2 1 2 0
4 3 0 0 0
报送类别 通过 未通过
新增 0 1
变更 1 1
终止 1 0
if not object_id('msgToShi') is null
drop table msgToShi
Go
Create table msgToShi([ID] int,[hAdmin] int,[hTg] int,[hBslb] int,[hYd] int)
Insert msgToShi
select 1,2,0,1,0 union all
select 2,2,0,0,0 union all
select 3,2,1,1,0 union all
select 3,2,1,2,0 union all
select 4,3,0,0,0
Go
select
case hBslb when 0 then '新增'
when 1 then '变更'
when 2 then '终止'
end as 报送类别,
[0] as '未通过',
[1] as '通过'
from
(Select [ID],[hBslb], [hTg] from msgToShi where hAdmin=2 )a
pivot
(count([ID]) for [hTg] in([0],[1]))b
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (ID int,hAdmin int,hTg int,hBslb int,hYd int)
insert into #T
select 1,2,0,1,0 union all
select 2,2,0,0,0 union all
select 3,2,1,1,0 union all
select 3,2,1,2,0 union all
select 4,3,0,0,0
select case when hbslb=0 then '新增'
when hbslb=1 then '更改'
else '终止' end as 报送类型
,max(case when htg=1 then 1 else 0 end) as '通过',
max(case when htg=0 then 1 else 0 end) as '未通过'
from #t
group by hbslb
报送类型 通过 未通过
---- ----------- -----------
新增 0 1
更改 1 1
终止 1 0
(3 行受影响)
select
报送类别,
sum(case hTg when 1 then 1 else 0 end) as 通过,
sum(case hTg when 1 then 0 else 1 end) as 未通过
from
(select case hBslb when 0 then '新增' when 1 then '变更' when 2 then '终止' end as 报送类别,* from msgToShi) t
where
hAdmin=2 and hYd=0
group by
报送类别
select
case hBslb when 0 then '新增'
when 1 then '变更'
when 2 then '终止'
end as 报送类别,
isnull(sum(case htg when 1 then 1 end),0) [通过],
isnull(sum(case htg when 0 then 1 end),0) [未通过]
from [msgToShi] as a where hadmin='2' and hYd='0'
group by hBslb order by hBslb
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (ID int,hAdmin int,hTg int,hBslb int,hYd int)
insert into #T
select 1,2,0,1,0 union all
select 2,2,0,0,0 union all
select 3,2,1,1,0 union all
select 3,2,1,2,0 union all
select 4,3,0,0,0
select case hBslb when 0 then '新增'
when 1 then '变更'
when 2 then '终止'
end as 报送类别,
count(case htg when 1 then 1 end) [通过],
count(case htg when 0 then 1 end) [未通过]
from #T
where hadmin=2
group by hBslb
/*
报送类别 通过 未通过
---- ----------- -----------
新增 0 1
变更 1 1
终止 1 0
*/
select
报送类别,
sum(hTg ) as 通过,
sum(1-hTg) as 未通过
from
(select case hBslb when 0 then '新增' when 1 then '变更' when 2 then '终止' end as 报送类别,* from msgToShi) t
where
hAdmin=2 and hYd=0
group by
报送类别
select case bhslb when 0 then '新增'
when 1 then '变更'
when 2 then '终止'
end as 报送类别,
count(case htg when 1 then 1 end) [通过],
count(case htg when 0 then 1 end) [未通过]
from tb
where hadmin=2
group by bhslb