插入...按顺序选择

时间:2016-01-12 14:23:33

标签: sql-server tsql ssis sql-order-by dynamics-ax-2012

我在从另一个表中按特定顺序将数据插入表中时遇到问题。我希望按列Niveau

排序数据

我的查询如下:

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

SELECT  
    Ecritures.RecIdLine    AS RecIdLine, 
    Ecritures.DISPLAYVALUEComb AS DISPLAYVALUEComb,
    LevelTable.DIMENSIONATTRIBUTE   AS DimAttribute,
    LevelTable.LEVEL_          AS Niveau,
    DimAttr.Name               AS Dim_Label
INTO 
    #DISPLAYVALUEALL
FROM 
    #BudgetTransTmp     As Ecritures            
INNER JOIN 
    #InterfacesParmTmp  AS Tmp1 ON Tmp1.DATAAREAID = Ecritures.DATAAREAID   
INNER JOIN 
    MicrosoftDynamicsAx.dbo.DIMENSIONHIERARCHYLEVEL As LevelTable ON LevelTable.DIMENSIONHIERARCHY = Ecritures.ACCOUNTSTRUCTURE
                                   AND LevelTable.PARTITION = @Partition
                                   AND LevelTable.DIMENSIONATTRIBUTE IN (Tmp1.TaskCode, Tmp1.Activity, Tmp1.BudgetNature, Tmp1.CentreCout)
INNER JOIN 
    MicrosoftDynamicsAx.dbo.DIMENSIONATTRIBUTE As DimAttr ON DimAttr.RECID = LevelTable.DIMENSIONATTRIBUTE
                              AND DimAttr.PARTITION = @Partition
ORDER BY  
    RecIdLine,DISPLAYVALUEComb , Niveau ASC, DimAttribute DESC

    --SELECT *  from #DISPLAYVALUEALL

这不起作用。在select into语句中使用order by是不合法的吗?

这是我的结果

ReciID      DisplayValue        Dimension   Niveau
5637157342  601200-001-026--    22565421189 5
5637157342  601200-001-026--    22565421195 2
5637157342  601200-001-026--    22565421196 3
5637157342  601200-001-026--    22565421197 4

5637157326  601200-001-027--    22565421189 5
5637157326  601200-001-027--    22565421195 2
5637157326  601200-001-027--    22565421196 3
5637157326  601200-001-027--    22565421197 4

谢谢!

更新:我的解决方案是添加以下行:

Row_number() over(ORDER BY Ecritures.RecIdLine, Ecritures.DISPLAYVALUEComb, LevelTable.LEVEL_ ASC, LevelTable.DIMENSIONATTRIBUTE DESC) as RowNumber,

我的剧本变成了:

IF OBJECT_ID('tempdb..#DISPLAYVALUEALL') IS NOT NULL
            DROP TABLE #DISPLAYVALUEALL
    SELECT  Row_number() over(ORDER BY Ecritures.RecIdLine  ,Ecritures.DISPLAYVALUEComb , LevelTable.LEVEL_  asc, LevelTable.DIMENSIONATTRIBUTE  desc) as RowNumber,
                 Ecritures.RecIdLine       AS RecIdLine, 
                Ecritures.DISPLAYVALUEComb AS DISPLAYVALUEComb,
                LevelTable.DIMENSIONATTRIBUTE   AS DimAttribute,
                LevelTable.LEVEL_          AS Niveau,
                DimAttr.Name               AS Dim_Label

        INTO #DISPLAYVALUEALL
        FROM #BudgetTransTmp        As Ecritures            

            INNER JOIN #InterfacesParmTmp   AS Tmp1
                ON Tmp1.DATAAREAID          = Ecritures.DATAAREAID  

            INNER JOIN MicrosoftDynamicsAx.dbo.DIMENSIONHIERARCHYLEVEL      As LevelTable
                ON LevelTable.DIMENSIONHIERARCHY    = Ecritures.ACCOUNTSTRUCTURE
                AND LevelTable.PARTITION            = @Partition
                AND LevelTable.DIMENSIONATTRIBUTE   IN (Tmp1.TaskCode,Tmp1.Activity, Tmp1.BudgetNature, Tmp1.CentreCout)

            INNER JOIN MicrosoftDynamicsAx.dbo.DIMENSIONATTRIBUTE   As DimAttr
                ON DimAttr.RECID                    = LevelTable.DIMENSIONATTRIBUTE
                AND DimAttr.PARTITION               = @Partition 
               ORDER BY RowNumber

现在,数据按表I排序:

SELECT * from #DISPLAYVALUEALL

2 个答案:

答案 0 :(得分:0)

无意订购插页

如果没有订单,则没有保证订单

您需要移动ORDER BY

SELECT * from #DISPLAYVALUEALL
ORDER BY RecIdLine, DISPLAYVALUEComb , Niveau asc, DimAttribute desc

或者您可以使用

SELECT * from #DISPLAYVALUEALL
 ORDER BY RowNumber

答案 1 :(得分:-1)

尝试将您的选择查询更改为以下内容并再次执行:

SELECT T.*
  INTO #DISPLAYVALUEALL
  FROM (SELECT  
         Ecritures.RecIdLine       AS RecIdLine, 
        Ecritures.DISPLAYVALUEComb AS DISPLAYVALUEComb,
        LevelTable.DIMENSIONATTRIBUTE   AS DimAttribute,
        LevelTable.LEVEL_          AS Niveau,
        DimAttr.Name               AS Dim_Label

FROM #BudgetTransTmp        As Ecritures            

    INNER JOIN #InterfacesParmTmp   AS Tmp1
        ON Tmp1.DATAAREAID          = Ecritures.DATAAREAID  

    INNER JOIN MicrosoftDynamicsAx.dbo.DIMENSIONHIERARCHYLEVEL      As LevelTable
        ON LevelTable.DIMENSIONHIERARCHY    = Ecritures.ACCOUNTSTRUCTURE
        AND LevelTable.PARTITION            = @Partition
        AND LevelTable.DIMENSIONATTRIBUTE   IN (Tmp1.TaskCode,Tmp1.Activity, Tmp1.BudgetNature, Tmp1.CentreCout)

    INNER JOIN MicrosoftDynamicsAx.dbo.DIMENSIONATTRIBUTE   As DimAttr
        ON DimAttr.RECID                    = LevelTable.DIMENSIONATTRIBUTE
        AND DimAttr.PARTITION               = @Partition                        

 ORDER BY RecIdLine,DISPLAYVALUEComb , Niveau asc, DimAttribute desc) T