0

如何Max(OD.CYCLE_END_DATE)从下面的查询中获取其他列?

    SELECT   C.MASTER_CUSTOMER_ID
            ,C.USR_SPE_Membership_Status
            ,OD.PRODUCT_CODE
            ,OD.CYCLE_BEGIN_DATE
            ,OD.CYCLE_END_DATE
            ,LINE_STATUS_CODE
FROM CUSTOMER C
LEFT OUTER JOIN ORDER_DETAIL OD
      ON C.MASTER_CUSTOMER_ID = OD.SHIP_MASTER_CUSTOMER_ID
      AND C.SUB_CUSTOMER_ID = OD.SHIP_SUB_CUSTOMER_ID
      AND OD.PRODUCT_CODE in ('PROFESSIONAL','LIFE','STUDENT','STAFF')
     --AND OD.CYCLE_BEGIN_DATE <= GETDATE()
     AND OD.CYCLE_END_DATE >= GETDATE()

where C.MASTER_CUSTOMER_ID= '3420065'

结果

MASTER_CUSTOMER_ID  USR_SPE_Membership_Status   PRODUCT_CODE    CYCLE_BEGIN_DATE    CYCLE_END_DATE  LINE_STATUS_CODE
3420065             Paid Member                 LIFE            2013-01-01          2013-12-31          A
3420065             Paid Member                 LIFE            2014-01-01          2014-12-31          A

我需要2014-12-31行。

4

5 回答 5

2

你有选择,

你可以取你不想取的MAX()和字段,GROUP BYMAX()

或者您可以使用以下ROW_NUMBER()功能:

WITH    cte
      AS ( SELECT   C.MASTER_CUSTOMER_ID ,
                    C.USR_SPE_Membership_Status ,
                    OD.PRODUCT_CODE ,
                    OD.CYCLE_BEGIN_DATE ,
                    OD.CYCLE_END_DATE ,
                    LINE_STATUS_CODE ,
                    ROW_NUMBER() OVER ( PARTITION BY C.MASTER_CUSTOMER_ID ORDER BY OD.CYCLE_END_DATE DESC ) RN
           FROM     CUSTOMER C
                    LEFT OUTER JOIN ORDER_DETAIL OD ON C.MASTER_CUSTOMER_ID = OD.SHIP_MASTER_CUSTOMER_ID
                                                       AND C.SUB_CUSTOMER_ID = OD.SHIP_SUB_CUSTOMER_ID
                                                       AND OD.PRODUCT_CODE IN (
                                                       'PROFESSIONAL',
                                                       'LIFE', 'STUDENT',
                                                       'STAFF' )
                     --AND OD.CYCLE_BEGIN_DATE <= GETDATE()
                                                       AND OD.CYCLE_END_DATE >= GETDATE()
           WHERE    C.MASTER_CUSTOMER_ID = '3420065'
         )
SELECT  *
FROM    cte
WHERE   RN = 1

该 ROW_NUMBER() 函数为每一行分配一个数字。 

PARTITION BY 是可选的,但用于为该组中的每个值开始编号,

即:如果您PARTITION BY MASTER_CUSTOMER_ID 随后为每个唯一的客户 ID 值编号,则编号将从 1 开始。

ORDER BY当然是用来定义计数应该如何进行的,

并且在函数中是必需的ROW_NUMBER()

于 2013-10-23T19:56:22.397 回答
0
SELECT *
FROM
(
    SELECT   C.MASTER_CUSTOMER_ID
            ,C.USR_SPE_Membership_Status
            ,OD.PRODUCT_CODE
            ,OD.CYCLE_BEGIN_DATE
            ,OD.CYCLE_END_DATE
            ,LINE_STATUS_CODE,
             ROW_NUMBER() OVER (PARTITION BY C.MASTER_CUSTOMER_ID ORDER BY CYCLE_BEGIN_DATE DESC) AS [number]
FROM CUSTOMER C
LEFT OUTER JOIN ORDER_DETAIL OD
      ON C.MASTER_CUSTOMER_ID = OD.SHIP_MASTER_CUSTOMER_ID
      AND C.SUB_CUSTOMER_ID = OD.SHIP_SUB_CUSTOMER_ID
      AND OD.PRODUCT_CODE in ('PROFESSIONAL','LIFE','STUDENT','STAFF')
     --AND OD.CYCLE_BEGIN_DATE <= GETDATE()
     AND OD.CYCLE_END_DATE >= GETDATE()

where C.MASTER_CUSTOMER_ID= '3420065'
) A 
WHERE A.[number] = 1
于 2013-10-23T19:56:30.280 回答
0

您可以有一个返回一个值的子查询SELECT

SELECT   C.MASTER_CUSTOMER_ID
        ,C.USR_SPE_Membership_Status
        ,OD.PRODUCT_CODE
        ,OD.CYCLE_BEGIN_DATE
        ,OD.CYCLE_END_DATE
        ,LINE_STATUS_CODE
        ,(SELECT MAX(CYCLE_END_DATE) 
             FROM ORDER_DETAIL 
             WHERE PRODUCT_CODE in ('PROFESSIONAL','LIFE','STUDENT','STAFF')
                 AND CYCLE_END_DATE >= GETDATE()
                 AND C.MASTER_CUSTOMER_ID = SHIP_MASTER_CUSTOMER_ID
                 AND C.SUB_CUSTOMER_ID = SHIP_SUB_CUSTOMER_ID
            ) MAX_CYCLE_END_DATE
FROM ...             
于 2013-10-23T19:57:34.277 回答
0

为了在使用时从其他列返回数据,MAX()您必须将它们包含在其他聚合函数或GROUP BY将在您的WHEREcaluse之后添加的子句中

于 2013-10-23T19:55:08.993 回答
0

您可以使用交叉应用:

SELECT   C.MASTER_CUSTOMER_ID
            ,C.USR_SPE_Membership_Status
            ,CrossApplyTable.PRODUCT_CODE
            ,CrossApplyTable.CYCLE_BEGIN_DATE
            ,CrossApplyTable.CYCLE_END_DATE
            ,LINE_STATUS_CODE
FROM CUSTOMER C
CROSS APPLY(SELECT TOP 1 * 
            FROM ORDER_DETAIL OD
            WHERE C.MASTER_CUSTOMER_ID = OD.SHIP_MASTER_CUSTOMER_ID
            AND C.SUB_CUSTOMER_ID = OD.SHIP_SUB_CUSTOMER_ID
            AND OD.PRODUCT_CODE in ('PROFESSIONAL','LIFE','STUDENT','STAFF')
     --     AND OD.CYCLE_BEGIN_DATE <= GETDATE()
            AND OD.CYCLE_END_DATE >= GETDATE() 
            ORDER BY CYCLE_END_DATE DESC) CrossApplyTable

where C.MASTER_CUSTOMER_ID = '3420065'
于 2013-10-23T19:58:01.480 回答