如何通过在sql中连接两个表来删除select语句中的重复项

时间:2016-11-07 08:53:15

标签: sql-server-2008 sap crystal-reports-2008

美好的一天,我正在使用此查询,我想从两个不同的行中删除重复的数据,所以例如我从这里有这个记录查询结果:

SELECT DISTINCT 
T9.SlpName, T1.CardName [Customer Name],T1.DocNum [SO No.], 
T1.DocDate [SO Date],T1.DocTotal [SO Total], T3.DocNum [Delivery Doc Num], 
T5.DocNum [TRA No], T5.DocDate [TRA Date],T5.DocTotal [TRA Total],
T5.GrosProfit [Gross Profit]
FROM RDR1 T0  INNER JOIN ORDR T1 ON T0.DocEntry = T1.DocEntry  
    left outer join DLN1 T2 on T2.BaseEntry = T0.DocEntry  
    left outer join ODLN T3 on T2.DocEntry = T3.DocEntry  
    left Outer join INV1 T4 on T4.BaseEntry = T3.DocEntry and T4.BaseLine = T2.Linenum  and T4.BaseType = 15  
                   OR (T4.Basetype=17 and T4.BaseEntry=T0.DocEntry and T4.BaseLine=T0.LineNum)  
            left outer join OINV T5 on T5.DocEntry = T4.DocEntry  
            left outer join OSLP T9 on T9.SlpCode = T1.SlpCode  
            WHERE T1.DocDate BETWEEN '10.01.16' AND '10.27.16' AND T1.CardCode='C-ACQUA TECH'
Group by T9.SlpName, T1.[CardName], T1.[DocNum], T1.[DocDate], T1.DocTotal,
T3.DocNum, T5.DocNum,  T5.DocDate, T5.DocTotal,T5.GrosProfit
ORDER BY  T9.SlpName,T1.CardName


Customer | SO-Date | SO-Number | SO-Amount | INV-Date | INV-Amount 
B1        10-07-16   000001      80,000.50   11-26      54,000.00
B1        10-07-16   000001      80,000.50   11-29      24,000.00

SO是在同一天发布的,但是发票日期不同所以当我做一个水晶报告时......即使它刚刚重复,SO数量也会被提升。我想要的(因为我无法在水晶报告中找到一种方法来总结重复值)是:

Customer | SO-Date | SO-Number | SO-Amount | INV-Date | INV-Amount 
B1        10-07-16   000001      80,000.50   11-26-16   54,000.00
null         null     null       null        11-29-16   24,000.00

1 个答案:

答案 0 :(得分:0)

如果某一行在您的联接的另一侧有多个加入伙伴,您将始终复制该行。您需要两个单独的查询来汇总SO-AmountINV-Amount

---编辑---

考虑这个简单的例子:我们有三个表。一个节省公司部门,一个存储部门的年度收入,一个存储这些部门的每月成本。

表1

DepartmentId | DepartmentName          | NumberEmployees
5234         | "Software Development"  | 20
3465         | "Sales"                 | 120

表2

DepartmentId | Year | Revenue
5234         | 2015 | 2,000,000
5234         | 2014 | 1,500,000

表3

DepartmentId | Year | Month | Cost
5234         | 2015 | Jan   | 120,000
5234         | 2015 | Feb   | 150,000
5234         | 2014 | Jan   | 80,000

Out out任务现在总结部门5234的总收入以及总体成本。

如果我们加入表1和表2,我们得到:

DepartmentId | DepartmentName          | NumberEmployees| Year | Revenue
5234         | "Software Development"  | 20             | 2015 | 2,000,000
5234         | "Software Development"  | 20             | 2014 | 1,500,000

通过此表,我们可以计算总收入。

如果我们加入表1和表3,我们得到:

DepartmentId | DepartmentName          | NumberEmployees | Year | Month | Cost
5234         | "Software Development"  | 20              | 2015 | Jan   | 120,000
5234         | "Software Development"  | 20              | 2015 | Feb   | 150,000
5234         | "Software Development"  | 20              | 2014 | Jan   | 80,000

使用此表,您可以计算总体成本。

你不想做的事情是加入所有3张桌子,因为你得到:

DepartmentId | DepartmentName          | NumberEmployees| Year | Revenue   | Month | Cost
5234         | "Software Development"  | 20             | 2015 | 2,000,000 | Jan   | 120,000
5234         | "Software Development"  | 20             | 2015 | 2,000,000 | Feb   | 150,000
5234         | "Software Development"  | 20             | 2014 | 1,500,000 | Jan   | 80,000

正如您所看到的,2015年的收入是重复的,因为2015年(1月和2月)的成本条目太多了。如果您使用此表来计算总收入和成本,则最终会得到错误的值。

所以要结束并解决您的问题:您应该使用两个单独的查询来计算您的聚合。