SQL连接中的重复记录

时间:2013-11-21 02:37:11

标签: sql sql-server sql-server-2008

我在SQL Server数据库(2008 R2)中有三个表:Bidders,Sales和Transactions。

我有六个有销售或销售的竞标者,总共有六个销售人员。在Transaction表中共有六条记录。数据如下:

投标人表数据:

+-----------+-------------------------+
| Bidder ID |       Bidder Name       |
+-----------+-------------------------+
|       101 | Tom & Joan Bergland     |
|       103 | Jack & Sally Cook       |
|       106 | Steve & Jan Cross       |
|       109 | Cynthia Davis           |
|       122 | Arnold & Peggy Jensen   |
|       126 | Wayne & Connie Lindell  |
+-----------+-------------------------+

销售表数据:

+---------------+--------------+-------------+------------+
| Sales Counter | BidderNumber | AmountSpent | Quantity   |
+---------------+--------------+-------------+------------+
|             1 |          101 | 2600.00     |        1   |
|             2 |          106 | 90.00       |        1   |
|             3 |          122 | 65.00       |        1   |
|             4 |          103 | 353.00      |        2   |
|             5 |          126 | 2500.00     |        1   |
|             6 |          109 | 315.00      |        3   |
+---------------+--------------+-------------+------------+

交易表数据:

+--------------+------------+------------------+
| BidderNumber | AmountPaid | SalesCounter     |
+--------------+------------+------------------+
|          101 | 2600.00    |              1   |
|          103 | 500.00     |              4   |
|          103 | 206.00     |              4   |
|          122 | 65.00      |              3   |
|          126 | 1500.00    |              5   |
|          126 | 1000.00    |              5   |
+--------------+------------+------------------+

这是我用来获取我正在寻找的数据的SQL代码:

Select s.[BidderNumber] as 'Bid #',
   ltrim(rtrim(b.bidderName)) as 'Name',
    isnull(sum(s.saleprice * Quantity),0) as 'Spent', 
   isnull(sum(t.Amount),0) as 'Paid', 
   case 
        when sum(t.Amount) is null then sum(s.saleprice * Quantity) 
        else
            case when (sum(t.Amount) > sum(s.saleprice * Quantity)) then (sum(t.Amount) - sum(s.saleprice * Quantity)) 
                 else (sum(s.saleprice * Quantity) - sum(t.amount ))
                 end
        end as 'Outstanding', b.cconfile, t.Notes --, s.biddernumber 
Into #Temp1
from sales s inner join Bidders b on s.BidderNumber = b.BidderNumber
             left  join transactions t on s.SaleCounter = t.SalesCounter  

group by s.BidderNumber, b.bidderName,b.CCOnFile, t.Notes  
order by s.Biddernumber, b.biddername, b.cconfile, t.Notes

  Select [Bid #], Name, Spent as 'Total Purchases', Paid as 'Current Payments', 
       case
            when CCOnFile = 1 then Sum(Outstanding)
           else
                case 
                        when CCOnFile = 0 then 0
                    end
            end as 'Amount To Charge Credit Card',
        case
            when CCOnFile = 0 then sum(Outstanding)
            ELSE
                case
                    when CCOnfile = 1 then 0
                    end
            end as 'Outstanding Balance',
            isnull(Notes,' ')as 'Notes'             
from #Temp1 
group by [Bid #], name, spent, paid,CCOnFile,Notes
order by [Bid #], Name, spent, paid, CCOnFile,Notes


drop table #temp1

以下是我要回的结果:

出价#名称总购买额当前付款金额为信用卡未付余额票据收取费用 101汤姆& Joan Bergland 2600.00 2600.00 0.00 0.00
103杰克&莎莉库克1412.00 706.00 0.00 706.00
106 Steve& Jan Cross 90.00 0.00 0.00 90.00
109 Cynthia Davis 945.00 0.00 0.00 945.00
122 Arnold& Peggy Jensen 65.00 65.00 0.00 0.00
126 Wayne& Connie Lindell 5000.00 2500.00 0.00 2500.00

我的问题很简单:为什么总购买量翻了一番?只有在交易表中有两条记录时才会发生加倍的总计(参见ID 103和126。

当然,SQL不是我最强的技能,但我认为我的编码正确。但经过3天的敲击,我真的可以使用一些建议。

谢谢!

1 个答案:

答案 0 :(得分:1)

看一下销售表: 4 103 353.00 2
和交易表:103 500.00 4
103 206.00 4

当您离开加入销售和交易时,因为要加入的密钥在事务表中有2行,因此结果将重复,请参阅下面的连接结果:
4 103 353.00 2 103 500.00 4
4 103 353.00 2 103 206.00 4

==>总购买量= 353 * 2 + 353 * 2 = 1412.00


在你的sql中,你应该将大小写移动到第二个选择语句,并将计算板移动到子选择,而不是左连接事务。

Select s.[BidderNumber] as 'Bid #',
    ltrim(rtrim(b.bidderName)) as 'Name',
    isnull(sum(s.saleprice * Quantity),0) as 'Spent', 
    (select isnull(sum(t.Amount),0) from transactions t
          where t.SalesCounter = s.SaleCounter) as 'Paid', b.cconfile, t.Notes --, s.biddernumber 
Into #Temp1
from sales s inner join Bidders b on s.BidderNumber = b.BidderNumber
group by s.BidderNumber, b.bidderName, b.CCOnFile
order by s.Biddernumber, b.biddername, b.cconfile