使用子查询作为使用'其中'中的另一列的列。条款

时间:2016-03-16 20:04:24

标签: sql sql-server subquery ssms

我可能搞砸了那个头衔!所以我有一个名为" 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

1 个答案:

答案 0 :(得分:0)

您希望join在表carddet上使用嵌套选择。我推断RCPIDcarddet与您的主要表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/