这是一篇很长的文章
:) 1- 选择不带 JoinedDate 和 leftDate 的不同列
2- 使用其他列创建唯一列(Id
列)
select
distinct [clubID],[personID],[Sex],[Birth Year],[Birth Month],
([clubID] +'-'+ convert(nvarchar,[personID]) +'-'+ convert(nvarchar,[Sex])+'-'+
convert(nvarchar,[Birth Year]) + '-' +convert(nvarchar,[Birth Month])) as Id
into #dt
from tbRegInfo
3-为选择 JoinedDate 创建其他临时表
SELECT b.JoinedDate, a.Id, COUNT(*) AS Count
into #dt2
FROM dbo.#dt AS a INNER JOIN
dbo.tbRegInfo AS b ON
(b.[clubID] +'-'+ convert(nvarchar,b.[personID]) +'-'+
convert(nvarchar,b.[Sex])+'-'+ convert(nvarchar,b.[Birth Year])
+ '-' +convert(nvarchar,b.[Birth Month]))=a.Id
GROUP BY b.JoinedDate, a.Id
ORDER BY Count
4-为 JoinedDate 添加列。它将为最大重复加入日期添加列。我的意思是如果一个人 5 次加入并离开。它会添加
JoinedDate,JoinedDate1,JoinedDate2,JoinedDate... JoinedDate5
declare @Id nvarchar(250) = ''
declare @date date
declare @count int
declare @counter int =2
declare @mySql nvarchar(4000)=''
ALTER TABLE #dt ADD JoinedDate date
select @count =max(Count) from #dt2
while @counter<=@count
begin
set @mySql = 'ALTER TABLE #dt ADD JoinedDate'
+ convert(nvarchar,@counter-1) + ' date '
exec (@mySql )
set @counter= @counter+1
end
5-使用光标将 Joineddate 添加到相关列
DECLARE my_cursor CURSOR FOR select Id, JoinedDate, Count from #dt2
open my_cursor
FETCH NEXT FROM my_cursor INTO @Id,@date,@count
WHILE @@FETCH_STATUS =0
BEGIN
set @counter=1
set @mySql=''
while @counter<=@count
begin
if @counter=1
begin
set @mySql = ' update #dt set JoinedDate = ' + char(39) +
convert(nvarchar,@date) + char(39) + ' where Id= '
+ char(39) + @Id + char(39)
end
else
begin
set @mySql = ' update #dt set
JoinedDate' + convert(nvarchar,@counter-1) + ' = '
+ char(39) + convert(nvarchar,@date) + char(39) + '
where Id= ' + char(39) + @Id + char(39)
end
exec (@mySql )
set @counter= @counter+1
end
FETCH NEXT FROM my_cursor INTO @Id,@date,@count
END
close my_cursor
deallocate my_cursor
------------ JOINEDDATE 现在完成 LEFTDATE 开始 ----
1- 插入 leftdate 临时表并加入 #dt
SELECT b.leftDate, a.Id, COUNT(*) AS Count
into #dt3
FROM dbo.#dt AS a INNER JOIN
dbo.tbRegInfo AS b ON
(b.[clubID] +'-'+ convert(nvarchar,b.[personID]) +'-'
+ convert(nvarchar,b.[Sex])+'-'+ convert(nvarchar,b.[Birth Year])
+ '-' +convert(nvarchar,b.[Birth Month]))=a.Id
GROUP BY b.leftDate, a.Id
ORDER BY Count
2-为 Leftdate 添加列
ALTER TABLE #dt ADD LeftDate date
select @count =max(Count) from #dt3
set @mySql =''
set @counter=2
while @counter<=@count
begin
set @mySql = 'ALTER TABLE #dt ADD LeftDate'
+ convert(nvarchar,@counter-1) + ' date '
exec (@mySql )
set @counter= @counter+1
end
3- 用光标将 Leftdate 插入到#dt
DECLARE my_cursor_sec CURSOR FOR select distinct Id, LeftDate,Count from #dt3
open my_cursor_sec
FETCH NEXT FROM my_cursor_sec INTO @Id,@date,@count
WHILE @@FETCH_STATUS =0
BEGIN
set @counter=1
set @mySql=''
while @counter<=@count
begin
if @counter=1
begin
set @mySql = ' update #dt set LeftDate = '
+ char(39) + convert(nvarchar,@date) + char(39)
+ ' where Id= ' + char(39) + @Id + char(39)
end
else
begin
set @mySql = ' update #dt set LeftDate'
+ convert(nvarchar,@counter-1) + ' = ' + char(39)
+ convert(nvarchar,@date) + char(39) + ' where Id= '
+ char(39) + @Id + char(39)
end
exec (@mySql )
set @counter= @counter+1
end
FETCH NEXT FROM my_cursor_sec INTO @Id,@date,@count
END
close my_cursor_sec
deallocate my_cursor_sec
4-现在我们不需要 Id 列
ALTER TABLE #dt DROP COLUMN Id
5-最后选择行
select distinct * from #dt
- 结果
clubID personID Sex Birth Year Birth Month JoinedDate LeftDate
-------- ----------- ---- ----------- ----------- ---------- ----------
B92 84 M 2006 6 2008-07-03 NULL
B92 367 F 1955 8 1988-10-18 NULL
B92 833 F 1962 4 2002-09-05 2000-03-13
B92 836 M 1965 7 2008-11-07 2013-01-18
C77 59 M 1941 5 2011-09-07 2012-01-24
C77 246 M 2008 5 2009-04-15 2011-01-14
C77 478 M 1935 6 2010-06-07 NULL
C77 498 F 1945 1 2011-03-12 NULL
C77 745 M 1942 5 2002-05-14 2010-09-09