【问题标题】:SQL Compare and Unity tableSQL 比较和统一表
【发布时间】:2014-05-28 09:14:33
【问题描述】:

我有 2 张桌子,当他们统一时我会遇到问题 告诉你

表 1

 Customer | Buyer | Usage | Item Code | Item Name | Month1 | Month2 

   A      Jirulu  Bottom    111111     Item1        100      50          
   B      Bakeyo  Top       122222     Item2        100      50        
   D      Sagero  Bottom    133333     Item3        100      50        

表 2

Customer | Buyer | Usage | Item Code | Item Name | Month3 | Month4 | 

   A      Jirulu  Bottom    111111     Item1        100      50      
   C      Bakeyo  Top       122222     Item2        100      50      
   D      Sagero  Bottom    133333     Item3        100      50           

我怎样才能得到这样的结果:

Customer | Buyer | Usage | Item Code | Item Name | Month1 | Month2| Month3 | Month4 | 

   A      Jirulu  Bottom    111111     Item1        100      50      100      50    
   B      Bakeyo  Top       122222     Item2        100      50      100      50        
   C      Bakeyo  Top       122222     Item2         0        0      100      50
   D      Sagero  Bottom    133333     Item3        100      50      100      50   

请指教,谢谢!

【问题讨论】:

  • 请展示您尝试过的内容以及您使用的是哪个 RDBMS?
  • 我正在使用 SQL Server 2012 我已经尝试过 UNION ALL ,但到目前为止没有结果

标签: sql sql-server join sql-server-2012


【解决方案1】:

下面的查询会帮助你。

SELECT tab.customer, 
       tab.buyer, 
       tab.usage, 
       tab.usage, 
       tab.itemcode, 
       tab.itemname, 
       Sum(month1) AS Month1, 
       Sum(month2) AS Month2, 
       Sum(month3) AS Month3, 
       Sum(month4) AS Month4 
FROM   (SELECT customer, 
           buyer, 
           usage, 
           itemcode, 
           itemname, 
           month1, 
           month2, 
           0 AS month3, 
           0 AS month4 
    FROM   table1 
    UNION ALL 
    SELECT customer, 
           buyer, 
           usage, 
           itemcode, 
           itemname, 
           0 AS month1, 
           0 AS month2, 
           month3, 
           month4 
    FROM   table2) tab 
GROUP  BY tab.customer, 
      tab.buyer, 
      tab.usage, 
      tab.usage, 
      tab.itemcode, 
      tab.itemname 

【讨论】:

    【解决方案2】:

    除了已经显示的UNION,还可以JOIN 两个表,在这种情况下,两个表中有不同的客户FULL JOIN

    SELECT Coalesce(t1.[Customer], t2.[Customer]) [Customer]
         , Coalesce(t1.[Buyer], t2.[Buyer]) [Buyer]
         , Coalesce(t1.[Usage], t2.[Usage]) [Usage]
         , Coalesce(t1.[Item Code], t2.[Item Code]) [Item Code]
         , Coalesce(t1.[Item Name], t2.[Item Name]) [Item Name]
         , Coalesce([Month1], 0) [Month1]
         , Coalesce([Month2], 0) [Month2]
         , Coalesce([Month3], 0) [Month3]
         , Coalesce([Month4], 0) [Month4]
    FROM   Table1 t1
           FULL JOIN Table2 t2 ON t1.Customer = t2.Customer 
                              and t1.Buyer = t2.Buyer 
                              and t1.[usage] = t2.[usage]
                              and t1.[Item Code] = t2.[Item Code]
                              and t1.[Item Name] = t2.[Item Name]
    ORDER BY Coalesce(t1.[Customer], t2.[Customer])
    

    附带说明,如果您有垂直分区,那么如果您创建一个键来加入较短的分区会更好,例如,您可以划分键部分并添加一个假 ID 以与另一个相关分区之类的东西

    ID | Customer | Buyer | Usage | Item Code | Item Name
    
    ID | Month1 | Month2
    
    ID | Month3 | Month4
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多