SQl服务器表中的重复记录不同年份

时间:2015-01-13 15:09:43

标签: sql sql-server tsql duplicates qsqlquery

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

答案 0 :(得分: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来定义它。

答案 1 :(得分:0)

忽略您的查询并专注于您的示例集以简化一点,您可以使用ROW_NUMBER()COUNT() 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添加了每个{{1}}多少年的计数有记录。

更新:我最初并没有想到它仅限于2年,并认为任何多年的情况都是理想的输出。因此,鉴于只有2年的要求,戈登的方法更简单,如果您需要考虑某人在其他年份有订单的地方,您可以使用此答案。

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

答案 3 :(得分:0)

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