如何在SQL Server的where子句中使用别名

时间:2019-02-04 13:22:36

标签: sql-server asp.net-mvc

我有以下查询,但我得到的结果是我想要的完美结果,但是我想在where子句条件下使用stManufacturerPartReference别名,就像下面的第二个查询一样,但是它给了我错误。

WITH ProductsCTE (inProductId, inCategoryId, stCategory, stManufacturers, inCompanyId, stERPId,stManufacturerPartReference,
                                stProductName, stProductNumber, stModel, stFileLink, stImage, dcPrice,dcStandardPrice, dcOnHandQty,dcQtyOnPO,dtEstimatedShipDate, dcWeight, inSyncStatus, dtLastSyncDate,
                                inErrorRetry,flgIsActive, flgIsDeleted, inCreatedBy, inModifiedBy, dtModificationDate, dtCreationDate, inRecordCount)
                AS (
                    SELECT
                        product.inProductId,
                        product.inCategoryId,
                        product.stCategory,
                        product.stManufacturers,
                        product.inCompanyId,
                        product.stERPId,
                        product.stProductName,
                        STUFF((SELECT ', ' + PM.stManufacturerPartReference
                           FROM tblProductManufacturers PM
                           JOIN tblProducts Product on PM.inProductId = Product.inProductId
                           JOIN tblManufacturers M on M.inManufacturerId = PM.inManufacturerId
                           WHERE PM.inProductId=product.inProductId
                           ORDER BY M.stManufacturer
                           FOR XML PATH('')), 1, 1, '') as stManufacturerPartReference,
                        product.stProductNumber,
                        product.stModel,
                        product.stFileLink,
                        product.stImage,
                        product.dcPrice,
                        product.dcStandardPrice,
                        product.dcOnHandQty,
                        product.dcQtyOnPO,
                        product.dtEstimatedShipDate,
                        product.dcWeight,
                        product.inSyncStatus,
                        product.dtLastSyncDate,
                        product.inErrorRetry,
                        product.flgIsActive,
                        product.flgIsDeleted,
                        product.inCreatedBy,
                        product.inModifiedBy,
                        product.dtModificationDate,
                        product.dtCreationDate,
                        CAST((COUNT(product.inProductId) OVER()) AS BIGINT) AS inRecordCount
                    FROM tblProducts Product WITH (NOLOCK)
                    WHERE 1=1
                    AND product.flgIsDeleted <> 1 
                    AND flgIsHistoricItem <> 1 AND (product.inCompanyId = 1)   )

    SELECT  P.inProductId,
            P.inCategoryId,
            P.stCategory,
            P.stManufacturers,
            P.stManufacturerPartReference,
            P.inCompanyId,
            P.stERPId,
            P.stProductName,
            P.stProductNumber,
            P.stModel,
            P.stFileLink,
            P.stImage,
            P.dcPrice,
            P.dcStandardPrice,
            P.dcOnHandQty,
            P.dcQtyOnPO,
            P.dtEstimatedShipDate,
            P.dcWeight,
            P.inSyncStatus,
            P.dtLastSyncDate,
            P.inErrorRetry,
            P.flgIsActive,
            P.flgIsDeleted,
            P.inCreatedBy,
            P.inModifiedBy,
            P.dtModificationDate,
            P.dtCreationDate,
            P.inRecordCount
    FROM ProductsCTE P
    ORDER BY stCategory ASC
    OFFSET (1 - 1) * 1000 ROWS
    FETCH NEXT 1000 ROWS ONLY;

我想在喜欢以下内容的子句中使用stManufacturerPartReference。

WITH ProductsCTE (inProductId, inCategoryId, stCategory, stManufacturers, inCompanyId, stERPId,stManufacturerPartReference,
                                stProductName, stProductNumber, stModel, stFileLink, stImage, dcPrice,dcStandardPrice, dcOnHandQty,dcQtyOnPO,dtEstimatedShipDate, dcWeight, inSyncStatus, dtLastSyncDate,
                                inErrorRetry,flgIsActive, flgIsDeleted, inCreatedBy, inModifiedBy, dtModificationDate, dtCreationDate, inRecordCount)
                AS (
                    SELECT
                        product.inProductId,
                        product.inCategoryId,
                        product.stCategory,
                        product.stManufacturers,
                        product.inCompanyId,
                        product.stERPId,
                        product.stProductName,
                        STUFF((SELECT ', ' + PM.stManufacturerPartReference
                           FROM tblProductManufacturers PM
                           JOIN tblProducts Product on PM.inProductId = Product.inProductId
                           JOIN tblManufacturers M on M.inManufacturerId = PM.inManufacturerId
                           WHERE PM.inProductId=product.inProductId
                           ORDER BY M.stManufacturer
                           FOR XML PATH('')), 1, 1, '') as stManufacturerPartReference,
                        product.stProductNumber,
                        product.stModel,
                        product.stFileLink,
                        product.stImage,
                        product.dcPrice,
                        product.dcStandardPrice,
                        product.dcOnHandQty,
                        product.dcQtyOnPO,
                        product.dtEstimatedShipDate,
                        product.dcWeight,
                        product.inSyncStatus,
                        product.dtLastSyncDate,
                        product.inErrorRetry,
                        product.flgIsActive,
                        product.flgIsDeleted,
                        product.inCreatedBy,
                        product.inModifiedBy,
                        product.dtModificationDate,
                        product.dtCreationDate,
                        CAST((COUNT(product.inProductId) OVER()) AS BIGINT) AS inRecordCount
                    FROM tblProducts Product WITH (NOLOCK)
                    WHERE 1=1
                    AND product.flgIsDeleted <> 1 
                    AND flgIsHistoricItem <> 1 AND (product.inCompanyId = 1) AND  stManufacturerPartReference LIKE '%ABC DEF%'  )

    SELECT  P.inProductId,
            P.inCategoryId,
            P.stCategory,
            P.stManufacturers,
            P.stManufacturerPartReference,
            P.inCompanyId,
            P.stERPId,
            P.stProductName,
            P.stProductNumber,
            P.stModel,
            P.stFileLink,
            P.stImage,
            P.dcPrice,
            P.dcStandardPrice,
            P.dcOnHandQty,
            P.dcQtyOnPO,
            P.dtEstimatedShipDate,
            P.dcWeight,
            P.inSyncStatus,
            P.dtLastSyncDate,
            P.inErrorRetry,
            P.flgIsActive,
            P.flgIsDeleted,
            P.inCreatedBy,
            P.inModifiedBy,
            P.dtModificationDate,
            P.dtCreationDate,
            P.inRecordCount
    FROM ProductsCTE P
    ORDER BY stCategory ASC
    OFFSET (1 - 1) * 1000 ROWS
    FETCH NEXT 1000 ROWS ONLY;

但是它给我错误“无效的列名'stManufacturerPartReference'。” 那么我该如何在where子句中使用别名呢? 谢谢。

3 个答案:

答案 0 :(得分:1)

我会改为:

SELECT *, STUFF(stManufacturerPartReference, 1, 1, '') AS stManufacturerPartReference
FROM . . . . 
     . . . . CROSS APPLY
     ( SELECT ', ' + PM.stManufacturerPartReference
       FROM tblProductManufacturers PM JOIN 
            tblProducts Product 
            ON PM.inProductId = Product.inProductId JOIN 
            tblManufacturers M 
            ON M.inManufacturerId = PM.inManufacturerId
       WHERE PM.inProductId=product.inProductId
       FOR XML PATH('') 
     ) tt(stManufacturerPartReference)
WHERE . . . AND
     stManufacturerPartReference LIKE '%ABC DEF%';

答案 1 :(得分:0)

您需要了解SQL查询中的执行顺序:https://sqlbolt.com/lesson/select_queries_order_of_execution

WHERE出现在FROM后面,因此任何别名都无法过滤。

由于包裹在CTE中,因此在查询后对stManufacturerPartReference进行过滤。

WITH ProductsCTE (inProductId, inCategoryId, stCategory, stManufacturers, inCompanyId, stERPId,stManufacturerPartReference,
                                stProductName, stProductNumber, stModel, stFileLink, stImage, dcPrice,dcStandardPrice, dcOnHandQty,dcQtyOnPO,dtEstimatedShipDate, dcWeight, inSyncStatus, dtLastSyncDate,
                                inErrorRetry,flgIsActive, flgIsDeleted, inCreatedBy, inModifiedBy, dtModificationDate, dtCreationDate, inRecordCount)
                AS (
                    SELECT
                        product.inProductId,
                        product.inCategoryId,
                        product.stCategory,
                        product.stManufacturers,
                        product.inCompanyId,
                        product.stERPId,
                        product.stProductName,
                        STUFF((SELECT ', ' + PM.stManufacturerPartReference
                           FROM tblProductManufacturers PM
                           JOIN tblProducts Product on PM.inProductId = Product.inProductId
                           JOIN tblManufacturers M on M.inManufacturerId = PM.inManufacturerId
                           WHERE PM.inProductId=product.inProductId
                           ORDER BY M.stManufacturer
                           FOR XML PATH('')), 1, 1, '') as stManufacturerPartReference,
                        product.stProductNumber,
                        product.stModel,
                        product.stFileLink,
                        product.stImage,
                        product.dcPrice,
                        product.dcStandardPrice,
                        product.dcOnHandQty,
                        product.dcQtyOnPO,
                        product.dtEstimatedShipDate,
                        product.dcWeight,
                        product.inSyncStatus,
                        product.dtLastSyncDate,
                        product.inErrorRetry,
                        product.flgIsActive,
                        product.flgIsDeleted,
                        product.inCreatedBy,
                        product.inModifiedBy,
                        product.dtModificationDate,
                        product.dtCreationDate,
                        CAST((COUNT(product.inProductId) OVER()) AS BIGINT) AS inRecordCount
                    FROM tblProducts Product WITH (NOLOCK)
                    WHERE 1=1
                    AND product.flgIsDeleted  1 
                    AND flgIsHistoricItem  1 AND (product.inCompanyId = 1)   )

    SELECT  P.inProductId,
            P.inCategoryId,
            P.stCategory,
            P.stManufacturers,
            P.stManufacturerPartReference,
            P.inCompanyId,
            P.stERPId,
            P.stProductName,
            P.stProductNumber,
            P.stModel,
            P.stFileLink,
            P.stImage,
            P.dcPrice,
            P.dcStandardPrice,
            P.dcOnHandQty,
            P.dcQtyOnPO,
            P.dtEstimatedShipDate,
            P.dcWeight,
            P.inSyncStatus,
            P.dtLastSyncDate,
            P.inErrorRetry,
            P.flgIsActive,
            P.flgIsDeleted,
            P.inCreatedBy,
            P.inModifiedBy,
            P.dtModificationDate,
            P.dtCreationDate,
            P.inRecordCount
    FROM ProductsCTE P
    WHERE P.stManufacturerPartReference LIKE '%ABC DEF%'
    ORDER BY stCategory ASC
    OFFSET (1 - 1) * 1000 ROWS
    FETCH NEXT 1000 ROWS ONLY;

答案 2 :(得分:0)

您必须使用外部应用或交叉应用(选择.. AS stManufacturerPartReference),如果服务器为2017,则可以使用String_AGG函数获取值列表,而不是xml子句。