22,207
社区成员
发帖
与我相关
我的任务
分享
教师保险表
CREATE TABLE A(
[techinsid] [int] IDENTITY(1,1) NOT NULL,
[techID] [int] NULL,
[setdate] [nvarchar](6) COLLATE Chinese_PRC_CI_AS NULL,
[insurances] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NULL,----B表中的insuranceID,多个用逗号隔开
[comppay] [money] NULL
) ON [PRIMARY]
Insert A
select N'1',N'201101',N'1,2',N'2011' union all
select ,N'1',N'201102'N'1,2,3,4',N'2011' union all
select N'1',N'201103',N'1,4',N'2011' union all
select N'1',N'201104',N'3,4',N'2011'union all
Go
保险类别表
CREATE TABLE B(
[insuranceID] [int] IDENTITY(1,1) NOT NULL,
[insurancename] [nvarchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
Insert B
select N'养老保险' union all
select ,N'医疗保险' union all
select N'失业保险' union all
select N'工伤保险'union all
GO
我想得到的效果是
techinsid setdate 养老保险 医疗保险 失业保险 工伤保险 comppay
1 2011 1 1 0 0 2011
2 2011 1 1 1 1 2011
3 2011 1 0 0 1 2011
4 2011 0 0 1 1 2011
1表示该月此保险已交,0表示该月此保险未交
create table a(
[techinsid] [int] identity(1,1) not null,
[techid] [int] null,
[setdate] [nvarchar](4) collate chinese_prc_ci_as null,
[insurances] [nvarchar](20) collate chinese_prc_ci_as null,----b表中的insuranceid,多个用逗号隔开
[comppay] [money] null
) on [primary]
insert a
select N'1',N'2011',N'1,2',N'2011' union all
select N'1',N'2011',N'1,2,3,4',N'2011' union all
select N'1',N'2011',N'1,4',N'2011' union all
select N'1',N'2011',N'3,4',N'2011'
go
create table b(
[insuranceid] [int] identity(1,1) not null,
[insurancename] [nvarchar](10) collate sql_latin1_general_cp1_ci_as null
) on [primary]
insert b
select N'养老保险' union all
select N'医疗保险' union all
select N'失业保险' union all
select N'工伤保险'
go
declare @sql varchar(8000)
set @sql = 'select a.techinsid,a.setdate'
select @sql = @sql + ',sum(case b.insuranceid when ' + ltrim(insuranceid) + ' then 1 else 0 end)[' + insurancename + ']'
from b
select @sql = @sql + ',a.comppay from a left join b on charindex(ltrim(b.insuranceid),a.insurances) > 0 group by a.techinsid,a.setdate,a.comppay'
exec(@sql)
drop table a,b
/*
techinsid setdate 养老保险 医疗保险 失业保险 工伤保险 comppay
----------- ------- ----------- ----------- ----------- ----------- ---------------------
1 2011 1 1 0 0 2011.00
2 2011 1 1 1 1 2011.00
3 2011 1 0 0 1 2011.00
4 2011 0 0 1 1 2011.00
(4 行受影响)
CREATE TABLE A(
[techinsid] [int] IDENTITY(1,1) NOT NULL,
[techID] [int] NULL,
[setdate] [nvarchar](6) COLLATE Chinese_PRC_CI_AS NULL,
[insurances] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NULL,----B表中的insuranceID,多个用逗号隔开
[comppay] [money] NULL
) ON [PRIMARY]
Insert A
select N'1',N'201101',N'1,2',N'2011' union all
select N'1',N'201102',N'1,2,3,4',N'2011' union all
select N'1',N'201103',N'1,4',N'2011' union all
select N'1',N'201104',N'3,4',N'2011'
Go
--保险类别表
CREATE TABLE B(
[insuranceID] [int] IDENTITY(1,1) NOT NULL,
[insurancename] [nvarchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
Insert B
select N'养老保险' union all
select N'医疗保险' union all
select N'失业保险' union all
select N'工伤保险'
GO
SELECT A.techinsid,A.techID,A.setdate,
MAX(CASE WHEN B.insurancename='养老保险' THEN 1 ELSE 0 END) AS 养老保险,
MAX(CASE WHEN B.insurancename='医疗保险' THEN 1 ELSE 0 END) AS 医疗保险,
MAX(CASE WHEN B.insurancename='失业保险' THEN 1 ELSE 0 END) AS 失业保险,
MAX(CASE WHEN B.insurancename='工伤保险' THEN 1 ELSE 0 END) AS 工伤保险
FROM A,B
WHERE CHARINDEX(','+RTRIM(B.insuranceID)+',',','+A.insurances+',')>0
GROUP BY A.techinsid,A.techID,A.setdate,A.comppay
GO
DROP TABLE A,B