修复支付问题

时间:2011-11-20 22:55:45

标签: sql sql-server-2005 join duplicate-removal

我正在通过编辑重新发布原始问题,因为该问题得到了回答并且选择了最佳答案。

付款来自我们的供应商,该供应商转向帐户,代表根据哪个帐户获得多少付款。

Customers Table (Usage is kwH)
+----+----------+------------+----------+----------+----------+-------+-------+
| ID | Customer | Account_no | Meter_no | Supplier |  Active  | Usage | Repid | 
+----+----------+------------+----------+----------+----------+-------+-------+
|  1 | Joe      |        123 |      111 | NSTAR    | active   |  20   |  100  |
|  2 | Joe      |        123 |      222 | NSTAR    | active   |  30   |  100  |
|  3 | Joe      |        123 |      150 | NSTAR    | inactive |  60   |  100  |
|  4 | Sam      |        456 |      352 | SEP      | active   |  50   |  100  |
|  5 | Jill     |        789 |      222 | FES      | active   |  40   |  200  |
|  6 | Mike     |        883 |      150 | ABB      | inactive |  40   |  200  |
+----+----------+------------+----------+----------+----------+-------+-------+

Payment_Receive (table)
+------------+----------+-------------+-------------+
| Account_no | Supplier | Amount_paid | PaymentDate |
+------------+----------+-------------+-------------+
|        123 | NSTAR    | 20          | 2011-11-01  |
|        456 | SEP      | 40          | 2011-11-01  |
|        456 | SEP      | -40         | 2011-11-01  |
|        456 | SEP      | 40          | 2011-11-01  |
|        789 | FES      | 50          | 2011-11-01  |
|        883 | ABB      | 30          | 2011-11-01  |
+------------+----------+-------------+-------------+

这两张表用于代表支付。每个帐户都会收到付款,并根据Account_No和Supplier与我们的客户进行匹配。我们无法控制payment_table,因为它来自外部。这会产生某些问题,因为我们无法在两个表之间进行一对一的匹配。抛开这一点,我希望按照一定的标准计算RepID = 100的支付金额。这是我希望看到的RepId = 100

的输出
+------------+----------+-------------+-------------+-------------+
| Account_no | Supplier | Amount_paid |    Usage    | PaymentDate |
+------------+----------+-------------+-------------+-------------+
|        123 | NSTAR    | 20          |    60*      | 2011-11-01  |
|        456 | SEP      | 40          |    50       | 2011-11-01  |
|        456 | SEP      | -40         |    40       | 2011-11-01  |
|        456 | SEP      | 40          |    40       | 2011-11-01  |
+------------+----------+-------------+-------------+-------------+

请注意

  • Account_no 123在customers表中存在三次,它必须在rep payout中显示一次
  • 3笔款项已支付给account_no 456,这三笔款项必须在报告中显示
  • * 60 =请注意,有2条活动记录(和一条非活动记录)。这可能是两个活跃的总和。但是,任何其他值都是可以接受的,如果这使得查询变得容易(对于两个或一个中的更大,而不是另一个)
  • 请注意,Usage列必须出现在输出表中,这是为我创建问题的列。如果我不包含这一切,一切正常。
  • 使用列的点,如果我有同一个客户的两条记录具有相同的Account_No和供应商但用途不同,那么当我包含使用列时,这两条记录会有所不同。因此,distinct不能删除此副本。

报告按月计算


问题的脚本

create database testcase
go

use testcase 
go

create table customers (
  id int not null primary key identity,
  customer_name varchar(25),
  account_no int,
  meter_no int,
  supplier varchar(20),
  active varchar(20),
  usage int,
  repid int
)

create table payments_received (
  account_no int,
  supplier varchar(20),
  amount_paid float,
  paymentdate smalldatetime
)

insert into customers values('Joe',123, 111,'NSTAR','active',20,100)
insert into customers values('Joe',123, 222,'NSTAR','active',30, 100)
insert into customers values('Joe',123, 150,'NSTAR','inactive',60,100)

insert into customers values('Sam',456, 352,'SEP','active',40,100)
insert into customers values('Jill',789, 222,'FES','active',40,200)
insert into customers values('Mike',883, 150,'ABB','inactive',40,200)

select * from customers

insert into payments_received values(123,'NSTAR',20,'2011-11-01')
insert into payments_received values(456,'SEP',40,'2011-11-01')
insert into payments_received values(456,'SEP',-40,'2011-11-01')
insert into payments_received values(456,'SEP',40,'2011-11-01')

insert into payments_received values(789,'FES',50,'2011-11-01')
insert into payments_received values(883,'ABB',30,'2011-11-01')

select * from payments_received

1 个答案:

答案 0 :(得分:2)

这个怎么样:

CREATE VIEW v_customers_by_rep
AS
  SELECT SUM(USAGE) AS USAGE ,
         REPID ,
         CAST(account_no AS VARCHAR) + '_' + Supplier AS UniqueId
    FROM customers
GROUP BY CAST(account_no AS VARCHAR) + '_' + Supplier ,
         REPID
GO
DECLARE
  @repid INT

SET @repid = 100

SELECT pr.* ,
       u.Usage
  FROM payments_received pr INNER JOIN v_customers_by_rep u
    ON CAST(pr.account_no AS VARCHAR) + '_' + pr.Supplier = u.UniqueId
WHERE u.repid = @repid

如果需要,您还可以删除视图中的非活动记录。