SQL Server 2014合并表避免重复

时间:2016-06-05 16:05:41

标签: sql-server

我有36个Sales表,每个表都指向一个商店:

st1.dbo.Sales  
st2.dbo.Sales  
...  
st35.dbo.Sales  
st36.dbo.Sales 

每条记录都有以下关键列:

UserName, PostalCode, Location, Country, InvoiceAmount, ItemsCount, StoreID

这是SQLFiddle

我需要将Customers

中尚未出现的所有用户名(及其详细信息)复制到Customers表中

如果重复,则需要使用InvoiceAmount为MAX

的记录字段

我尝试构建一个查询,但看起来太复杂了,这也是错误的,因为在CROSS APPLY中应该考虑Sales表的完整列表

INSERT INTO Customers (.....)
    SELECT distinct 
        d.UserName, 
        w.postalCode, 
        w.location, 
        W.country,  
        max(w.invoiceamount) invoiceamount, 
        max(w.itemscount) itemscount,
        w.storeID
    FROM 
        (SELECT * FROM st1.dbo.Sales
         UNION              
         SELECT * FROM st2.dbo.Sales
         UNION
         ...
         SELECT * FROM st36.dbo.Sales) d
    LEFT JOIN 
        G.dbo.Customers s ON d.Username = s.UserName
    CROSS APPLY 
        (SELECT TOP (1) * 
         FROM s.dbo.[Sales]  
         WHERE d.Username=w.Username
         ORDER BY InvoiceAmount DESC) w
    WHERE 
        s.UserName IS NULL
        AND d.username IS NOT NULL
    GROUP BY 
        d.UserName, w.postalCode, w.location,    
        w.country, w.storeID

有人可以提供一些提示吗?

3 个答案:

答案 0 :(得分:1)

作为基本的SQL查询,我在内部子查询中创建一个row_number,然后加入客户,然后隔离不在客户表中的每个客户的最大发票编号。

INSERT INTO Customers (.....)
    SELECT w.UserName, 
        w.postalCode, 
        w.location, 
        w.country,  
        w.invoiceamount, 
        w.itemscount,
        w.storeID
    FROM (select d.*,
              row_number() over(partition by d.Username order by d.invoiceamount desc) rownumber 
          from  (SELECT * 
                 FROM st1.dbo.Sales
                 UNION              
                 SELECT * 
                 FROM st2.dbo.Sales
                 UNION
                 ...
                 SELECT * 
                 FROM st36.dbo.Sales
                ) d 
           LEFT JOIN G.dbo.Customers s 
               ON d.Username = s.UserName
           WHERE s.UserName IS NULL
               AND d.username IS NOT NULL
         ) w
    where w.rownumber = 1

答案 1 :(得分:0)

使用您的小提琴,这将选择具有最大invoiceamount

的不同用户名行
with d as(
  SELECT * FROM Sales
  UNION              
  SELECT * FROM Sales2
)
select *
from ( select *, 
      rn = row_number() over(partition by Username order by invoiceamount desc)
      from d) dd
where rn=1;

答案 2 :(得分:0)

第1步 - 使用cte。

                select  username , invoiceamount ,itemscount from Sales
          UNION all
               select  user name , invoiceamount ,itemscount from Sales

.....

...

第2步

next cte使用group by并获取max invoiceamount,itemscount为最后结果集的用户。

         ,cte2 as (

      select  user name , max (invoiceamount) as invoiceamount  ,max(itemscount) as itemscount  from  cte)

第3步

使用左连接和用户表,找到缺少的记录和itemscount invoiceamount