1

我有一张如下表:

frm  -   To   cost   type
A    -   B     10     1
B    -   C     20     2
B    -   K     05     2
C    -   D     08     2
D    -   E     10     2
E    -   F     12     3
K    -   D     13     2
K    -   M     18     2
M    -   D     08     2 

输出应该是:

col1                           col2
A - B - C - D - E - F       -   60  -- (based on (A+B)+(B+C)+(C+D)+(D+E)+(E+F) i.e. (10+20+08+10+12) )
A - B - K - M - D - E - F   -   63  -- based on (10+05+14+08+10+12)

现在,

我想从类型 1 开始,A - B进一步组合类型 2,然后 以类型 3行结束,即上例中的E - F。

使用多个 while 循环并在其中使用可以实现的临时表,但这会对性能造成更大的影响。

这是我到目前为止所做的:

注意:可能存在一些错误,因为我已经找到并替换以替换实际的表和列,但是从这里你会明白我使用了什么

IF OBJECT_ID('tempdb..#tmpFinalTbl') IS NOT NULL
DROP TABLE #tmpFinalTbl

create table #tmpFinalTbl
(
    Sector_Name varchar(max),
    finalRate decimal(18,5)
)

IF OBJECT_ID('tempdb..#tmpType1Sectors') IS NOT NULL
DROP TABLE #tmpType1Sectors

select * into #tmpType1Sectors from TableName
where Type_Id = 1

select * from #tmpType1Sectors

declare @tempType1SectorsCount int
set @tempType1SectorsCount =  (select count(*) from #tmpType1Sectors)

while @tempType1SectorsCount > 0
begin -- begin of #tmpType1Sectors while loop

declare @fromID int
declare @toID int
declare @tempCost decimal(18,5)
declare @tempCost1 decimal(18,5)
declare @tempCost2 decimal(18,5)
declare @finalSectorName varchar(max)
declare @tempSectorID int
declare @finalCost decimal(18,5)

set @fromID = (select top 1 From_Id from #tmpType1Sectors)
set @toID = (select top 1 To_Id from #tmpType1Sectors)
set @tempSectorID = (select top 1 Id from #tmpType1Sectors)
set @finalSectorName = -- getting from another table as initial type 1 i.e. A - B
set @tempCost = (select top 1 Cost from #tmpType1Sectors)
set @tempCost1 = (select top 1 Cost1 from #tmpType1Sectors)
set @tempCost2 = (select top 1 Cost2 from #tmpType1Sectors)

print 'From Loc'
print @fromID

print 'To Loc'
print @toID

print 'Sector'
print @finalSectorName

print 'Cost'
print @tempCost

print 'Cost 1'
print @tempCost1

print 'Cost 2'
print @tempCost2

    IF OBJECT_ID('tempdb..#tmpType2Sectors') IS NOT NULL
    DROP TABLE #tmpType2Sectors

    select * into #tmpType2Sectors from TableName
    where From_Id = @toID
    and
    Sector_Type_Id = 2

    declare @tempTYpe2SectorsCount int
    set @tempTYpe2SectorsCount =  (select count(*) from #tmpType2Sectors)

    if(@tempTYpe2SectorsCount > 1)
    begin

        while @tempTYpe2SectorsCount > 0
        begin -- begin of #tmpType2Sectors while loop

            declare @tempTrSector varchar(max)

            set @fromID = (select top 1 From_Id from #tmpType2Sectors)
            set @toID = (select top 1 To_Id from #tmpType2Sectors)
            set @tempTrSector = (select top 1 substring(Sector_Code,CHARINDEX('-',Sector_Code)+1,LEN(Sector_Code)) from #tmpType2Sectors)
            set @tempCost = ISNULL(@tempCost,0) + (select top 1 Cost from #tmpType2Sectors)
            set @tempCost1 = ISNULL(@tempCost1,0) + (select top 1 Cost1 from #tmpType2Sectors)
            set @tempCost2 = ISNULL(@tempCost2,0) + (select top 1 Cost2 from #tmpType2Sectors)

            set @finalCost = ISNULL(@tempCost,0) + ISNULL(@tempCost1,0) + ISNULL(@tempCost2,0)
            set @finalSectorName = @finalSectorName + '-' + @tempTrSector

                print 'Tr From Loc'
                print @fromID

                print 'Tr To Loc'
                print @toID

                    IF OBJECT_ID('tempdb..#tmpType3Sectors') IS NOT NULL
                    DROP TABLE #tmpType3Sectors

                    select * into #tmpType3Sectors from TableName
                    where From_Id = @toID
                    and
                    Sector_Type_Id = 3

                    --select * from #tmpType3Sectors

                    declare @tempType3SectorsCount int
                    set @tempType3SectorsCount =  (select count(*) from #tmpType3Sectors)

                    if(@tempType3SectorsCount > 1)
                    begin

                        while @tempType3SectorsCount > 0
                        begin -- begin of #tmpType3Sectors while loop

                            declare @tempDelSector varchar(max)

                            set @fromID = (select top 1 From_Id from #tmpType3Sectors)
                            set @toID = (select top 1 To_Id from #tmpType3Sectors)
                            set @tempDelSector = (select top 1 substring(Sector_Code,CHARINDEX('-',Sector_Code)+1,LEN(Sector_Code)) from #tmpType3Sectors)
                            set @tempCost = ISNULL(@tempCost,0) + (select top 1 Cost from #tmpType3Sectors)
                            set @tempCost1 = ISNULL(@tempCost1,0) + (select top 1 Cost1 from #tmpType3Sectors)
                            set @tempCost2 = ISNULL(@tempCost2,0) + (select top 1 Cost2 from #tmpType3Sectors)

                            print 'Del From Loc'
                            print @fromID

                            print 'Del To Loc'
                            print @toID


                            print '@tempDelSector'
                            print @tempDelSector

                            set @finalCost = ISNULL(@tempCost,0) + ISNULL(@tempCost1,0) + ISNULL(@tempCost2,0)
                            set @finalSectorName = @finalSectorName + '-' + @tempDelSector

                                print 'final Sector'
                                print @finalSectorName

                            insert into #tmpFinalTbl
                            (
                            Sector_Name,
                            finalRate
                            )
                            values
                            (
                            @finalSectorName,
                            @finalCost
                            )

                        delete top(1) from #tmpType3Sectors
                        set @tempType3SectorsCount = @tempType3SectorsCount - 1
                        end -- end of #tmpType3Sectors while loop

                    end
                    else
                    begin
                    print 'One Del'
                        -- handle if one sequence is found

                            set @tempDelSector = (select top 1 substring(Sector_Code,CHARINDEX('-',Sector_Code)+1,LEN(Sector_Code)) from #tmpType3Sectors)
                            set @tempCost = ISNULL(@tempCost,0) + (select top 1 Cost from #tmpType3Sectors)
                            set @tempCost1 = ISNULL(@tempCost1,0) + (select top 1 Cost1 from #tmpType3Sectors)
                            set @tempCost2 = ISNULL(@tempCost2,0) + (select top 1 Cost2 from #tmpType3Sectors)
                            set @fromID = (select top 1 From_Id from #tmpType3Sectors)
                            set @toID = (select top 1 To_Id from #tmpType3Sectors)

                            print 'Del From Loc'
                            print @fromID

                            print 'Del To Loc'
                            print @toID

                            print '@tempDelSector'
                            print @tempDelSector

                            set @finalCost = ISNULL(@tempCost,0) + ISNULL(@tempCost1,0) + ISNULL(@tempCost2,0)
                            set @finalSectorName = @finalSectorName + '-' + @tempDelSector

                                print 'final Sector'
                                print @finalSectorName

                            insert into #tmpFinalTbl
                            (
                            Sector_Name,
                            finalRate
                            )
                            values
                            (
                            @finalSectorName,
                            @finalCost
                            )

                   end -- end of Del Handling


            print '@tempTrSector'
            print @tempTrSector

            set @finalCost = ISNULL(@tempCost,0) + ISNULL(@tempCost1,0) + ISNULL(@tempCost2,0)
            set @finalSectorName = @finalSectorName + '-' + @tempTrSector

            insert into #tmpFinalTbl
            (
            Sector_Name,
            finalRate
            )
            values
            (
            @finalSectorName,
            @finalCost
            )

        delete top(1) from #tmpType2Sectors
        set @tempTYpe2SectorsCount = @tempTYpe2SectorsCount - 1
        end -- end of #tmpType2Sectors while loop

    end
    else
    begin
    print 'One Tr'
        -- handle if one sequence is found

            set @fromID = (select top 1 From_Id from #tmpType2Sectors)
            set @toID = (select top 1 To_Id from #tmpType2Sectors)
            set @tempTrSector = (select top 1 substring(Sector_Code,CHARINDEX('-',Sector_Code)+1,LEN(Sector_Code)) from #tmpType2Sectors)
            set @tempCost = ISNULL(@tempCost,0) + (select top 1 Cost from #tmpType2Sectors)
            set @tempCost1 = ISNULL(@tempCost1,0) + (select top 1 Cost1 from #tmpType2Sectors)
            set @tempCost2 = ISNULL(@tempCost2,0) + (select top 1 Cost2 from #tmpType2Sectors)
            set @finalCost = ISNULL(@tempCost,0) + ISNULL(@tempCost1,0) + ISNULL(@tempCost2,0)
            set @finalSectorName = @finalSectorName + '-' + @tempTrSector

            print 'Tr From Loc'
            print @fromID

            print 'Tr To Loc'
            print @toID


            print '@tempTrSector'
            print @tempTrSector

                    IF OBJECT_ID('tempdb..#tmpType3Sectors1') IS NOT NULL
                    DROP TABLE #tmpType3Sectors1

                    select * into #tmpType3Sectors1 from TableName
                    where From_Id = @toID
                    and
                    Sector_Type_Id = 3

                    --select * from #tmpType3Sectors1

                    --declare @tempType3SectorsCount int
                    set @tempType3SectorsCount =  (select count(*) from #tmpType3Sectors1)

                    if(@tempType3SectorsCount > 1)
                    begin

                        while @tempType3SectorsCount > 0
                        begin -- begin of #tmpType3Sectors1 while loop

                            --declare @tempDelSector varchar(max)

                            set @fromID = (select top 1 From_Id from #tmpType3Sectors1)
                            set @toID = (select top 1 To_Id from #tmpType3Sectors1)
                            set @tempDelSector = (select top 1 substring(Sector_Code,CHARINDEX('-',Sector_Code)+1,LEN(Sector_Code)) from #tmpType3Sectors1)
                            set @tempCost = ISNULL(@tempCost,0) + (select top 1 Cost from #tmpType3Sectors1)
                            set @tempCost1 = ISNULL(@tempCost1,0) + (select top 1 Cost1 from #tmpType3Sectors1)
                            set @tempCost2 = ISNULL(@tempCost2,0) + (select top 1 Cost2 from #tmpType3Sectors1)




                            print '@tempDelSector'
                            print @tempDelSector

                            set @finalCost = ISNULL(@tempCost,0) + ISNULL(@tempCost1,0) + ISNULL(@tempCost2,0)
                            set @finalSectorName = @finalSectorName + '-' + @tempDelSector

                                print 'final Sector'
                                print @finalSectorName

                            insert into #tmpFinalTbl
                            (
                            Sector_Name,
                            finalRate
                            )
                            values
                            (
                            @finalSectorName,
                            @finalCost
                            )

                        delete top(1) from #tmpType3Sectors1
                        set @tempType3SectorsCount = @tempType3SectorsCount - 1
                        end -- end of #tmpType3Sectors1 while loop

                    end
                    else
                    begin
                    print 'One Del'
                        -- handle if one sequence is found

                            set @fromID = (select top 1 From_Id from #tmpType3Sectors1)
                            set @toID = (select top 1 To_Id from #tmpType3Sectors1)
                            set @tempDelSector = (select top 1 substring(Sector_Code,CHARINDEX('-',Sector_Code)+1,LEN(Sector_Code)) from #tmpType3Sectors1)
                            set @tempCost = ISNULL(@tempCost,0) + (select top 1 Cost from #tmpType3Sectors1)
                            set @tempCost1 = ISNULL(@tempCost1,0) + (select top 1 Cost1 from #tmpType3Sectors1)
                            set @tempCost2 = ISNULL(@tempCost2,0) + (select top 1 Cost2 from #tmpType3Sectors1)

                            print '@tempDelSector'
                            print @tempDelSector

                            set @finalCost = ISNULL(@tempCost,0) + ISNULL(@tempCost1,0) + ISNULL(@tempCost2,0)
                            set @finalSectorName = @finalSectorName + '-' + @tempDelSector

                                print 'final Sector'
                                print @finalSectorName

                            insert into #tmpFinalTbl
                            (
                            Sector_Name,
                            finalRate
                            )
                            values
                            (
                            @finalSectorName,
                            @finalCost
                            )

                   end -- end of Del Handling

            --set @finalCost = ISNULL(@tempCost,0) + ISNULL(@tempCost1,0) + ISNULL(@tempCost2,0)
            --set @finalSectorName = @finalSectorName + '-' + @tempTrSector

            insert into #tmpFinalTbl
            (
            Sector_Name,
            finalRate
            )
            values
            (
            @finalSectorName,
            @finalCost
            )

    end

delete top(1) from #tmpType1Sectors
set @tempType1SectorsCount = @tempType1SectorsCount - 1

end -- end of while loop

select * from #tmpFinalTbl

编辑:尝试 CTE(递归查询)来获取输出,但如果类型 2 组合不止一个,即 B 到 C 和 B 到 K,那么它将以无限递归结束。:(

任何帮助将不胜感激。

提前致谢。

4

0 回答 0