在SQL中使用其他列获取Max?

时间:2013-10-23 19:51:25

标签: sql sql-server tsql

如何使用以下查询中的其他列获取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行。

5 个答案:

答案 0 :(得分:2)

你有选择,

您可以访问不想使用MAX()的{​​{1}}和GROUP BY字段,

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

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,那么对于每个唯一的客户ID值,编号将从1开始。

PARTITION BY MASTER_CUSTOMER_ID当然用于定义计数应该如何进行,

并且在ORDER BY函数中是必需的。

答案 1 :(得分:0)

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

答案 2 :(得分: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

答案 3 :(得分: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 ...             

答案 4 :(得分:0)

您可以使用Cross Apply:

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'