有关组内编号的问题,一直困扰着我。现在找到一些方法,不过不是很好。写在这里!!!
第一,
Create table #tmp(SNo varchar(10),flag varchar(4),SName varchar(10))
Create table #tmp(SNo int ,flag varchar(4),SName varchar(10))
INSERT INTO #tmp VALUES (NULL,'組1','Nipsan')
INSERT INTO #tmp VALUES (NULL,'組1','Chow')
INSERT INTO #tmp VALUES (NULL,'組1','Jose')
INSERT INTO #tmp VALUES (NULL,'組1','Vick')
INSERT INTO #tmp VALUES (NULL,'組2','TyN')
INSERT INTO #tmp VALUES (NULL,'組2','MyN')
INSERT INTO #tmp VALUES (NULL,'組3','Cyons')
INSERT INTO #tmp VALUES (NULL,'組3','Jack')
INSERT INTO #tmp VALUES (NULL,'組3','oj')
INSERT INTO #tmp VALUES (NULL,'組4','BP')
INSERT INTO #tmp VALUES (NULL,'組4','Steven')
INSERT INTO #tmp VALUES (NULL,'組4','Kass')
Declare @i int,@j int
SET @j=0
Declare @flag varchar(8000)
UPDATE #tmp
SET @i=@i+1, @flag=@flag+flag,
Sno='00'+cast(@j as varchar)+RIGHT(CAST(power(10,2)+@i as varchar),2),
-- Son = @j + @i ,
@i=CASE WHEN flag=LEFT(@flag,LEN(@flag)/@i) THEN @i ELSE 1 END,
@j=CASE WHEN flag=LEFT(@flag ,LEN(@flag)/@i) THEN @j ELSE @j+1 END,
@flag=CASE WHEN Flag=LEFT(@flag,LEN(@flag)/@i) THEN @flag ELSE flag END
SELECT * From #tmp Order by SNo
Drop table #tmp
-------------------------------------------------------------------------------------------------
select
(select count(1)+1 from #TempTable_DatCharterRouteDetail where DriveDate=T.DriveDate and CharterCode=T.CharterCode and TruckCode=T.TruckCode and StartDate = T.StartDate and Sequence = T.Sequence and id<t.id) as QID , *
-- into #TempTable_DatCharterRouteDetailNew
from #TempTable_DatCharterRouteDetail T