0

我试图编写一个查找重复项的查询。该查询将查看最新的 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
4

4 回答 4

2

如果您在 SQL Server 中执行此操作,则可以使用窗口函数:

select s.*
from (select s.*,
             min(year(order_date) over (partition by Bill_Member_ID) as minyear,
             max(year(order_date) over (partition by Bill_Member_ID) as maxyear,
             row_number() over (partition by Bill_Member_ID, year(order_date) order by order_date desc) as seqnum
      from sample s
      where year(order_date) in (2013, 2014)
     ) s
where minyear <> maxyear and seqnum = 1;

子查询获取每个成员的最小和最大年份,并为每年的记录分配一个序号。

外部where验证有两年(where minyear = 2013 and maxyear = 2014如果你愿意,写起来会更明确)并从每年中选择一个记录。

我不确定sample与您拥有的表格有什么关系。您可以轻松地使用子查询或 CTE 来定义它。

于 2015-01-13T15:24:31.570 回答
0

您还可以尝试临时表或 CTE 比较。

于 2015-02-11T05:22:26.603 回答
0

忽略您的查询并专注于您的样本集以简化一点,您可以使用 withROW_NUMBER()和 a COUNT()with几个步骤获得所需的输出OVER()

;with cte AS (SELECT *,ROW_NUMBER() OVER(PARTITION BY Bill_Member_ID,YEAR(Order_Date) ORDER BY Order_Date DESC) AS RN
              FROM YourTable
              WHERE YEAR(Order_Date) IN (2013,2014)
             )
     ,cte2 AS (SELECT *,COUNT() OVER(PARTITION BY Bill_Member_ID) AS Yr_CT
               FROM cte
               WHERE RN = 1
              )
SELECT Bill_Member_ID,Lname,Order_Date 
FROM cte2
WHERE Yr_CT > 1
ORDER BY Bill_Member_ID,Order_Date

ROW_NUMBER()函数为每条记录添加一个数字,从PARTITION BY(可选)中定义的每个组开始,并根据ORDER BY(必需)排序。

添加OVER()COUNT()允许您通过分组获得计数,而不会像使用GROUP BY.

因此,第一个cte添加了一个有用的数字,可用于过滤到Order_Date每个 每年的最新Bill_Memmber_ID数据,第二个添加一个计数每个有多少年Bill_Member_ID的记录。

更新:我最初并没有考虑将其限制为 2 年,而是将任何多年的情况视为所需的输出。因此,鉴于只有 2 年的要求,Gordon 的方法更简单,如果您需要考虑某人在其他几年内有订单的地方,您可以使用这个答案。

于 2015-01-13T15:22:05.743 回答
0
select s1.id, s1.name, s1.orderDate as [date 2013], s2.orderDate as [date 2014] 
from 
(  select id, name, max(orderDate) as [orderDate]
     from cusOrder 
    where DatePart(yy, orderDate) = '2013' 
    group by id, name) as s1 
join 
(  select id, name, max(orderDate) as [orderDate]
     from cusOrder 
    where DatePart(yy, orderDate) = '2014' 
    group by id, name) as s2  
on s1.id = s2.id
于 2015-01-13T15:33:18.897 回答