我们可以在子查询中的where子句之后使用order by?

时间:2016-03-09 07:26:22

标签: sql sql-server sql-server-2008 stored-procedures

我正在尝试对里程碑id coloumn上的记录进行排序,但它在命令中抛出错误,在子查询中给出错误“ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效,除非还指定了TOP或FOR XML。“

CREATE PROCEDURE [dbo].[spGetALLStudyCodedtls]
(
    @StudyCodeId VARCHAR(MAX)
)
AS

BEGIN

    SELECT X.[IsActive]
           --scd.[StudyCodeId]
           ,
           X.[StudyCode],
           X.[ProductName],
           X.[TherapyName],
           X.[Dosage],
           X.[StudyType],
           X.[Condition],
           X.[CountryName],
           X.[CRO],
           X.[Design],
           X.[DesignTag],
           X.[RnD ProjManager],
           X.[Project Manager],
           X.[IPDFillingTimeline],
           X.[Comments],
           X.[MileStoneId],
           X.[Milestone],
           X.[Category],
           X.[BaseLineDate],
           X.[ActualDate]
    FROM (
        SELECT sc.IsActive
               --scd.[StudyCodeId]
               ,
               scd.[StudyCode],
               pm.ProductName,
               tm.TherapyName,
               dm.Dosage,
               stm.StudyType,
               com.Condition,
               cn.CountryName,
               crm.CRO,
               dsm.Design,
               dtm.DesignTag,
               ud.[user_name] AS 'RnD ProjManager',
               udd.[user_name] AS 'Project Manager',
               scd.[IPDFillingTimeline],
               scd.[Remarks] AS 'Comments',
               mm.MileStoneId,
               mm.MileStoneName AS 'Milestone',
               mm.Category AS 'Category',
               sc.[BaseLineDate],
               sc.[ActualDate]
        FROM [PMS].[dbo].[StudyCodeDetails] scd
        INNER JOIN [dbo].[StudyCodeToMileStone] sc ON scd.StudyCodeId = sc.StudyCodeId
        INNER JOIN [dbo].[MileStoneMaster] mm ON sc.MileStoneId = mm.MileStoneId
        INNER JOIN dbo.ProductMaster pm ON scd.ProductId = pm.ProductId
        INNER JOIN dbo.TherapyMaster tm ON tm.TherapyId = scd.TherapyId
        INNER JOIN dbo.DosageMaster dm ON dm.DosageId = scd.DosageId
        INNER JOIN dbo.CountryMaster cn ON cn.CountryId = scd.CountryId
        INNER JOIN dbo.[User_Dtls] ud ON ud.Pk_ID = scd.RnDProjManagerId
        INNER JOIN dbo.[User_Dtls] udd ON udd.Pk_ID = scd.ProjectManagerId
        INNER JOIN dbo.CroMaster crm ON crm.CroId = scd.CRO
        INNER JOIN dbo.DesignMaster dsm ON dsm.DesignId = scd.DesignId
        INNER JOIN dbo.DesignTagMaster dtm ON dtm.DesignTagId = scd.DesignTagId
        INNER JOIN dbo.StudyTypeMaster stm ON stm.StudyTypeId = scd.StudyTypeId
        INNER JOIN dbo.ConditionMaster com ON com.ConditionId = scd.Condition
        WHERE scd.StudyCodeId IN (
                SELECT CAST(Item AS INTEGER)
                FROM [dbo].[SplitString](@StudyCodeId, ',')
            )
            AND mm.[AdditionalPayment] = 0
        ORDER BY mm.MileStoneId
    ) AS X
    GROUP BY X.[IsActive]
             --scd.[StudyCodeId]
             ,
             X.[StudyCode],
             X.[ProductName],
             X.[TherapyName],
             X.[Dosage],
             X.[StudyType],
             X.[Condition],
             X.[CountryName],
             X.[CRO],
             X.[Design],
             X.[DesignTag],
             X.[RnD ProjManager],
             X.[Project Manager],
             X.[IPDFillingTimeline],
             X.[Comments],
             X.[MileStoneId],
             X.[Milestone],
             X.[Category],
             X.[BaseLineDate],
             X.[ActualDate]

END

1 个答案:

答案 0 :(得分:0)

请尝试这个:

CREATE PROCEDURE [dbo].[spGetALLStudyCodedtls]
(
    @StudyCodeId VARCHAR(MAX)
)
AS

BEGIN

    SELECT X.[IsActive]
           --scd.[StudyCodeId]
           ,
           X.[StudyCode],
           X.[ProductName],
           X.[TherapyName],
           X.[Dosage],
           X.[StudyType],
           X.[Condition],
           X.[CountryName],
           X.[CRO],
           X.[Design],
           X.[DesignTag],
           X.[RnD ProjManager],
           X.[Project Manager],
           X.[IPDFillingTimeline],
           X.[Comments],
           X.[MileStoneId],
           X.[Milestone],
           X.[Category],
           X.[BaseLineDate],
           X.[ActualDate]
    FROM (
        SELECT t1.IsActive,
               t1.[StudyCode],
               t2.ProductName,
               t2.TherapyName,
               t2.Dosage,
               t2.StudyType,
               t2.Condition,
               t2.CountryName,
               t2.CRO,
               t2.Design,
               t2.DesignTag,
               t2.[user_name] AS 'RnD ProjManager',
               t2.[user_name] AS 'Project Manager',
               t1.[IPDFillingTimeline],
               scd.[Remarks] AS 'Comments',
               t2.MileStoneId,
               t2.MileStoneName AS 'Milestone',
               t2.Category AS 'Category',
               t1.[BaseLineDate],
               t1.[ActualDate]
        FROM 
        (
            SELECT sc.IsActive,
                   scd.[StudyCode],              
                   scd.[IPDFillingTimeline],
                   scd.[Remarks] AS 'Comments',              
                   sc.[BaseLineDate],
                   sc.[ActualDate],
                   sc.MileStoneId
             FROM [PMS].[dbo].[StudyCodeDetails] scd
             INNER JOIN [dbo].[StudyCodeToMileStone] sc 
                 ON scd.StudyCodeId = sc.StudyCodeId
        ) AS t1
        CROSS APPLY (
                    SELECT pm.ProductName,
                           tm.TherapyName,
                           dm.Dosage,
                           stm.StudyType,
                           com.Condition,
                           cn.CountryName,
                           crm.CRO,
                           dsm.Design,
                           dtm.DesignTag,
                           ud.[user_name] AS 'RnD ProjManager',
                           udd.[user_name] AS 'Project Manager',               
                           mm.MileStoneId,
                           mm.MileStoneName AS 'Milestone',
                           mm.Category AS 'Category',
                    FROM [dbo].[MileStoneMaster] mm 
                    INNER JOIN dbo.ProductMaster pm ON scd.ProductId = pm.ProductId
                    INNER JOIN dbo.TherapyMaster tm ON tm.TherapyId = scd.TherapyId
                    INNER JOIN dbo.DosageMaster dm ON dm.DosageId = scd.DosageId
                    INNER JOIN dbo.CountryMaster cn ON cn.CountryId = scd.CountryId
                    INNER JOIN dbo.[User_Dtls] ud ON ud.Pk_ID = scd.RnDProjManagerId
                    INNER JOIN dbo.[User_Dtls] udd ON udd.Pk_ID = scd.ProjectManagerId
                    INNER JOIN dbo.CroMaster crm ON crm.CroId = scd.CRO
                    INNER JOIN dbo.DesignMaster dsm ON dsm.DesignId = scd.DesignId
                    INNER JOIN dbo.DesignTagMaster dtm ON dtm.DesignTagId = scd.DesignTagId
                    INNER JOIN dbo.StudyTypeMaster stm ON stm.StudyTypeId = scd.StudyTypeId
                    INNER JOIN dbo.ConditionMaster com ON com.ConditionId = scd.Condition
                    WHERE scd.StudyCodeId IN (
                            SELECT CAST(Item AS INTEGER)
                            FROM [dbo].[SplitString](@StudyCodeId, ',')
                        )
                    AND mm.[AdditionalPayment] = 0
                    AND mm.MileStoneId = t1.MileStoneId
                    ORDER BY mm.MileStoneId
        ) AS t2         
    ) AS X
    GROUP BY X.[IsActive]
             --scd.[StudyCodeId]
             ,
             X.[StudyCode],
             X.[ProductName],
             X.[TherapyName],
             X.[Dosage],
             X.[StudyType],
             X.[Condition],
             X.[CountryName],
             X.[CRO],
             X.[Design],
             X.[DesignTag],
             X.[RnD ProjManager],
             X.[Project Manager],
             X.[IPDFillingTimeline],
             X.[Comments],
             X.[MileStoneId],
             X.[Milestone],
             X.[Category],
             X.[BaseLineDate],
             X.[ActualDate]

END