22,294
社区成员
发帖
与我相关
我的任务
分享
if object_id('[A]') is not null drop table [A]
go
create table [A]([numbm] int,[date] int,[mark] int)
insert [A]
select 2,2013,5 union all
select 2,2013,7 union all
select 2,2013,5 union all
select 3,2013,5 union all
select 3,2013,7 union all
select 3,2013,5 union all
select 3,2013,6
if object_id('[B]') is not null drop table [B]
go
create table [B]([numbm] int,[bmmc] varchar(8))
insert [B]
select 2,'人事部门' union all
select 3,'后勤部门'
go
declare @sql varchar(8000)
set @sql = ''
select @sql = @sql + ',count(case when bmmc='''+b.bmmc+''' then 1 else null end) as ['+
b.bmmc+']'
from A
inner join B
on a.numbm = b.numbm
group by b.bmmc
select @sql = 'select '+ stuff(@sql,1,1,'') +
' from A
inner join B
on a.numbm = b.numbm
where a.date=2013 and a.mark in (5,7)'
exec(@sql)
/*
后勤部门 人事部门
3 3
*/
if object_id('[A]') is not null drop table [A]
go
create table [A]([numbm] int,[date] int,[mark] int)
insert [A]
select 2,2013,5 union all
select 2,2013,7 union all
select 2,2013,5 union all
select 3,2013,5 union all
select 3,2013,7 union all
select 3,2013,5 union all
select 3,2013,6
if object_id('[B]') is not null drop table [B]
go
create table [B]([numbm] int,[bmmc] varchar(8))
insert [B]
select 2,'人事部门' union all
select 3,'后勤部门'
go
declare @sql varchar(8000)
set @sql = ''
select @sql = @sql + ',count(case when bmmc='''+b.bmmc+''' then 1 else null end) as ['+
b.bmmc+']'
from A
inner join B
on a.numbm = b.numbm
group by b.bmmc
select @sql = 'select '+ stuff(@sql,1,1,'') +
' from A
inner join B
on a.numbm = b.numbm'
exec(@sql)
/*
后勤部门 人事部门
4 3
*/----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2014-01-16 10:35:20
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([numbm] int,[date] int,[mark] int)
insert [A]
select 2,2013,5 union all
select 2,2013,7 union all
select 2,2013,5 union all
select 3,2013,5 union all
select 3,2013,7 union all
select 3,2013,5 union all
select 3,2013,6
--> 测试数据:[B]
if object_id('[B]') is not null drop table [B]
go
create table [B]([numbm] int,[bmmc] varchar(8))
insert [B]
select 2,'人事部门' union all
select 3,'后勤部门'
--------------开始查询--------------------------
IF OBJECT_ID('tempdb..#t','u')IS NOT NULL
DROP TABLE #t
select bmmc,COUNT(CASE WHEN bmmc=bmmc THEN 1 ELSE NULL END )[countNumb] INTO #t
from [A] LEFT JOIN [b] ON a.[numbm]=b.[numbm]
where [date]=2013 and mark IN (5,7)
GROUP BY bmmc
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename(bmmc)+'=max(case when [bmmc]='+quotename(bmmc,'''')+' then [countNumb] else 0 end)'
from #t group by bmmc
SET @s=SUBSTRING(@s,2,LEN(@s))
exec('select '+@s+' from #t ')
----------------结果----------------------------
/*
后勤部门 人事部门
----------- -----------
3 3
*/