使用子查询作为在 'where' 子句中使用另一列的列

using a subquery as a column that's using another column in the 'where' clause

我可能搞砸了那个标题!所以我有一个名为 "programID" 的列,我想要另一列告诉我使用 programID 下订单的最近日期。我想我需要一个子列,但问题是如何使用该行的 programID 使其与同一行的 programID 相匹配?

     DECLARE @ClientIDs varchar(4) = 6653;
    DECLARE @ProgramStatus char(1) = 'Y'; -- Y, N, or R

SELECT
         rcp.RCPID AS ProgramID
       , rcp.RCPName AS Program
       , rcp.RCPActive AS ProgramStatus
       , aa.AACustomCardInternalReview AS VCCP
       , aa.AAJobNo AS AAJobNo
       , aas.AASName AS AAStatus
       , rcp.RCPOpsApproved AS OpsApproved
       , clt.CltID AS ClientID
       , rcp.RCPSignatureRequired AS SignatureRequired
       , st.STEnumValue AS DefaultShipType
       , rcp.RCPShipMethodOverrideType AS ShipTypeOverride
       ,aa.AANetworkProgramID
       ,(Select max(cdconfirmationdate) from carddet where ) --can't figure this part out
FROM
       RetailerCardProgram rcp WITH (NOLOCK)
       INNER JOIN ClientRetailerMap crm WITH (NOLOCK)
              ON crm.CRMRetailerID = rcp.RCPRetailerID
       INNER JOIN Client clt WITH(NOLOCK)
              ON clt.CltID = crm.CRMCltID
       LEFT JOIN AssociationApproval aa WITH (NOLOCK)
              ON  aa.AARetailerID = rcp.RCPRetailerID
              AND aa.AABin = rcp.RCPBin6
              AND aa.AAFrontOfPlasticTemplateID = rcp.RCPFOCTemplateID
              AND aa.AABackOfPlasticTemplateID = rcp.RCPBOCTemplateID
              AND ISNULL(aa.AACardID, 0) = ISNULL(rcp.RCPDefaultPlasticCardID, 0)
--            AND LOWER(rcp.RCPName) NOT LIKE '%do not use%' -- Needed for AA Job Number 1594
       LEFT JOIN AssociationApprovalStatus aas WITH (NOLOCK)
              ON aas.AASID = aa.AAAssociationApprovalStatusID
       LEFT JOIN OpenLoopAssociation ola WITH (NOLOCK)
              ON ola.OLAID=rcp.RCPOLAID
       LEFT JOIN ClientCardProgramMap ccpm WITH (NOLOCK)
              ON  ccpm.CCPMCardProgramID = rcp.RCPID
              AND ccpm.CCPMClientID = clt.CltID
       LEFT JOIN TippingModule tm WITH (NOLOCK)
              ON tm.TMid = rcp.RCPTippingModuleID
       LEFT JOIN GiftCardTemplate fgt WITH (NOLOCK)
              ON  fgt.gtid = rcp.RCPFOCTemplateID
              AND fgt.GTPage='P'
       LEFT JOIN GiftCardTemplate bgt WITH (NOLOCK)
              ON bgt.gtid = rcp.RCPBOCTemplateID
              AND bgt.GTPage='PB'
       LEFT JOIN Card c WITH (NOLOCK)
              ON c.CardID = rcp.RCPDefaultCarrierID
       LEFT JOIN CardType ct WITH (NOLOCK)
              ON ct.CTID = c.CardTypeID
       LEFT JOIN RetailerCardProgramTCSheetMap rtm1 WITH (NOLOCK)
              ON  rtm1.RTMRCPID = rcp.RCPID
              AND rtm1.RTMInsertOrder = 1
       LEFT JOIN RetailerCardProgramTCSheetMap rtm2 WITH (NOLOCK)
              ON  rtm2.RTMRCPID = rcp.RCPID
              AND rtm2.RTMInsertOrder = 2
       LEFT JOIN RetailerCardProgramTCSheetMap rtm3 WITH (NOLOCK)
              ON  rtm3.RTMRCPID = rcp.RCPID
              AND rtm3.RTMInsertOrder = 3
       LEFT JOIN RetailerCardProgramTCSheetMap rtm4 WITH (NOLOCK)
              ON  rtm4.RTMRCPID = rcp.RCPID
              AND rtm4.RTMInsertOrder = 4
       LEFT JOIN TCSheet i1 WITH (NOLOCK)
              ON i1.TCSID = rtm1.RTMTCSID
       LEFT JOIN TCSheet i2 WITH (NOLOCK)
              ON i2.TCSID = rtm2.RTMTCSID
       LEFT JOIN TCSheet i3 WITH (NOLOCK)
              ON i3.TCSID = rtm3.RTMTCSID
       LEFT JOIN TCSheet i4 WITH (NOLOCK)
              ON i4.TCSID = rtm4.RTMTCSID
       LEFT JOIN ShipType st WITH (NOLOCK)
              ON st.STId = rcp.RCPDefaultShipTypeID
WHERE
       clt.CltID IN (@ClientIDs) -- 6653 and 6657.
       AND rcp.RCPActive IN (@ProgramStatus)
ORDER BY
         AAJobNo
       , Program

您想 join 在 table carddet 上嵌套 select。我推断 RCPIDcarddet 和你的主要 table RetainerCardProgram...

之间的关系
SELECT     rcp.RCPID AS ProgramID,
           date.MAXDATE AS MaxDate,
           rest of your columns...

FROM       RetailerCardProgram rcp WITH (NOLOCK)
INNER JOIN (
            SELECT RCPID, MAX(cdconfirmationdate) as 'MAXDATE'
            FROM   carddet
            GROUP BY RCPID
           ) date on date.RCPID = rcp.RCPID

rest of query...

如果不是所有 ID 在 carddet 中都有日期,您可能需要 left join

强制补充:还有你对NOLOCK的使用有点恐怖。参见 http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/