如何使用Order By和With子句

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Order By和With子句相关的知识,希望对你有一定的参考价值。

我正在使用以下SQL查询来获取记录集。

;WITH SFPIPELINE AS (
    SELECT
        PIPELINE_STRING,
        PACKET_NUMBER,
        PIPELINE_NUMBER
    FROM
        [RTMASTER].[DBO].[SF_PIPELINE]
    WHERE
        PIPELINE_STRING IN (
            'SOLUTION_TEST',
            '2018.01_SVC_SANDBOX',
            '2018.01_SVC_ENG'
        )
        AND PACKET_NUMBER IN (98, 1090, 1092)
),
PROJ_INST_PIPELINE AS (
    SELECT
        DISTINCT PIP.PROJECT_INSTANCE_PIPELINE_ID,
        PIP.PROJECT_INSTANCE_ID,
        PIP.PACKET_NUMBER,
        PIP.PROJECT_NUMBER,
        PIP.SOURCE_SET_INSTANCE,
        SFP.PIPELINE_STRING
    FROM
        PROJECT_INSTANCE_PIPELINE PIP
        INNER JOIN SFPIPELINE SFP ON PIP.PACKET_NUMBER = SFP.PACKET_NUMBER
        AND PIP.PIPELINE_NUMBER = SFP.PIPELINE_NUMBER
        AND PIP.ACTIVE = 1
        AND PIP.PROJECT_INSTANCE_PIPELINE_ID >= 20481038
),
PROJ_INST_BASE AS (
    SELECT
        PIP.PROJECT_INSTANCE_PIPELINE_ID,
        PIP.PROJECT_NUMBER,
        PIP.PACKET_NUMBER,
        PIP.PIPELINE_STRING,
        PIP.SOURCE_SET_INSTANCE,
        PIP.PROJECT_INSTANCE_ID,
        PIB.ORIGINAL_PROMOTER,
        PIB.DEV_INSTANCE,
        PROJECT_TYPE_NUMBER,
        PIB.SUBVERSION_PROJECT_REVISION,
        PIB.SUBVERSION _PROJECT_URL,
        PIB.Front_End,
        PIB.Back_End
    FROM
        PROJECT_INSTANCE_BASE PIB
        INNER JOIN PROJ_INST_PIPELINE PIP ON PIB.PROJECT_INSTANCE_ID = PIP.PROJECT_INSTANCE_ID
        AND PIP.PROJECT_NUMBER = PIB.PROJECT_NUMBER
        AND PIB.PROJECT_TYPE_NUMBER IN (5, 105, 106)
),
SF_PROJ AS (
    SELECT
        PJTINST.PROJECT_INSTANCE_PIPELINE_ID,
        PJTINST.PROJECT_INSTANCE_ID,
        PJTINST.PROJECT_NUMBER,
        PJTINST.PIPELINE_STRING,
        PJTINST.ORIGINAL_PROMOTER,
        PJTINST.SOURCE_SET_INSTANCE,
        PJTINST.PROJECT_TYPE_NUMBER,
        PJTINST.PACKET_NUMBER,
        SFP.PROJECT_NAME,
        PJTINST.SUBVERSION_PROJECT_REVISION,
        PJTINST.SUBVERSION_PROJECT_URL,
        PJTINST.Front_End,
        PJTINST.Back_End
    FROM
        DBO.SF_PROJECT SFP
        INNER JOIN PROJ_INST_BASE PJTINST ON SFP.PROJECT_NUMBER = PJTINST.PROJECT_NUMBER
),
USER_DETAIL AS (
    SELECT
        SFP.PROJECT_NAME,
        SFP.PROJECT_NUMBER,
        SFP.PROJECT_TYPE_NUMBER,
        SFP.SOURCE_SET_INSTANCE,
        SFP.PACKET_NUMBER,
        SFP.PIPELINE_STRING,
        SFP.SUBVERSION_PROJECT_REVISION,
        SFP.SUBVERSION_PROJECT_URL,
        SFP.PROJECT_INSTANCE_PIPELINE_ID,
        SFP.PROJECT_INSTANCE_ID,
        AIAA.EMAIL_ADDRESS,
        SFP.Front_End,
        SFP.Back_End
    FROM
        SF_ASSOCIATE_INFO_ALL_ASSOCIATES AIAA
        INNER JOIN SF_PROJ SFP ON AIAA.OPER_ID = SFP.ORIGINAL_PROMOTER
),
FINAL AS (
    SELECT
        UD.PROJECT_NAME,
        FP.Feature_Number,
        UD.PROJECT_NUMBER,
        UD.PROJECT_TYPE_NUMBER,
        UD.SOURCE_SET_INSTANCE,
        UD.PACKET_NUMBER,
        UD.PIPELINE_STRING,
        UD.SUBVERSION_PROJECT_REVISION,
        UD.SUBVERSION_PROJECT_URL,
        UD.PROJECT_INSTANCE_PIPELINE_ID,
        UD.PROJECT_INSTANCE_ID,
        UD.EMAIL_ADDRESS,
        UD.Front_End,
        UD.Back_End
    FROM
        [RTMaster].[dbo].[Feature_Projects_History] FP
        INNER JOIN USER_DETAIL UD ON FP.Project_Instance_Pipeline_ID = UD.PROJECT_INSTANCE_PIPELINE_ID
)
SELECT
    *
FROM
    FINAL

查询工作正常只是记录没有排序。

我想在PROJECT_INSTANCE_PIPELINE_ID上使用order by,以便对所有行进行排序。当我使用ORDER BY子句看到以下错误。

错误:ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效,除非还指定了TOP,OFFSET或FOR XML。

不确定如何一起使用Order By和With Clause。

有什么想法吗!

答案

尝试下面我只是在PROJECT_NAME使用order by

WITH SFPIPELINE AS     
                    (SELECT PIPELINE_STRING, PACKET_NUMBER, PIPELINE_NUMBER FROM [RTMASTER].[DBO].[SF_PIPELINE]    
                    WHERE PIPELINE_STRING IN ( 'SOLUTION_TEST', '2018.01_SVC_SANDBOX', '2018.01_SVC_ENG')    
                    AND PACKET_NUMBER IN (98, 1090, 1092)),   
                PROJ_INST_PIPELINE AS   
                    (SELECT DISTINCT PIP.PROJECT_INSTANCE_PIPELINE_ID, PIP.PROJECT_INSTANCE_ID, PIP.PACKET_NUMBER, PIP.PROJECT_NUMBER, PIP.SOURCE_SET_INSTANCE, SFP.PIPELINE_STRING FROM PROJECT_INSTANCE_PIPELINE PIP  
                    INNER JOIN SFPIPELINE SFP ON PIP.PACKET_NUMBER = SFP.PACKET_NUMBER AND PIP.PIPELINE_NUMBER = SFP.PIPELINE_NUMBER AND PIP.ACTIVE = 1   
                    AND PIP.PROJECT_INSTANCE_PIPELINE_ID >=   20481038),    
                PROJ_INST_BASE AS             
                    (SELECT PIP.PROJECT_INSTANCE_PIPELINE_ID, PIP.PROJECT_NUMBER, PIP.PACKET_NUMBER, PIP.PIPELINE_STRING, PIP.SOURCE_SET_INSTANCE, PIP.PROJECT_INSTANCE_ID, PIB.ORIGINAL_PROMOTER, PIB.DEV_INSTANCE,PROJECT_TYPE_NUMBER, PIB.SUBVERSION_PROJECT_REVISION, PIB.SUBVERSION_PROJECT_URL, 
                    PIB.Front_End, PIB.Back_End FROM PROJECT_INSTANCE_BASE PIB INNER JOIN PROJ_INST_PIPELINE PIP ON PIB.PROJECT_INSTANCE_ID = PIP.PROJECT_INSTANCE_ID AND PIP.PROJECT_NUMBER= PIB.PROJECT_NUMBER AND PIB.PROJECT_TYPE_NUMBER IN (5,105, 106)),           
                SF_PROJ AS   
                    (SELECT PJTINST.PROJECT_INSTANCE_PIPELINE_ID, PJTINST.PROJECT_INSTANCE_ID, PJTINST.PROJECT_NUMBER, PJTINST.PIPELINE_STRING, PJTINST.ORIGINAL_PROMOTER, PJTINST.SOURCE_SET_INSTANCE, PJTINST.PROJECT_TYPE_NUMBER, PJTINST.PACKET_NUMBER, SFP.PROJECT_NAME,    
                    PJTINST.SUBVERSION_PROJECT_REVISION, PJTINST.SUBVERSION_PROJECT_URL, PJTINST.Front_End, PJTINST.Back_End FROM DBO.SF_PROJECT SFP INNER JOIN PROJ_INST_BASE PJTINST ON SFP.PROJECT_NUMBER = PJTINST.PROJECT_NUMBER),  
                USER_DETAIL AS  
                    (SELECT SFP.PROJECT_NAME, SFP.PROJECT_NUMBER, SFP.PROJECT_TYPE_NUMBER, SFP.SOURCE_SET_INSTANCE, SFP.PACKET_NUMBER, SFP.PIPELINE_STRING, SFP.SUBVERSION_PROJECT_REVISION, SFP.SUBVERSION_PROJECT_URL, SFP.PROJECT_INSTANCE_PIPELINE_ID, SFP.PROJECT_INSTANCE_ID, AIAA.EMAIL_ADDRESS, SFP.Front_End, SFP.Back_End  
                    FROM SF_ASSOCIATE_INFO_ALL_ASSOCIATES AIAA INNER JOIN SF_PROJ SFP ON AIAA.OPER_ID = SFP.ORIGINAL_PROMOTER),  
                FINAL AS 
                    (SELECT UD.PROJECT_NAME, FP.Feature_Number, UD.PROJECT_NUMBER, UD.PROJECT_TYPE_NUMBER, UD.SOURCE_SET_INSTANCE, UD.PACKET_NUMBER, UD.PIPELINE_STRING, UD.SUBVERSION_PROJECT_REVISION, UD.SUBVERSION_PROJECT_URL,   
                    UD.PROJECT_INSTANCE_PIPELINE_ID, UD.PROJECT_INSTANCE_ID, UD.EMAIL_ADDRESS, UD.Front_End, UD.Back_End  FROM [RTMaster].[dbo].[Feature_Projects_History] FP 
                    INNER JOIN USER_DETAIL UD ON FP.Project_Instance_Pipeline_ID = UD.PROJECT_INSTANCE_PIPELINE_ID) 
                SELECT * FROM FINAL order by PROJECT_NAME -- use here others column name as you need

以上是关于如何使用Order By和With子句的主要内容,如果未能解决你的问题,请参考以下文章

使用 GROUP BY 和 ORDER BY 子句以降序打印记录

如何将 pdo 的准备好的语句用于 order by 和 limit 子句?

如何在 PostgreSQL ORDER BY 子句中使用别名?

带有“Order Each by”子句的 Google BigQuery 大表(105M 记录)产生“资源超出查询执行”错误

Sql order by 和 group BY 如何共同运用?

only_full_group_by : "ORDER BY 子句不在 GROUP BY 子句中"