【发布时间】:2012-01-02 13:51:19
【问题描述】:
我正在重新发布我的原始问题并进行修改,因为该问题已得到回答并选择了最佳答案。
付款来自我们的供应商,该供应商进入帐户,代表根据哪个帐户获得多少获得报酬。
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 和供应商与我们的客户匹配。我们无法控制 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 在客户表中存在三次,它必须在代表支付中显示一次
- 向 account_no 456 支付了 3 笔款项,这三笔款项都必须显示在报告中
- *60 = 请注意,有 2 条活动记录(和 1 条不活动记录)。这可能是两个活动的总和。但任何其他值都是可以接受的,如果这使得查询变得容易(对于两个或一个中的较大者,而不是另一个)
- 请注意,使用列必须出现在输出表中,这是给我带来问题的列。如果我不包括这个一切正常。
- 使用列的要点,如果我有两个相同客户的记录,它们具有相同的 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
【问题讨论】:
标签: sql sql-server-2005 join duplicate-removal