我试图编写一个查找重复项的查询。该查询将查看最新的 2013 年订单日期,然后与最新的 2014 年订单匹配,以查找两个年份中都存在的记录。我的代码只给了我 2013 年的数据,而不是 2013 年和 2014 年的数据。所以我想显示这两年的重复数据。
例如下面是示例数据
Bill_Member_ID........Lname....................Order_Date
123..........................Smith...............01/05/2013
123..........................Smith...............02/15/2014
123..........................Smith...............02/18/2014
456..........................Jones...............01/07/2013
789..........................Brown...............01/05/2013
789..........................Brown...............02/17/2014
789..........................Brown...............03/17/2014
992..........................White...............03/15/2013
992..........................White...............01/05/2014
那么我应该得到一个回报
Bill_Member_ID........Lname....................Order_Date
123..........................Smith...............01/05/2013
123..........................Smith...............02/18/2014
789..........................Brown...............01/05/2013
789..........................Brown...............03/17/2014
992..........................White...............03/15/2013
992..........................White...............01/05/2014
这是我的代码
SELECT
OMFMC.BILL_MASTER_CUSTOMER_ID,
OMFMC.BILL_FIRST_NAME,
OMFMC.BILL_LAST_NAME,
OMFMC.BILL_LABEL_NAME,
OMFMC.BILL_PRIMARY_EMAIL_ADDRESS,
OMFMC.BILL_ADDRESS_1,
OMFMC.BILL_ADDRESS_2,
OMFMC.BILL_CITY,
OMFMC.BILL_STATE,
OMFMC.BILL_POSTAL_CODE,
CMI.NATIONAL_LEVEL2,
MAX(OMFMC.ORDER_DATE),
OMFMC.ORDER_DATE,
FT.PAYMENT_AMOUNT,
SUM(FT.PAYMENT_AMOUNT)as SUM
FROM
CUSTOMER CUSTOMER_IN_TRIBUTE_TO
RIGHT OUTER JOIN ORDER_FND_DETAIL OFD
ON (CUSTOMER_IN_TRIBUTE_TO.MASTER_CUSTOMER_ID=OFD.IN_TRIBUTE_TO_MAST_CUST
and CUSTOMER_IN_TRIBUTE_TO.SUB_CUSTOMER_ID=OFD.IN_TRIBUTE_TO_SUB_CUST)
RIGHT OUTER JOIN ORDER_MBR_FND_MTG_CUS_INFO_VW OMFMC
ON (OMFMC.ORDER_NO=OFD.ORDER_NO
and OMFMC.ORDER_LINE_NO = OFD.ORDER_LINE_NO )
LEFT OUTER JOIN CUS_CURRENT_MEMBERSHIP_INFO CMI
ON (CMI.MASTER_CUSTOMER_ID=OMFMC.BILL_MASTER_CUSTOMER_ID
and CMI.SUB_CUSTOMER_ID=OMFMC.BILL_SUB_CUSTOMER_ID)
LEFT OUTER JOIN FAR_TXN FT
ON (FT.ORDER_NO=OMFMC.ORDER_NO
and FT.ORDER_LINE_NO=OMFMC.ORDER_LINE_NO)
WHERE
OMFMC.ORDER_STATUS_CODE='A'
AND OMFMC.LINE_STATUS_CODE = 'A'
AND OMFMC.ORDER_STATUS_CODE = 'A'
AND OMFMC.LINE_STATUS_CODE = 'A'
AND OMFMC.BILL_CUSTOMER_CLASS_CODE Not IN ( 'TEST_MBR','STAFF' )
AND FUND in ('FOSFN' , 'MFUND')
and(DATEPART(year, OMFMC.ORDER_DATE) ='2013')
and OMFMC.BILL_MASTER_CUSTOMER_ID In (
(select OMFMC.BILL_MASTER_CUSTOMER_ID
From ORDER_MBR_FND_MTG_CUS_INFO_VW OMFMC
where (DATEPART(year, OMFMC.ORDER_DATE) ='2014'))
)
GROUP BY
OMFMC.BILL_LABEL_NAME,
OMFMC.BILL_FIRST_NAME,
OMFMC.BILL_LAST_NAME,
OMFMC.ORDER_DATE,
OMFMC.CAMPAIGN,
OMFMC.FUND,
OMFMC.PRODUCT_CODE,
OMFMC.BILL_MASTER_CUSTOMER_ID,
OMFMC.BILL_COMPANY_NAME,
OMFMC.BILL_COUNTRY_DESCR,
OMFMC.BILL_LAST_FIRST_NAME,
OMFMC.ORDER_NO,
OMFMC.COMMENTS,
year(OMFMC.ORDER_DATE),
month(OMFMC.ORDER_DATE),
OMFMC.BILL_COUNTRY_CODE,
CMI.NATIONAL_LEVEL2,
CMI.NATIONAL_SINCE_DATE,
CMI.CYCLE_END_DATE,
OMFMC.BILL_FORMATTED_DETAIL + OMFMC.BILL_FORMATTED_ADDRESS,
OMFMC.BILL_PRIMARY_EMAIL_ADDRESS,
OMFMC.BILL_ADDRESS_1,
OMFMC.BILL_CITY,
OMFMC.BILL_STATE,
OMFMC.BILL_POSTAL_CODE,
OMFMC.BILL_ADDRESS_2,
OMFMC.BILL_ADDRESS_3,
OMFMC.BILL_ADDRESS_4,
OFD.IN_TRIBUTE_TO_DESCR,
CUSTOMER_IN_TRIBUTE_TO.LABEL_NAME,
OFD.TRIBUTE_TYPE_CODE,
OMFMC.MARKET_CODE,
OMFMC.BILL_JOB_TITLE,
FT.PAYMENT_AMOUNT
HAVING SUM(FT.PAYMENT_AMOUNT) < 0
Order By BILL_MASTER_CUSTOMER_ID